Skip to content

Commit

Permalink
[AutoImport] Handle complex usage of multiple @\ combined with other …
Browse files Browse the repository at this point in the history
…doc (#5273)

* [AutoImport] Handle complex usage of multiple @\ combined with other doc

* [ci-review] Rector Rectify

* fix re-create object

* Fix

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 22, 2023
1 parent fcd1b1a commit 284b1b9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes(
unset($phpDocNode->children[$key]);
array_splice($phpDocNode->children, $key, 0, $spacelessPhpDocTagNodes);

$key += count($spacelessPhpDocTagNodes);

continue;
}

Expand All @@ -177,6 +179,12 @@ private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes(
}

if (! $phpDocChildNode->value instanceof GenericTagValueNode) {
$key = $this->processDescriptionAsSpacelessPhpDoctagNode(
$phpDocNode,
$phpDocChildNode,
$currentPhpNode,
$key
);
continue;
}

Expand Down Expand Up @@ -207,6 +215,54 @@ private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes(
}
}

private function processDescriptionAsSpacelessPhpDoctagNode(
PhpDocNode $phpDocNode,
PhpDocTagNode $phpDocTagNode,
Node $currentPhpNode,
int $key
): int {
if (! property_exists($phpDocTagNode->value, 'description')) {
return $key;
}

$description = (string) $phpDocTagNode->value->description;
if (! str_contains($description, "\n")) {
return $key;
}

$phpDocTextNode = new PhpDocTextNode($description);
$startAndEnd = $phpDocTagNode->value->getAttribute(PhpDocAttributeKey::START_AND_END);
if (! $startAndEnd instanceof StartAndEnd) {
return $key;
}

$phpDocTextNode->setAttribute(PhpDocAttributeKey::START_AND_END, $startAndEnd);
$spacelessPhpDocTagNodes = $this->resolveFqnAnnotationSpacelessPhpDocTagNode(
$phpDocTextNode,
$currentPhpNode
);

if ($spacelessPhpDocTagNodes === []) {
return $key;
}

$unsetKey = $phpDocNode->children[$key] instanceof SpacelessPhpDocTagNode
? $key + count($spacelessPhpDocTagNodes)
: $key;

unset($phpDocNode->children[$unsetKey]);

$classNode = new PhpDocTagNode($phpDocTagNode->name, $phpDocTagNode->value);
$description = Strings::replace($description, self::LONG_ANNOTATION_REGEX, '');
$description = trim(str_replace("\n *", '', $description));
$phpDocTagNode->value->description = $description;
$phpDocNode->children[$unsetKey] = $classNode;

array_splice($phpDocNode->children, $unsetKey + 1, 0, $spacelessPhpDocTagNodes);

return $key + count($spacelessPhpDocTagNodes);
}

/**
* This is closed block, e.g. {( ... )},
* false on: {( ... )
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ parameters:
- packages/NodeNameResolver/NodeNameResolver.php
- utils/ChangelogGenerator/Command/
- packages/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php
- packages/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php

# known types
- '#Call to an undefined method PHPStan\\Type\\ConstantType\:\:getValue\(\)#'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TwoRoutesWithNextDoc2
* @Route("/first", methods={"GET"})
* @Route("/second", methods={"GET"})
* @return Response
* @\Symfony\Component\Routing\Annotation\Route("/third", methods={"GET"})
* @Route("/third", methods={"GET"})
*/
public function some(): Response
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,25 @@ class TwoRoutesWithPrevDoc
return new Response();
}
}

?>
-----
<?php

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

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

?>

0 comments on commit 284b1b9

Please sign in to comment.