Skip to content

Commit

Permalink
Helpers::parseAnnotation() improved regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 3, 2018
1 parent cf8afe5 commit 485a875
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ public static function parseAnnotation(\Reflector $ref, string $name): ?string
if (!Reflection::areCommentsAvailable()) {
throw new Nette\InvalidStateException('You have to enable phpDoc comments in opcode cache.');
}
$name = preg_quote($name, '#');
if ($ref->getDocComment() && preg_match("#[\\s*]@$name(?:\\s++([^@]\\S*)?|$)#", trim($ref->getDocComment(), '/*'), $m)) {
$re = '#[\s*]@' . preg_quote($name, '#') . '(?=\s|$)(?:[ \t]+([^@\s]\S*))?#';
if ($ref->getDocComment() && preg_match($re, trim($ref->getDocComment(), '/*'), $m)) {
return $m[1] ?? '';
}
return null;
Expand Down
26 changes: 26 additions & 0 deletions tests/DI/Helpers.parseAnnotation().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,29 @@ class TestClass2
$rc = new ReflectionClass('TestClass2');

Assert::same('', Helpers::parseAnnotation($rc, 'return'));


/** @return
var
*/
class TestClass3
{
}

$rc = new ReflectionClass('TestClass3');

Assert::same('', Helpers::parseAnnotation($rc, 'return'));


/**
* @inject@var
*/
class TestClass4
{
}

$rc = new ReflectionClass('TestClass4');

Assert::same(null, Helpers::parseAnnotation($rc, 'inject'));
Assert::same(null, Helpers::parseAnnotation($rc, 'injec'));
Assert::same(null, Helpers::parseAnnotation($rc, 'var'));

0 comments on commit 485a875

Please sign in to comment.