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

[Symfony] framework bundle class replaces + form isValid() #16

Merged
merged 4 commits into from
Sep 1, 2017
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
5 changes: 5 additions & 0 deletions easy-coding-standard.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/NodeVisitor/NodeConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class NodeConnector extends NodeVisitorAbstract
/**
* @var Node
*/
private $stack;
private $stack = [];

/**
* @var Node
Expand Down
41 changes: 41 additions & 0 deletions src/Rector/AbstractClassReplacerRector.php
Original file line number Diff line number Diff line change
@@ -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 changes: 71 additions & 0 deletions src/Rector/Contrib/Symfony/FormIsValidRector.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare (strict_types=1);

$form = new \Symfony\Component\Form\Form;

if ($form->isSubmitted() && $form->isValid()) {
$this->processForm($form);
}
25 changes: 25 additions & 0 deletions tests/Rector/Contrib/Symfony/FormIsValidRector/Test.php
Original file line number Diff line number Diff line change
@@ -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];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare (strict_types=1);

$form = new \Symfony\Component\Form\Form;

if ($form->isValid()) {
$this->processForm($form);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php declare (strict_types=1);

$containerBuilder = new \Symfony\Component\DependencyInjection\ContainerBuilder;
$containerBuilder->addCompilerPass(new \Symfony\Component\Serializer\DependencyInjection\SerializerPass);
Original file line number Diff line number Diff line change
@@ -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];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php declare (strict_types=1);

$containerBuilder = new \Symfony\Component\DependencyInjection\ContainerBuilder;
$containerBuilder->addCompilerPass(new Symfony\Bundle\FrameworkBundle\DependencyInjectino\Compiler\SerializerPass);