Skip to content

Commit 872ecd4

Browse files
committed
Improved annotations parsing
1 parent 2189ea4 commit 872ecd4

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

SlevomatCodingStandard/Helpers/AnnotationHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function getAnnotations(\PHP_CodeSniffer\Files\File $codeSnifferFi
6666
$annotationName = $tokens[$i]['content'];
6767
$annotationParameters = null;
6868
$annotationContent = null;
69-
if (preg_match('~^(@[a-zA-Z\\\\]+)(?:\(([^)]*)\))?(?:\\s+(.+))?($)~s', $annotationCode, $matches)) {
69+
if (preg_match('~^(@[a-zA-Z\\\\]+)(?:\((.*?)\))?(?:\\s+(.+))?($)~s', $annotationCode, $matches)) {
7070
$annotationName = $matches[1];
7171
$annotationParameters = trim($matches[2]);
7272
if ($annotationParameters === '') {

tests/Helpers/AnnotationHelperTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public function testFunctionWithParametrizedAnnotation(): void
7070
$this->assertNull($annotations[0]->getContent());
7171
}
7272

73+
public function testFunctionWithParametrizedAnnotationContainingParenthesis(): void
74+
{
75+
$annotations = AnnotationHelper::getAnnotationsByName($this->getTestedCodeSnifferFile(), $this->findFunctionPointerByName($this->getTestedCodeSnifferFile(), 'withParametrizedAnnotationContainingParenthesis'), '@Security');
76+
$this->assertCount(1, $annotations);
77+
$this->assertSame('"is_granted(\'ROLE_ADMIN\')"', $annotations[0]->getParameters());
78+
$this->assertNull($annotations[0]->getContent());
79+
}
80+
7381
public function testFunctionWithMultilineParametrizedAnnotation(): void
7482
{
7583
$annotations = AnnotationHelper::getAnnotationsByName($this->getTestedCodeSnifferFile(), $this->findFunctionPointerByName($this->getTestedCodeSnifferFile(), 'withMultilineParametrizedAnnotation'), '@Route');

tests/Helpers/data/annotation.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function withAnnotation($b, $c);
3636
*/
3737
public function withParametrizedAnnotation();
3838

39+
/**
40+
* @Security("is_granted('ROLE_ADMIN')")
41+
*/
42+
public function withParametrizedAnnotationContainingParenthesis();
43+
3944
/**
4045
* @Assert\Callback()
4146
*/

tests/Sniffs/TypeHints/TypeHintDeclarationSniffTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function testNoErrors(): void
2222
'@see',
2323
'@Assert\Callback',
2424
'@Something\\',
25+
'@Security',
2526
],
2627
]));
2728
}

tests/Sniffs/TypeHints/data/typeHintDeclarationNoErrors.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,4 +654,12 @@ public function parameterHasDescriptionContainingVariable(bool $foo, int $bar) :
654654
{
655655
}
656656

657+
/**
658+
* @Security("is_granted('ROLE_ADMIN')")
659+
*/
660+
public function usefullSymfonySecurityAnnotation()
661+
{
662+
663+
}
664+
657665
}

0 commit comments

Comments
 (0)