Merge pull request #16 from TomasVotruba/symfony-class-replaces

[Symfony] framework bundle class replaces + form isValid()
This commit is contained in:
Tomáš Votruba 2017-09-01 21:29:05 +02:00 committed by GitHub
commit 387a721b1e
11 changed files with 225 additions and 1 deletions

View File

@ -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

View File

@ -13,7 +13,7 @@ final class NodeConnector extends NodeVisitorAbstract
/**
* @var Node
*/
private $stack;
private $stack = [];
/**
* @var Node

View 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];
}
}

View 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);
}
}

View File

@ -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
];
}
}

View File

@ -0,0 +1,7 @@
<?php declare (strict_types=1);
$form = new \Symfony\Component\Form\Form;
if ($form->isSubmitted() && $form->isValid()) {
$this->processForm($form);
}

View 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];
}
}

View File

@ -0,0 +1,7 @@
<?php declare (strict_types=1);
$form = new \Symfony\Component\Form\Form;
if ($form->isValid()) {
$this->processForm($form);
}

View File

@ -0,0 +1,4 @@
<?php declare (strict_types=1);
$containerBuilder = new \Symfony\Component\DependencyInjection\ContainerBuilder;
$containerBuilder->addCompilerPass(new \Symfony\Component\Serializer\DependencyInjection\SerializerPass);

View File

@ -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];
}
}

View File

@ -0,0 +1,4 @@
<?php declare (strict_types=1);
$containerBuilder = new \Symfony\Component\DependencyInjection\ContainerBuilder;
$containerBuilder->addCompilerPass(new Symfony\Bundle\FrameworkBundle\DependencyInjectino\Compiler\SerializerPass);