Skip to content

Commit

Permalink
Fix PHPDoc for methods with common closure
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Apr 3, 2019
1 parent ddb7ec3 commit 32cd7ae
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 254 deletions.
45 changes: 43 additions & 2 deletions src/Types/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected function getMethodsDefinitions($boot, $source)
$methods = '';
$source = str_replace('\\', '/', realpath($source));
$sourceLength = strlen($source);
$files = array();

foreach ($this->getMethods($boot) as $name => $closure) {
try {
Expand All @@ -50,17 +51,57 @@ protected function getMethodsDefinitions($boot, $source)
continue;
}

$file = str_replace('\\', '/', $function->getFileName());
$file = $function->getFileName();

if (!isset($files[$file])) {
$files[$file] = file($file);
}

$lines = $files[$file];
$file = str_replace('\\', '/', $file);

if (substr($file, 0, $sourceLength + 1) !== "$source/") {
continue;
}

$file = substr($file, $sourceLength + 1);

$parameters = implode(', ', array_map(array($this, 'dumpParameter'), $function->getParameters()));
$methodDocBlock = trim($function->getDocComment() ?: '');
$length = $function->getStartLine() - 1;
$code = array_slice($lines, 0, $length);
$className = '\\'.str_replace('/', '\\', substr($file, 0, -4));

for ($i = $length - 1; $i >= 0; $i--) {
if (preg_match('/^\s*(public|protected)\s+function\s+(\S+)\(.*\)(\s*\{)?$/', $code[$i], $match)) {
if ($name !== $match[2]) {
$method = new \ReflectionMethod($className, $name);
$methodFile = $method->getFileName();

if (!isset($files[$methodFile])) {
$files[$methodFile] = file($methodFile);
}

$length = $method->getEndLine() - 1;
$lines = $files[$methodFile];
$code = array_slice($lines, 0, $length);

for ($i = $length - 1; $i >= 0; $i--) {
if (preg_match('/^\s*(public|protected)\s+function\s+(\S+)\(.*\)(\s*\{)?$/', $code[$i], $match)) {
break;
}
}

$code = implode('', array_slice($code, $i));

if (preg_match('/(\/\*\*[\s\S]+\*\/)\s+return\s/U', $code, $match)) {
$methodDocBlock = $match[1];
}
}

break;
}
}

$file .= ':'.$function->getStartLine();

if ($methods !== '') {
Expand Down
Loading

0 comments on commit 32cd7ae

Please sign in to comment.