From e7e40f175015799441dde3e99663dd6e88d87dea Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 23 Nov 2023 07:27:42 +0700 Subject: [PATCH] [AutoImport] Handle multile @\ with comment before (#5275) * [AutoImport] Handle multile @\ with comment before * fix * fix --- .../DoctrineAnnotationDecorator.php | 46 +++++++++++++------ .../two_routes_with_comment_before.php.inc | 39 ++++++++++++++++ 2 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 tests/Issues/AutoImport/Fixture/DocBlock/two_routes_with_comment_before.php.inc diff --git a/packages/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php b/packages/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php index 215882d348d..8267ab0a1eb 100644 --- a/packages/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php +++ b/packages/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php @@ -150,6 +150,37 @@ private function mergeNestedDoctrineAnnotations(PhpDocNode $phpDocNode): void } } + private function processTextSpacelessInTextNode( + PhpDocNode $phpDocNode, + PhpDocTextNode $phpDocTextNode, + Node $currentPhpNode, + int $key + ): int { + $spacelessPhpDocTagNodes = $this->resolveFqnAnnotationSpacelessPhpDocTagNode( + $phpDocTextNode, + $currentPhpNode + ); + + if ($spacelessPhpDocTagNodes === []) { + return $key; + } + + $otherText = Strings::replace($phpDocTextNode->text, self::LONG_ANNOTATION_REGEX, ''); + if (! in_array($otherText, ["\n", ""], true)) { + $phpDocNode->children[$key] = new PhpDocTextNode($otherText); + array_splice($phpDocNode->children, $key + 1, 0, $spacelessPhpDocTagNodes); + + $key += count($spacelessPhpDocTagNodes); + } else { + unset($phpDocNode->children[$key]); + array_splice($phpDocNode->children, $key, 0, $spacelessPhpDocTagNodes); + + $key += count($spacelessPhpDocTagNodes) - 1; + } + + return $key; + } + private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes( PhpDocNode $phpDocNode, Node $currentPhpNode @@ -157,20 +188,7 @@ private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes( foreach ($phpDocNode->children as $key => $phpDocChildNode) { // the @\FQN use case if ($phpDocChildNode instanceof PhpDocTextNode) { - $spacelessPhpDocTagNodes = $this->resolveFqnAnnotationSpacelessPhpDocTagNode( - $phpDocChildNode, - $currentPhpNode - ); - - if ($spacelessPhpDocTagNodes === []) { - continue; - } - - unset($phpDocNode->children[$key]); - array_splice($phpDocNode->children, $key, 0, $spacelessPhpDocTagNodes); - - $key += count($spacelessPhpDocTagNodes); - + $key = $this->processTextSpacelessInTextNode($phpDocNode, $phpDocChildNode, $currentPhpNode, $key); continue; } diff --git a/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_with_comment_before.php.inc b/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_with_comment_before.php.inc new file mode 100644 index 00000000000..04f023bfa37 --- /dev/null +++ b/tests/Issues/AutoImport/Fixture/DocBlock/two_routes_with_comment_before.php.inc @@ -0,0 +1,39 @@ + +----- +