Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test case for #2158 #2181

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\PhpDocNode\Symfony;

use Nette\Utils\Strings;
use Rector\BetterPhpDocParser\PhpDocNode\AbstractTagValueNode;
use Symfony\Component\Routing\Annotation\Route;

Expand Down Expand Up @@ -34,6 +35,11 @@ final class SymfonyRouteTagValueNode extends AbstractTagValueNode
*/
private $methods = [];

/**
* @var bool
*/
private $isPathExplicit = true;

/**
* @param string[] $methods
*/
Expand All @@ -48,19 +54,22 @@ 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);
}
}
}

public function __toString(): string
{
$contentItems = [
'path' => sprintf('path="%s"', $this->path),
'path' => $this->createPath(),
];

if ($this->name) {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php declare (strict_types=1);

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService43 extends SymfonyController
class ClassWithNamedService43
{
/**
* @Template("AdminBundle:Payment:create.html.twig")
Expand All @@ -20,10 +19,9 @@ class ClassWithNamedService43 extends SymfonyController
-----
<?php declare (strict_types=1);

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService43 extends SymfonyController
class ClassWithNamedService43
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Fixture;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;

class SkipJustTemplateController extends SymfonyController
class SkipJustTemplateController
{
/**
* @Template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace AppBundle\Controller;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService15Controller extends SymfonyController
class ClassWithNamedService15Controller
{
/**
* @Template
Expand Down Expand Up @@ -38,10 +37,9 @@ class ClassWithNamedService15Controller extends SymfonyController

namespace AppBundle\Controller;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService15Controller extends SymfonyController
class ClassWithNamedService15Controller
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace AppBundle\Controller;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService25Controller extends SymfonyController
class ClassWithNamedService25Controller
{
/**
* @Template()
Expand All @@ -32,10 +31,9 @@ class ClassWithNamedService25Controller extends SymfonyController

namespace AppBundle\Controller;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService25Controller extends SymfonyController
class ClassWithNamedService25Controller
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace AppBundle\Controller;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService35Controller extends SymfonyController
class ClassWithNamedService35Controller
{
/**
* @Template()
Expand Down Expand Up @@ -38,10 +37,9 @@ class ClassWithNamedService35Controller extends SymfonyController

namespace AppBundle\Controller;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService35Controller extends SymfonyController
class ClassWithNamedService35Controller
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace App\Controller;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService45Controller extends SymfonyController
class ClassWithNamedService45Controller
{
/**
* @Template("AdminBundle:Payment:create.html.twig")
Expand All @@ -24,10 +23,9 @@ class ClassWithNamedService45Controller extends SymfonyController

namespace App\Controller;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService45Controller extends SymfonyController
class ClassWithNamedService45Controller
{
public function indexAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace App\Controller;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService55Controller extends SymfonyController
class ClassWithNamedService55Controller
{
/**
* @Template()
Expand All @@ -22,10 +21,9 @@ class ClassWithNamedService55Controller extends SymfonyController

namespace App\Controller;

use Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Source\SymfonyController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ClassWithNamedService55Controller extends SymfonyController
class ClassWithNamedService55Controller
{
public function index()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;

class WithRouteToo
{
/**
* @Route("/change", name="facility_change")
* @Template("PAPPUserBundle:Facility:facility.html.twig")
*/
public function index()
{
return [];
}
}

?>
-----
<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;

class WithRouteToo
{
/**
* @Route("/change", name="facility_change")
*/
public function index()
{
return $this->render('PAPPUserBundle:Facility:facility.html.twig');
}
}

?>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function provideDataForTest(): Iterator
yield [__DIR__ . '/Fixture/Version5/fixture3.php.inc'];
yield [__DIR__ . '/Fixture/Version5/fixture4.php.inc'];
yield [__DIR__ . '/Fixture/Version5/fixture5.php.inc'];
yield [__DIR__ . '/Fixture/Version5/with_route_too.php.inc'];
}

/**
Expand Down
Loading