Skip to content

Commit 485a875

Browse files
committed
Helpers::parseAnnotation() improved regexp
1 parent cf8afe5 commit 485a875

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/DI/Helpers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ public static function parseAnnotation(\Reflector $ref, string $name): ?string
215215
if (!Reflection::areCommentsAvailable()) {
216216
throw new Nette\InvalidStateException('You have to enable phpDoc comments in opcode cache.');
217217
}
218-
$name = preg_quote($name, '#');
219-
if ($ref->getDocComment() && preg_match("#[\\s*]@$name(?:\\s++([^@]\\S*)?|$)#", trim($ref->getDocComment(), '/*'), $m)) {
218+
$re = '#[\s*]@' . preg_quote($name, '#') . '(?=\s|$)(?:[ \t]+([^@\s]\S*))?#';
219+
if ($ref->getDocComment() && preg_match($re, trim($ref->getDocComment(), '/*'), $m)) {
220220
return $m[1] ?? '';
221221
}
222222
return null;

tests/DI/Helpers.parseAnnotation().phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,29 @@ class TestClass2
3636
$rc = new ReflectionClass('TestClass2');
3737

3838
Assert::same('', Helpers::parseAnnotation($rc, 'return'));
39+
40+
41+
/** @return
42+
var
43+
*/
44+
class TestClass3
45+
{
46+
}
47+
48+
$rc = new ReflectionClass('TestClass3');
49+
50+
Assert::same('', Helpers::parseAnnotation($rc, 'return'));
51+
52+
53+
/**
54+
* @inject@var
55+
*/
56+
class TestClass4
57+
{
58+
}
59+
60+
$rc = new ReflectionClass('TestClass4');
61+
62+
Assert::same(null, Helpers::parseAnnotation($rc, 'inject'));
63+
Assert::same(null, Helpers::parseAnnotation($rc, 'injec'));
64+
Assert::same(null, Helpers::parseAnnotation($rc, 'var'));

0 commit comments

Comments
 (0)