Skip to content

Commit 83fb531

Browse files
committed
Next version of PHPStan docparser
1 parent 07884cd commit 83fb531

File tree

8 files changed

+82
-91
lines changed

8 files changed

+82
-91
lines changed

SlevomatCodingStandard/Helpers/AnnotationHelper.php

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@
2525
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeItemNode;
2626
use PHPStan\PhpDocParser\Ast\Type\ObjectShapeNode;
2727
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
28-
use function count;
2928
use function in_array;
3029
use function sprintf;
31-
use function strlen;
3230
use function strtolower;
33-
use const T_DOC_COMMENT_STRING;
3431

3532
/**
3633
* @internal
@@ -67,11 +64,11 @@ static function () use ($phpcsFile, $docCommentOpenPointer, $name): array {
6764

6865
if ($parsedDocComment !== null) {
6966
foreach ($parsedDocComment->getNode()->getTags() as $node) {
70-
$annotationStartPointer = self::getStartPointer($phpcsFile, $parsedDocComment->getOpenPointer(), $node);
67+
$annotationStartPointer = $parsedDocComment->getNodeStartPointer($phpcsFile, $node);
7168
$annotations[] = new Annotation(
7269
$node,
7370
$annotationStartPointer,
74-
self::getEndPointer($phpcsFile, $parsedDocComment, $annotationStartPointer, $node)
71+
$parsedDocComment->getNodeEndPointer($phpcsFile, $node, $annotationStartPointer)
7572
);
7673
}
7774
}
@@ -309,64 +306,6 @@ public static function isAnnotationUseless(
309306
);
310307
}
311308

312-
private static function getStartPointer(File $phpcsFile, int $docCommentOpenPointer, PhpDocTagNode $annotationNode): int
313-
{
314-
$tokens = $phpcsFile->getTokens();
315-
316-
$tagStartLine = $tokens[$docCommentOpenPointer]['line'] + $annotationNode->getAttribute('startLine') - 1;
317-
318-
$searchPointer = $docCommentOpenPointer + 1;
319-
for ($i = $docCommentOpenPointer + 1; $i < $tokens[$docCommentOpenPointer]['comment_closer']; $i++) {
320-
if ($tagStartLine === $tokens[$i]['line']) {
321-
$searchPointer = $i;
322-
break;
323-
}
324-
}
325-
326-
return TokenHelper::findNext($phpcsFile, TokenHelper::$annotationTokenCodes, $searchPointer);
327-
}
328-
329-
private static function getEndPointer(
330-
File $phpcsFile,
331-
ParsedDocComment $parsedDocComment,
332-
int $annotationStartPointer,
333-
PhpDocTagNode $annotationNode
334-
): int
335-
{
336-
$tokens = $phpcsFile->getTokens();
337-
338-
$annotationContent = $parsedDocComment->getTokens()->getContentBetween(
339-
$annotationNode->getAttribute(Attribute::START_INDEX),
340-
$annotationNode->getAttribute(Attribute::END_INDEX) + 1
341-
);
342-
$annotationLength = strlen($annotationContent);
343-
344-
$searchPointer = $annotationStartPointer;
345-
346-
$content = '';
347-
for ($i = $annotationStartPointer; $i < count($tokens); $i++) {
348-
$content .= $tokens[$i]['content'];
349-
350-
if (strlen($content) >= $annotationLength) {
351-
$searchPointer = $i;
352-
break;
353-
}
354-
}
355-
356-
$nextAnnotationStartPointer = TokenHelper::findNext(
357-
$phpcsFile,
358-
TokenHelper::$annotationTokenCodes,
359-
$searchPointer + 1,
360-
$parsedDocComment->getClosePointer()
361-
);
362-
363-
$pointerAfter = $nextAnnotationStartPointer ?? $parsedDocComment->getClosePointer();
364-
365-
$stringPointerBefore = TokenHelper::findPrevious($phpcsFile, T_DOC_COMMENT_STRING, $pointerAfter - 1, $searchPointer);
366-
367-
return $stringPointerBefore ?? $searchPointer;
368-
}
369-
370309
private static function changeAnnotationNode(PhpDocTagNode $tagNode, Node $nodeToChange, Node $changedNode): PhpDocTagNode
371310
{
372311
static $visitor;

SlevomatCodingStandard/Helpers/ParsedDocComment.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
namespace SlevomatCodingStandard\Helpers;
44

5+
use PHP_CodeSniffer\Files\File;
6+
use PHPStan\PhpDocParser\Ast\Attribute;
7+
use PHPStan\PhpDocParser\Ast\Node;
58
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
69
use PHPStan\PhpDocParser\Parser\TokenIterator;
10+
use function array_merge;
11+
use function count;
12+
use function strlen;
13+
use function trim;
14+
use const T_DOC_COMMENT_STRING;
715

816
/**
917
* @internal
@@ -51,4 +59,50 @@ public function getTokens(): TokenIterator
5159
return $this->tokens;
5260
}
5361

62+
public function getNodeStartPointer(File $phpcsFile, Node $node): int
63+
{
64+
$tokens = $phpcsFile->getTokens();
65+
66+
$tagStartLine = $tokens[$this->openPointer]['line'] + $node->getAttribute('startLine') - 1;
67+
68+
$searchPointer = $this->openPointer + 1;
69+
for ($i = $this->openPointer + 1; $i < $this->closePointer; $i++) {
70+
if ($tagStartLine === $tokens[$i]['line']) {
71+
$searchPointer = $i;
72+
break;
73+
}
74+
}
75+
76+
return TokenHelper::findNext($phpcsFile, array_merge(TokenHelper::$annotationTokenCodes, [T_DOC_COMMENT_STRING]), $searchPointer);
77+
}
78+
79+
public function getNodeEndPointer(File $phpcsFile, Node $node, int $nodeStartPointer): int
80+
{
81+
$tokens = $phpcsFile->getTokens();
82+
83+
$content = trim($this->tokens->getContentBetween(
84+
$node->getAttribute(Attribute::START_INDEX),
85+
$node->getAttribute(Attribute::END_INDEX) + 1
86+
));
87+
$length = strlen($content);
88+
89+
$searchPointer = $nodeStartPointer;
90+
91+
$content = '';
92+
for ($i = $nodeStartPointer; $i < count($tokens); $i++) {
93+
$content .= $tokens[$i]['content'];
94+
95+
if (strlen($content) >= $length) {
96+
$searchPointer = $i;
97+
break;
98+
}
99+
}
100+
101+
return TokenHelper::findPrevious(
102+
$phpcsFile,
103+
array_merge(TokenHelper::$annotationTokenCodes, [T_DOC_COMMENT_STRING]),
104+
$searchPointer
105+
);
106+
}
107+
54108
}

SlevomatCodingStandard/Helpers/PhpDocParserHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static function getParser(): PhpDocParser
4242
true,
4343
true,
4444
$usedAttributes,
45+
true,
4546
true
4647
);
4748
}

SlevomatCodingStandard/Sniffs/Commenting/DocCommentSpacingSniff.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use function array_key_exists;
1818
use function array_keys;
1919
use function array_map;
20-
use function array_merge;
2120
use function array_values;
2221
use function asort;
2322
use function count;
@@ -35,7 +34,6 @@
3534
use function usort;
3635
use const T_DOC_COMMENT_OPEN_TAG;
3736
use const T_DOC_COMMENT_STAR;
38-
use const T_DOC_COMMENT_STRING;
3937
use const T_DOC_COMMENT_WHITESPACE;
4038

4139
class DocCommentSpacingSniff implements Sniff
@@ -103,34 +101,23 @@ public function process(File $phpcsFile, $docCommentOpenerPointer): void
103101

104102
$tokens = $phpcsFile->getTokens();
105103

106-
$firstContentStartPointer = TokenHelper::findNextExcluding(
104+
if (TokenHelper::findNextExcluding(
107105
$phpcsFile,
108106
[T_DOC_COMMENT_WHITESPACE, T_DOC_COMMENT_STAR],
109107
$docCommentOpenerPointer + 1,
110108
$tokens[$docCommentOpenerPointer]['comment_closer']
111-
);
112-
113-
if ($firstContentStartPointer === null) {
109+
) === null) {
114110
return;
115111
}
116112

117-
$firstContentEndPointer = $firstContentStartPointer;
118-
$actualPointer = $firstContentStartPointer;
119-
do {
120-
/** @var int $actualPointer */
121-
$actualPointer = TokenHelper::findNextExcluding(
122-
$phpcsFile,
123-
[T_DOC_COMMENT_STAR, T_DOC_COMMENT_WHITESPACE],
124-
$actualPointer + 1,
125-
$tokens[$docCommentOpenerPointer]['comment_closer'] + 1
126-
);
127-
128-
if ($tokens[$actualPointer]['code'] !== T_DOC_COMMENT_STRING) {
129-
break;
130-
}
113+
$parsedDocComment = DocCommentHelper::parseDocComment($phpcsFile, $docCommentOpenerPointer);
131114

