mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-05 10:50:48 +00:00
Merge pull request #16 from TomasVotruba/symfony-class-replaces
[Symfony] framework bundle class replaces + form isValid()
This commit is contained in:
commit
387a721b1e
|
@ -45,9 +45,14 @@ parameters:
|
|||
# classes might not exist
|
||||
- */src/Rector/Contrib/Nette/*Rector.php
|
||||
- src/Rector/Contrib/Symfony/StringFormTypeToClassRector.php
|
||||
- src/Rector/Contrib/Symfony/FormIsValidRector.php
|
||||
Symplify\CodingStandard\Sniffs\Debug\CommentedOutCodeSniff:
|
||||
# examples of code to be found
|
||||
- src/Rector/Contrib/Symfony/GetterToPropertyRector.php
|
||||
SlevomatCodingStandard\Sniffs\Classes\UnusedPrivateElementsSniff:
|
||||
# will be used soon
|
||||
- packages/NodeTypeResolver/src/TypeContext.php
|
||||
|
||||
PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff:
|
||||
# long FQN classes that might not exist
|
||||
- src/Rector/Contrib/Symfony/FrameworkBundleClassReplacementsRector.php
|
||||
|
|
|
@ -13,7 +13,7 @@ final class NodeConnector extends NodeVisitorAbstract
|
|||
/**
|
||||
* @var Node
|
||||
*/
|
||||
private $stack;
|
||||
private $stack = [];
|
||||
|
||||
/**
|
||||
* @var Node
|
||||
|
|
41
src/Rector/AbstractClassReplacerRector.php
Normal file
41
src/Rector/AbstractClassReplacerRector.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
|
||||
abstract class AbstractClassReplacerRector extends AbstractRector
|
||||
{
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fqnName = $node->toString();
|
||||
|
||||
return isset($this->getOldToNewClasses()[$fqnName]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Name $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$newName = $this->getNewName($node->toString());
|
||||
|
||||
return new FullyQualified($newName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
abstract protected function getOldToNewClasses(): array;
|
||||
|
||||
private function getNewName(string $oldName): string
|
||||
{
|
||||
return $this->getOldToNewClasses()[$oldName];
|
||||
}
|
||||
}
|
71
src/Rector/Contrib/Symfony/FormIsValidRector.php
Normal file
71
src/Rector/Contrib/Symfony/FormIsValidRector.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\Symfony;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\Rector\AbstractRector;
|
||||
|
||||
/**
|
||||
* Converts all:
|
||||
* $form->isValid()
|
||||
*
|
||||
* into:
|
||||
* $form->isSubmitted() && $form->isValid()
|
||||
*/
|
||||
final class FormIsValidRector extends AbstractRector
|
||||
{
|
||||
public function getSetName(): string
|
||||
{
|
||||
return SetNames::SYMFONY;
|
||||
}
|
||||
|
||||
public function sinceVersion(): float
|
||||
{
|
||||
return 4.0;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof MethodCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($node->var->getAttribute('type') !== 'Symfony\Component\Form\Form') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((string) $node->name !== 'isValid') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($node->getAttribute('prev') !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$varName = $node->var->name;
|
||||
|
||||
return new BooleanAnd(
|
||||
$this->createMethodCall($varName, 'isSubmitted'),
|
||||
$this->createMethodCall($varName, 'isValid')
|
||||
);
|
||||
}
|
||||
|
||||
private function createMethodCall(string $varName, string $methodName): MethodCall
|
||||
{
|
||||
$varNode = new Variable($varName);
|
||||
|
||||
return new MethodCall($varNode, $methodName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\Symfony;
|
||||
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\Rector\AbstractClassReplacerRector;
|
||||
|
||||
/**
|
||||
* Ref.: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#frameworkbundle
|
||||
*
|
||||
* FrameworkBundle classes replaced by new ones
|
||||
*/
|
||||
final class FrameworkBundleClassReplacementsRector extends AbstractClassReplacerRector
|
||||
{
|
||||
public function getSetName(): string
|
||||
{
|
||||
return SetNames::SYMFONY;
|
||||
}
|
||||
|
||||
public function sinceVersion(): float
|
||||
{
|
||||
return 4.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getOldToNewClasses(): array
|
||||
{
|
||||
return [
|
||||
'Symfony\Bundle\FrameworkBundle\DependencyInjectino\Compiler\SerializerPass' => 'Symfony\Component\Serializer\DependencyInjection\SerializerPass',
|
||||
// @todo: complete the rest
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
$form = new \Symfony\Component\Form\Form;
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->processForm($form);
|
||||
}
|
25
tests/Rector/Contrib/Symfony/FormIsValidRector/Test.php
Normal file
25
tests/Rector/Contrib/Symfony/FormIsValidRector/Test.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Contrib\Symfony\FormIsValidRector;
|
||||
|
||||
use Rector\Rector\Contrib\Symfony\FormIsValidRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class Test extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/Wrong/wrong.php.inc',
|
||||
__DIR__ . '/Correct/correct.php.inc'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getRectorClasses(): array
|
||||
{
|
||||
return [FormIsValidRector::class];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
$form = new \Symfony\Component\Form\Form;
|
||||
|
||||
if ($form->isValid()) {
|
||||
$this->processForm($form);
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
$containerBuilder = new \Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
$containerBuilder->addCompilerPass(new \Symfony\Component\Serializer\DependencyInjection\SerializerPass);
|
|
@ -0,0 +1,25 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Contrib\Symfony\FrameworkBundleClassReplacementsRector;
|
||||
|
||||
use Rector\Rector\Contrib\Symfony\FrameworkBundleClassReplacementsRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class Test extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/Wrong/wrong.php.inc',
|
||||
__DIR__ . '/Correct/correct.php.inc'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getRectorClasses(): array
|
||||
{
|
||||
return [FrameworkBundleClassReplacementsRector::class];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
$containerBuilder = new \Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
$containerBuilder->addCompilerPass(new Symfony\Bundle\FrameworkBundle\DependencyInjectino\Compiler\SerializerPass);
|
Loading…
Reference in New Issue
Block a user