-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Symfony] Add MergeMethodAnnotationToRouteAnnotationRector
- Loading branch information
1 parent
4fbca58
commit 3756e25
Showing
15 changed files
with
367 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/NetteToSymfony/src/PhpDocParser/Ast/PhpDoc/SymfonyMethodTagValueNode.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\NetteToSymfony\PhpDocParser\Ast\PhpDoc; | ||
|
||
use Rector\BetterPhpDocParser\PhpDocParser\Ast\PhpDoc\AbstractTagValueNode; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | ||
|
||
final class SymfonyMethodTagValueNode extends AbstractTagValueNode | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const SHORT_NAME = '@Method'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public const CLASS_NAME = Method::class; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $methods = []; | ||
|
||
/** | ||
* @param string[] $methods | ||
*/ | ||
public function __construct(array $methods = []) | ||
{ | ||
$this->methods = $methods; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return '(' . $this->printArrayItem($this->methods) . ')'; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getMethods(): array | ||
{ | ||
return $this->methods; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/Symfony/src/Rector/ClassMethod/MergeMethodAnnotationToRouteAnnotationRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\Symfony\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use Rector\NetteToSymfony\PhpDocParser\Ast\PhpDoc\SymfonyMethodTagValueNode; | ||
use Rector\NetteToSymfony\PhpDocParser\Ast\PhpDoc\SymfonyRouteTagValueNode; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\RectorDefinition\CodeSample; | ||
use Rector\RectorDefinition\RectorDefinition; | ||
|
||
/** | ||
* @see https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#method-annotation | ||
* @see https://stackoverflow.com/questions/51171934/how-to-fix-symfony-3-4-route-and-method-deprecation | ||
* | ||
* @see \Rector\Symfony\Tests\Rector\ClassMethod\MergeMethodAnnotationToRouteAnnotationRector\MergeMethodAnnotationToRouteAnnotationRectorTest | ||
*/ | ||
final class MergeMethodAnnotationToRouteAnnotationRector extends AbstractRector | ||
{ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Merge removed @Method annotation to @Route one', [ | ||
new CodeSample( | ||
<<<'PHP' | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
class DefaultController extends Controller | ||
{ | ||
/** | ||
* @Route("/show/{id}") | ||
* @Method({"GET", "HEAD"}) | ||
*/ | ||
public function show($id) | ||
{ | ||
} | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
use Symfony\Component\Routing\Annotation\Route; | ||
class DefaultController extends Controller | ||
{ | ||
/** | ||
* @Route("/show/{id}", methods={"GET","HEAD"}) | ||
*/ | ||
public function show($id) | ||
{ | ||
} | ||
} | ||
PHP | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE); | ||
if ($classNode === null) { | ||
return null; | ||
} | ||
|
||
if (! $this->isObjectType($classNode, '*Controller')) { | ||
return null; | ||
} | ||
|
||
if (! $node->isPublic()) { | ||
return null; | ||
} | ||
|
||
$phpDocInfo = $this->getPhpDocInfo($node); | ||
if ($phpDocInfo === null) { | ||
return null; | ||
} | ||
|
||
$symfonyMethodPhpDocTagValueNode = $phpDocInfo->getByType(SymfonyMethodTagValueNode::class); | ||
if ($symfonyMethodPhpDocTagValueNode === null) { | ||
return null; | ||
} | ||
|
||
$methods = $symfonyMethodPhpDocTagValueNode->getMethods(); | ||
|
||
/** @var SymfonyRouteTagValueNode $symfonyRoutePhpDocTagValueNode */ | ||
$symfonyRoutePhpDocTagValueNode = $phpDocInfo->getByType(SymfonyRouteTagValueNode::class); | ||
$symfonyRoutePhpDocTagValueNode->changeMethods($methods); | ||
|
||
$phpDocInfo->removeTagValueNodeFromNode($symfonyMethodPhpDocTagValueNode); | ||
|
||
$this->docBlockManipulator->updateNodeWithPhpDocInfo($node, $phpDocInfo); | ||
|
||
return $node; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...s/Rector/ClassMethod/MergeMethodAnnotationToRouteAnnotationRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\Rector\ClassMethod\MergeMethodAnnotationToRouteAnnotationRector\Fixture; | ||
|
||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
class DefaultController | ||
{ | ||
/** | ||
* @Route("/show/{id}") | ||
* @Method({"GET", "HEAD"}) | ||
*/ | ||
public function show($id) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\Rector\ClassMethod\MergeMethodAnnotationToRouteAnnotationRector\Fixture; | ||
|
||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
class DefaultController | ||
{ | ||
/** | ||
* @Route(path="/show/{id}", methods={"GET", "HEAD"}) | ||
*/ | ||
public function show($id) | ||
{ | ||
} | ||
} | ||
|
||
?> |
Oops, something went wrong.