diff --git a/packages/BetterPhpDocParser/src/PhpDocNode/Symfony/SymfonyRouteTagValueNode.php b/packages/BetterPhpDocParser/src/PhpDocNode/Symfony/SymfonyRouteTagValueNode.php index 953fa43b3bfe..0d81b7267e30 100644 --- a/packages/BetterPhpDocParser/src/PhpDocNode/Symfony/SymfonyRouteTagValueNode.php +++ b/packages/BetterPhpDocParser/src/PhpDocNode/Symfony/SymfonyRouteTagValueNode.php @@ -4,6 +4,7 @@ namespace Rector\BetterPhpDocParser\PhpDocNode\Symfony; +use Nette\Utils\Strings; use Rector\BetterPhpDocParser\PhpDocNode\AbstractTagValueNode; use Symfony\Component\Routing\Annotation\Route; @@ -34,6 +35,11 @@ final class SymfonyRouteTagValueNode extends AbstractTagValueNode */ private $methods = []; + /** + * @var bool + */ + private $isPathExplicit = true; + /** * @param string[] $methods */ @@ -48,11 +54,14 @@ public function __construct( $this->methods = $methods; if ($originalContent !== null) { + $this->isPathExplicit = (bool) Strings::contains($originalContent, 'path='); + $this->resolveOriginalContentSpacingAndOrder($originalContent); // default value without key if ($this->path && ! in_array('path', (array) $this->orderedVisibleItems, true)) { - $this->orderedVisibleItems[] = 'path'; + // add path as first item + $this->orderedVisibleItems = array_merge(['path'], (array) $this->orderedVisibleItems); } } } @@ -60,7 +69,7 @@ public function __construct( public function __toString(): string { $contentItems = [ - 'path' => sprintf('path="%s"', $this->path), + 'path' => $this->createPath(), ]; if ($this->name) { @@ -82,4 +91,13 @@ public function changeMethods(array $methods): void $this->orderedVisibleItems[] = 'methods'; $this->methods = $methods; } + + private function createPath(): string + { + if ($this->isPathExplicit) { + return sprintf('path="%s"', $this->path); + } + + return sprintf('"%s"', $this->path); + } } diff --git a/packages/BetterPhpDocParser/src/PhpDocParser/BetterPhpDocParser.php b/packages/BetterPhpDocParser/src/PhpDocParser/BetterPhpDocParser.php index b15fbf2ae6e3..b784028e895d 100644 --- a/packages/BetterPhpDocParser/src/PhpDocParser/BetterPhpDocParser.php +++ b/packages/BetterPhpDocParser/src/PhpDocParser/BetterPhpDocParser.php @@ -177,6 +177,9 @@ private function parseChildAndStoreItsPositions(TokenIterator $tokenIterator): N $tokenStart = $this->getTokenIteratorIndex($tokenIterator); $phpDocNode = $this->privatesCaller->callPrivateMethod($this, 'parseChild', $tokenIterator); $tokenEnd = $this->getTokenIteratorIndex($tokenIterator); + + $tokenEnd = $this->adjustTokenEndToFitClassAnnotation($tokenIterator, $tokenEnd); + $startEndValueObject = new StartEndValueObject($tokenStart, $tokenEnd); $attributeAwareNode = $this->attributeAwareNodeFactory->createFromNode($phpDocNode); @@ -303,4 +306,30 @@ private function isTagMatchingPhpDocNodeFactory( return false; } + + /** + * @see https://github.com/rectorphp/rector/issues/2158 + * + * Need to find end of this bracket first, because the parseChild() skips class annotatinos + */ + private function adjustTokenEndToFitClassAnnotation(TokenIterator $tokenIterator, int $tokenEnd): int + { + $tokens = $this->privatesAccessor->getPrivateProperty($tokenIterator, 'tokens'); + if ($tokens[$tokenEnd][0] !== '(') { + return $tokenEnd; + } + + while ($tokens[$tokenEnd][0] !== ')') { + ++$tokenEnd; + + // to prevent missing index error + if (! isset($tokens[$tokenEnd])) { + return --$tokenEnd; + } + } + + ++$tokenEnd; + + return $tokenEnd; + } } diff --git a/packages/BetterPhpDocParser/src/Printer/PhpDocInfoPrinter.php b/packages/BetterPhpDocParser/src/Printer/PhpDocInfoPrinter.php index 92686467080d..dd1b1957cfae 100644 --- a/packages/BetterPhpDocParser/src/Printer/PhpDocInfoPrinter.php +++ b/packages/BetterPhpDocParser/src/Printer/PhpDocInfoPrinter.php @@ -108,6 +108,7 @@ private function printPhpDocNode( // node output $nodeCount = count($attributeAwarePhpDocNode->children); + foreach ($attributeAwarePhpDocNode->children as $i => $phpDocChildNode) { $output .= $this->printNode($phpDocChildNode, null, $i + 1, $nodeCount, $shouldSkipEmptyLinesAbove); } diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture.php.inc index c3a358cb6d10..20497cd4278a 100644 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture.php.inc @@ -2,10 +2,9 @@ namespace AppBundle\Controller; -use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; -class ClassWithNamedService13Controller extends SymfonyController +class ClassWithNamedService13Controller { /** * @Template @@ -38,10 +37,9 @@ class ClassWithNamedService13Controller extends SymfonyController namespace AppBundle\Controller; -use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; -class ClassWithNamedService13Controller extends SymfonyController +class ClassWithNamedService13Controller { public function indexAction() { diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture2.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture2.php.inc index bdc88216cf33..a64103cfc604 100644 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture2.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture2.php.inc @@ -2,10 +2,9 @@ namespace AppBundle\Controller; -use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; -class ClassWithNamedService23Controller extends SymfonyController +class ClassWithNamedService23Controller { /** * @Template() @@ -32,10 +31,9 @@ class ClassWithNamedService23Controller extends SymfonyController namespace AppBundle\Controller; -use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; -class ClassWithNamedService23Controller extends SymfonyController +class ClassWithNamedService23Controller { public function indexAction() { diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture3.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture3.php.inc index 6d0494e62d1a..02fb2e999b16 100644 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture3.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture3.php.inc @@ -2,10 +2,9 @@ namespace AppBundle\Controller; -use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; -class ClassWithNamedService33Controller extends SymfonyController +class ClassWithNamedService33Controller { /** * @Template() @@ -38,10 +37,9 @@ class ClassWithNamedService33Controller extends SymfonyController namespace AppBundle\Controller; -use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; -class ClassWithNamedService33Controller extends SymfonyController +class ClassWithNamedService33Controller { public function indexAction() { diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture4.php.inc b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture4.php.inc index 0e9308070a4a..44758218807f 100644 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture4.php.inc +++ b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Fixture/Version3/fixture4.php.inc @@ -1,9 +1,8 @@ +----- +render('PAPPUserBundle:Facility:facility.html.twig'); + } +} + +?> diff --git a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Source/SymfonyController.php b/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Source/SymfonyController.php deleted file mode 100644 index 9ce345737978..000000000000 --- a/packages/Sensio/tests/Rector/FrameworkExtraBundle/TemplateAnnotationRector/Source/SymfonyController.php +++ /dev/null @@ -1,10 +0,0 @@ -