Skip to content

Commit

Permalink
Improved annotations parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jan 28, 2018
1 parent 2189ea4 commit 872ecd4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SlevomatCodingStandard/Helpers/AnnotationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function getAnnotations(\PHP_CodeSniffer\Files\File $codeSnifferFi
$annotationName = $tokens[$i]['content'];
$annotationParameters = null;
$annotationContent = null;
if (preg_match('~^(@[a-zA-Z\\\\]+)(?:\(([^)]*)\))?(?:\\s+(.+))?($)~s', $annotationCode, $matches)) {
if (preg_match('~^(@[a-zA-Z\\\\]+)(?:\((.*?)\))?(?:\\s+(.+))?($)~s', $annotationCode, $matches)) {
$annotationName = $matches[1];
$annotationParameters = trim($matches[2]);
if ($annotationParameters === '') {
Expand Down
8 changes: 8 additions & 0 deletions tests/Helpers/AnnotationHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public function testFunctionWithParametrizedAnnotation(): void
$this->assertNull($annotations[0]->getContent());
}

public function testFunctionWithParametrizedAnnotationContainingParenthesis(): void
{
$annotations = AnnotationHelper::getAnnotationsByName($this->getTestedCodeSnifferFile(), $this->findFunctionPointerByName($this->getTestedCodeSnifferFile(), 'withParametrizedAnnotationContainingParenthesis'), '@Security');
$this->assertCount(1, $annotations);
$this->assertSame('"is_granted(\'ROLE_ADMIN\')"', $annotations[0]->getParameters());
$this->assertNull($annotations[0]->getContent());
}

public function testFunctionWithMultilineParametrizedAnnotation(): void
{
$annotations = AnnotationHelper::getAnnotationsByName($this->getTestedCodeSnifferFile(), $this->findFunctionPointerByName($this->getTestedCodeSnifferFile(), 'withMultilineParametrizedAnnotation'), '@Route');
Expand Down
5 changes: 5 additions & 0 deletions tests/Helpers/data/annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function withAnnotation($b, $c);
*/
public function withParametrizedAnnotation();

/**
* @Security("is_granted('ROLE_ADMIN')")
*/
public function withParametrizedAnnotationContainingParenthesis();

/**
* @Assert\Callback()
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Sniffs/TypeHints/TypeHintDeclarationSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testNoErrors(): void
'@see',
'@Assert\Callback',
'@Something\\',
'@Security',
],
]));
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Sniffs/TypeHints/data/typeHintDeclarationNoErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,4 +654,12 @@ public function parameterHasDescriptionContainingVariable(bool $foo, int $bar) :
{
}

/**
* @Security("is_granted('ROLE_ADMIN')")
*/
public function usefullSymfonySecurityAnnotation()
{

}

}

0 comments on commit 872ecd4

Please sign in to comment.