Skip to content

Commit 6808bca

Browse files
committed
Fixed parsing really strange WordPress documentation
1 parent 27bff0d commit 6808bca

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

SlevomatCodingStandard/Helpers/AnnotationHelper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
use function get_class;
3737
use function in_array;
3838
use function preg_match;
39+
use function preg_match_all;
3940
use function preg_replace;
4041
use function sprintf;
41-
use function substr_count;
4242
use function trim;
4343
use const T_DOC_COMMENT_CLOSE_TAG;
4444
use const T_DOC_COMMENT_STAR;
@@ -238,7 +238,7 @@ public static function getAnnotations(File $phpcsFile, int $pointer): array
238238
$annotationEndPointer = $i;
239239

240240
// Fix for wrong PHPCS parsing
241-
$parenthesesLevel = substr_count($tokens[$i]['content'], '(') - substr_count($tokens[$i]['content'], ')');
241+
$parenthesesLevel = (int) preg_match_all('~[({]~', $tokens[$i]['content']) - (int) preg_match_all('~[)}]~', $tokens[$i]['content']);
242242
$annotationCode = $tokens[$i]['content'];
243243

244244
for ($j = $i + 1; $j <= $tokens[$docCommentOpenToken]['comment_closer']; $j++) {
@@ -267,7 +267,7 @@ public static function getAnnotations(File $phpcsFile, int $pointer): array
267267
}
268268
}
269269

270-
$parenthesesLevel += substr_count($tokens[$j]['content'], '(') - substr_count($tokens[$j]['content'], ')');
270+
$parenthesesLevel += (int) preg_match_all('~[({]~', $tokens[$j]['content']) - (int) preg_match_all('~[)}]~', $tokens[$j]['content']);
271271
$annotationCode .= $tokens[$j]['content'];
272272
}
273273

tests/Helpers/AnnotationHelperTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SlevomatCodingStandard\Helpers;
44

55
use PHP_CodeSniffer\Files\File;
6+
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
67
use SlevomatCodingStandard\Helpers\Annotation\GenericAnnotation;
78
use SlevomatCodingStandard\Helpers\Annotation\ParameterAnnotation;
89
use SlevomatCodingStandard\Helpers\Annotation\PropertyAnnotation;
@@ -167,6 +168,21 @@ public function testInlineDocCommentWithParametrizedAnnotation(): void
167168
self::assertNull($annotations[0]->getContent());
168169
}
169170

171+
public function testWordPressAnnotations(): void
172+
{
173+
$annotations = AnnotationHelper::getAnnotations($this->getTestedCodeSnifferFile(), $this->findFunctionPointerByName($this->getTestedCodeSnifferFile(), 'wordPress'));
174+
175+
self::assertCount(1, $annotations);
176+
self::assertCount(1, $annotations['@param']);
177+
178+
$annotation = $annotations['@param'][0];
179+
180+
self::assertInstanceOf(ParameterAnnotation::class, $annotation);
181+
self::assertInstanceOf(IdentifierTypeNode::class, $annotation->getType());
182+
self::assertSame('$parameters', $annotation->getParameterName());
183+
self::assertSame('{ Optional. Parameters for filtering the list of user assignments. Default empty array. @type bool $is_active Pass `true` to only return active user assignments and `false` to return inactive user assignments. @type DateTime|string $updated_since Only return user assignments that have been updated since the given date and time. }', $annotation->getDescription());
184+
}
185+
170186
public function testFunctionWithoutAnnotation(): void
171187
{
172188
self::assertCount(0, AnnotationHelper::getAnnotationsByName($this->getTestedCodeSnifferFile(), $this->findFunctionPointerByName($this->getTestedCodeSnifferFile(), 'withoutAnnotation'), '@param'));

tests/Helpers/data/annotation.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,18 @@ public function withReturnAnnotation()
8585

8686
}
8787

88+
/**
89+
* @param array $parameters {
90+
* Optional. Parameters for filtering the list of user assignments. Default empty array.
91+
*
92+
* @type bool $is_active Pass `true` to only return active user assignments and `false` to
93+
* return inactive user assignments.
94+
* @type DateTime|string $updated_since Only return user assignments that have been updated since the given
95+
* date and time.
96+
* }
97+
*/
98+
public function wordPress(array $parameters = [])
99+
{
100+
}
101+
88102
}

0 commit comments

Comments
 (0)