132-
$firstContentEndPointer = $actualPointer;
133-
} while (true);
115+
$firstContentStartPointer = $parsedDocComment->getNodeStartPointer($phpcsFile, $parsedDocComment->getNode()->children[0]);
116+
$firstContentEndPointer = $parsedDocComment->getNodeEndPointer(
117+
$phpcsFile,
118+
$parsedDocComment->getNode()->children[0],
119+
$firstContentStartPointer
120+
);
134121

135122
$annotations = AnnotationHelper::getAnnotations($phpcsFile, $docCommentOpenerPointer);
136123
usort($annotations, static function (Annotation $a, Annotation $b): int {
@@ -142,11 +129,7 @@ public function process(File $phpcsFile, $docCommentOpenerPointer): void
142129

143130
/** @var int $lastContentEndPointer */
144131
$lastContentEndPointer = $annotationsCount > 0
145-
? TokenHelper::findPrevious(
146-
$phpcsFile,
147-
array_merge(TokenHelper::$annotationTokenCodes, [T_DOC_COMMENT_STRING]),
148-
$tokens[$docCommentOpenerPointer]['comment_closer'] - 1
149-
)
132+
? $annotations[$annotationsCount - 1]->getEndPointer()
150133
: $firstContentEndPointer;
151134

152135
$this->checkLinesBeforeFirstContent($phpcsFile, $docCommentOpenerPointer, $firstContentStartPointer);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.2 || ^8.0",
2020
"dealerdirect/phpcodesniffer-composer-installer": "^0.6.2 || ^0.7 || ^1.0",
21-
"phpstan/phpdoc-parser": "^1.22.0",
21+
"phpstan/phpdoc-parser": "^1.23.0",
2222
"squizlabs/php_codesniffer": "^3.7.1"
2323
},
2424
"require-dev": {

tests/Sniffs/Commenting/data/docCommentSpacingDefaultSettingsNoErrors.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
/**
88
* Description
9+
*
10+
* phpcs:disable SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
911
*/
1012
class Whatever
1113
{

tests/Sniffs/Commenting/data/docCommentSpacingModifiedSettingsNoErrors.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*
1111
* Description
1212
*
13+
* phpcs:disable SlevomatCodingStandard.Classes.RequireAbstractOrFinal.ClassNeitherAbstractNorFinal
14+
*
1315
*/
1416
class Whatever
1517
{

tests/Sniffs/Commenting/data/uselessFunctionDocCommentSniffNoErrors.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,14 @@ public function specificAnnotationInFQN(): void
191191
{
192192
}
193193

194+
/**
195+
* @param string $parameter
196+
* Some parameter
197+
* @return string
198+
* Some return value
199+
*/
200+
public function descriptionOnNextLine()
201+
{
202+
}
203+
194204
}

0 commit comments

Comments
 (0)