Skip to content

Commit

Permalink
[AutoImport] Handle multile @\ with comment before (#5275)
Browse files Browse the repository at this point in the history
* [AutoImport] Handle multile @\ with comment before

* fix

* fix
  • Loading branch information
samsonasik committed Nov 23, 2023
1 parent 152b8e3 commit e7e40f1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,45 @@ 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
): void {
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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Core\Tests\Issues\AutoImport\Fixture\DocBlock;

class TwoRoutesWithCommentBefore
{
/**
* Testsssssssssss
*
* @\Symfony\Component\Routing\Annotation\Route("/first", methods={"GET"})
* @\Symfony\Component\Routing\Annotation\Route("/second", methods={"GET"})
*/
public function some(): Response
{
return new Response();
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\AutoImport\Fixture\DocBlock;

use Symfony\Component\Routing\Annotation\Route;
class TwoRoutesWithCommentBefore
{
/**
* Testsssssssssss
* @Route("/first", methods={"GET"})
* @Route("/second", methods={"GET"})
*/
public function some(): Response
{
return new Response();
}
}

?>

0 comments on commit e7e40f1

Please sign in to comment.