[Phalcon 4.0] Add FlashWithCssClassesToExtraCallRector

This commit is contained in:
TomasVotruba 2019-12-15 23:00:59 +01:00
parent d337f6e6e7
commit 9f1b745592
6 changed files with 164 additions and 14 deletions

View File

@ -102,7 +102,8 @@
"Rector\\Utils\\RectorGenerator\\": "utils/RectorGenerator/src",
"Rector\\StrictCodeQuality\\": "packages/StrictCodeQuality/src",
"Rector\\DynamicTypeAnalysis\\": "packages/DynamicTypeAnalysis/src",
"Rector\\PhpDeglobalize\\": "packages/PhpDeglobalize/src"
"Rector\\PhpDeglobalize\\": "packages/PhpDeglobalize/src",
"Rector\\Phalcon\\": "packages/Phalcon/src"
}
},
"autoload-dev": {
@ -160,7 +161,8 @@
"Rector\\ZendToSymfony\\Tests\\": "packages/ZendToSymfony/tests",
"Rector\\StrictCodeQuality\\Tests\\": "packages/StrictCodeQuality/tests",
"Rector\\DynamicTypeAnalysis\\Tests\\": "packages/DynamicTypeAnalysis/tests",
"Rector\\PhpDeglobalize\\Tests\\": "packages/PhpDeglobalize/tests"
"Rector\\PhpDeglobalize\\Tests\\": "packages/PhpDeglobalize/tests",
"Rector\\Phalcon\\Tests\\": "packages/Phalcon/tests"
},
"classmap": [
"packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource",

View File

@ -114,3 +114,4 @@ services:
Rector\Renaming\Rector\ConstFetch\RenameConstantRector:
FILTER_SPECIAL_CHARS: 'FILTER_SPECIAL'
FILTER_ALPHANUM: 'FILTER_ALNUM'
Rector\Phalcon\Rector\Assign\FlashWithCssClassesToExtraCallRector: ~

View File

@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
namespace Rector\Phalcon\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://github.com/rectorphp/rector/issues/2408#issue-534441142
*
* @see \Rector\Phalcon\Tests\Rector\Assign\FlashWithCssClassesToExtraCallRector\FlashWithCssClassesToExtraCallRectorTest
*/
final class FlashWithCssClassesToExtraCallRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Add $cssClasses in Flash to separated method call', [
new CodeSample(
<<<'PHP'
class SomeClass {
public function run()
{
$cssClasses = [];
$flash = new Phalcon\Flash($cssClasses);
}
}
PHP
,
<<<'PHP'
class SomeClass {
public function run()
{
$cssClasses = [];
$flash = new Phalcon\Flash();
$flash->setCssClasses($cssClasses);
}
}
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Assign::class];
}
/**
* @param Assign $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof Node\Expr\New_) {
return null;
}
if (! $this->isName($node->expr->class, 'Phalcon\Flash')) {
return null;
}
if (! isset($node->expr->args[0])) {
return null;
}
$argument = $node->expr->args[0];
// remove arg
unset($node->expr->args[0]);
// change the node
$variable = $node->var;
$setCssClassesMethodCall = new Node\Expr\MethodCall($variable, 'setCssClasses', [$argument]);
$this->addNodeAfterNode($setCssClassesMethodCall, $node);
return $node;
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Rector\Phalcon\Tests\Rector\Assign\FlashWithCssClassesToExtraCallRector\Fixture;
class SomeClass {
public function run()
{
$cssClasses = [];
$flash = new \Phalcon\Flash($cssClasses);
}
}
?>
-----
<?php
namespace Rector\Phalcon\Tests\Rector\Assign\FlashWithCssClassesToExtraCallRector\Fixture;
class SomeClass {
public function run()
{
$cssClasses = [];
$flash = new \Phalcon\Flash();
$flash->setCssClasses($cssClasses);
}
}
?>

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Rector\Phalcon\Tests\Rector\Assign\FlashWithCssClassesToExtraCallRector;
use Iterator;
use Rector\Phalcon\Rector\Assign\FlashWithCssClassesToExtraCallRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class FlashWithCssClassesToExtraCallRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}
public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return FlashWithCssClassesToExtraCallRector::class;
}
}

View File

@ -19,23 +19,22 @@ parameters:
# @see utils/RectorGenerator/config/config.yaml
rector_recipe:
# run "bin/rector create" to create a new Rector + tests from this config
package: "Rector"
name: "SwapClassMethodArgumentsRector"
package: "Phalcon"
name: "FlashWithCssClassesToExtraCallRector"
node_types:
# put main node first, it is used to create namespace
- "StaticCall"
- "MethodCall"
- "ClassMethod"
- "Assign"
description: "Reorder class method arguments, including their calls"
description: "Add $cssClasses in Flash to separated method call"
code_before: >
<?php
class SomeClass
{
public static function run($first, $second)
public function run()
{
self::run($first, $second);
$cssClasses = [];
$flash = new Phalcon\Flash($cssClasses);
}
}
@ -44,12 +43,14 @@ parameters:
class SomeClass
{
public static function run($second, $first)
public function run()
{
self::run($second, $first);
$cssClasses = [];
$flash = new Phalcon\Flash();
$flash->setCssClasses($cssClasses);
}
}
source: # e.g. link to RFC or headline in upgrade guide, 1 or more in the list
- ""
set: "" # e.g. symfony30, target config to append this rector to
- "https://github.com/rectorphp/rector/issues/2408#issue-534441142"
set: "phalcon40" # e.g. symfony30, target config to append this rector to