Skip to content

Commit

Permalink
Moved inline annotation parsing to the DocBlock type
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius authored and sebastianbergmann committed Sep 7, 2019
1 parent 38b7288 commit a5716b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
26 changes: 26 additions & 0 deletions src/Annotation/DocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,32 @@ public function getProvidedData() : ?array
return $data;
}

/**
* @psalm-return array<string, array{line: int, value: string}>
*/
public function getInlineAnnotations() : array
{
$code = \file($this->reflector->getFileName());
$lineNumber = $this->reflector->getStartLine();
$startLine = $this->reflector->getStartLine() - 1;
$endLine = $this->reflector->getEndLine() - 1;
$codeLines = \array_slice($code, $startLine, $endLine - $startLine + 1);
$annotations = [];

foreach ($codeLines as $line) {
if (\preg_match('#/\*\*?\s*@(?P<name>[A-Za-z_-]+)(?:[ \t]+(?P<value>.*?))?[ \t]*\r?\*/$#m', $line, $matches)) {
$annotations[\strtolower($matches['name'])] = [
'line' => $lineNumber,
'value' => $matches['value'],
];
}

$lineNumber++;
}

return $annotations;
}

private function parseSymbolAnnotations() : array
{
$annotations = [];
Expand Down
21 changes: 2 additions & 19 deletions src/Util/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,25 +436,8 @@ public static function getInlineAnnotations(string $className, string $methodNam
);
}

$code = \file($method->getFileName());
$lineNumber = $method->getStartLine();
$startLine = $method->getStartLine() - 1;
$endLine = $method->getEndLine() - 1;
$methodLines = \array_slice($code, $startLine, $endLine - $startLine + 1);
$annotations = [];

foreach ($methodLines as $line) {
if (\preg_match('#/\*\*?\s*@(?P<name>[A-Za-z_-]+)(?:[ \t]+(?P<value>.*?))?[ \t]*\r?\*/$#m', $line, $matches)) {
$annotations[\strtolower($matches['name'])] = [
'line' => $lineNumber,
'value' => $matches['value'],
];
}

$lineNumber++;
}

return $annotations;
return DocBlock::ofFunction($method)
->getInlineAnnotations();
}

// @TODO remove
Expand Down

0 comments on commit a5716b8

Please sign in to comment.