From 422114df3c3fb3ba5d3d72d3da9946f3cf802e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Mirtes?= Date: Fri, 30 Oct 2026 17:47:13 +0000 Subject: [PATCH] Remove phpdoc-parser workarounds fixed in phpdoc-parser 2.3.10 and 2.3.11 Amp-Thread-ID: https://ampcode.com/threads/T-01a17930-0285-73c5-a957-4325c77aa1b2 --- .../PhpDocParser/BetterPhpDocParser.php | 45 +------------------ .../Printer/PhpDocInfoPrinter.php | 29 ------------ 2 files changed, 1 insertion(+), 73 deletions(-) diff --git a/src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php b/src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php index 95f0e45102e..f398c3a2431 100644 --- a/src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php +++ b/src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php @@ -11,9 +11,6 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; -use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode; -use PHPStan\PhpDocParser\Lexer\Lexer; -use PHPStan\PhpDocParser\Parser\ParserException; use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface; @@ -34,17 +31,6 @@ */ private const string MULTI_NEW_LINES_REGEX = '#(?\r\n|\n){2,}#'; - /** - * @var int[] - */ - private const array NO_TEXT_TOKEN_TYPES = [ - Lexer::TOKEN_OPEN_PHPDOC, - Lexer::TOKEN_CLOSE_PHPDOC, - Lexer::TOKEN_PHPDOC_EOL, - Lexer::TOKEN_HORIZONTAL_WS, - Lexer::TOKEN_END, - ]; - /** * @var PhpDocNodeDecoratorInterface[] */ @@ -71,12 +57,7 @@ public function __construct( public function parseWithNode(TokenIterator $tokenIterator, Node $node): PhpDocNode { - try { - $phpDocNode = $this->phpDocParser->parse($tokenIterator); - } catch (ParserException) { - // invalid Doctrine annotation arguments; keep the docblock as it is - return $this->createTextPhpDocNode($tokenIterator); - } + $phpDocNode = $this->phpDocParser->parse($tokenIterator); foreach ($phpDocNode->children as $phpDocChildNode) { $this->setStartAndEnd($phpDocChildNode); @@ -120,28 +101,4 @@ private function setStartAndEnd(PhpDocParserNode $phpDocParserNode): void new StartAndEnd($startIndex, max($startIndex, $endIndex + 1)) ); } - - /** - * One text node with the content between "/**" and "*\/" - */ - private function createTextPhpDocNode(TokenIterator $tokenIterator): PhpDocNode - { - $tokens = $tokenIterator->getTokens(); - $startIndex = 0; - $endIndex = count($tokens) - 1; - - while ($startIndex < $endIndex && in_array($tokens[$startIndex][Lexer::TYPE_OFFSET], self::NO_TEXT_TOKEN_TYPES, true)) { - ++$startIndex; - } - - while ($endIndex > $startIndex && in_array($tokens[$endIndex][Lexer::TYPE_OFFSET], self::NO_TEXT_TOKEN_TYPES, true)) { - --$endIndex; - } - - $phpDocTextNode = new PhpDocTextNode($tokenIterator->getContentBetween($startIndex, $endIndex + 1)); - $phpDocTextNode->setAttribute(Attribute::START_INDEX, $startIndex); - $phpDocTextNode->setAttribute(Attribute::END_INDEX, $endIndex); - - return new PhpDocNode([$phpDocTextNode]); - } } diff --git a/src/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php b/src/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php index d3553876e74..986f61af7f4 100644 --- a/src/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php +++ b/src/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php @@ -79,8 +79,6 @@ public function printFormatPreserving(PhpDocInfo $phpDocInfo): string return $docContent; } - $this->reindexArrays($phpDocNode); - $originalPhpDocNode = $phpDocInfo->getOriginalPhpDocNode(); $phpDocNode = $this->removeTrailingEmptyLines($phpDocNode, $originalPhpDocNode); @@ -108,33 +106,6 @@ public function printToComments(PhpDocInfo $phpDocInfo): array return [new Comment($printedPhpDocContents)]; } - /** - * The printer compares lists of nodes; unset() in rules leaves gaps in the keys - */ - private function reindexArrays(Node $node): void - { - foreach (get_object_vars($node) as $propertyName => $value) { - if ($value instanceof Node) { - $this->reindexArrays($value); - continue; - } - - if (! is_array($value)) { - continue; - } - - if (! array_is_list($value)) { - $node->{$propertyName} = array_values($value); - } - - foreach ($value as $item) { - if ($item instanceof Node) { - $this->reindexArrays($item); - } - } - } - } - /** * Removing the last tag keeps the empty line above it, remove it too */