mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 17:00:51 +00:00
[Decomplex] Add UseMessageVariableForSprintfInSymfonyStyleRector
This commit is contained in:
parent
ede17b8f23
commit
05c10af920
|
@ -1,29 +1,14 @@
|
|||
{
|
||||
"name": "rector/rector",
|
||||
"description": "Instant upgrade and refactoring of your PHP code",
|
||||
"keywords": [
|
||||
"instant upgrades",
|
||||
"instant refactoring",
|
||||
"ast",
|
||||
"automated refactoring"
|
||||
],
|
||||
"keywords": ["instant upgrades", "instant refactoring", "ast", "automated refactoring"],
|
||||
"homepage": "https://getrector.org",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Tomas Votruba",
|
||||
"email": "tomas.vot@gmail.com",
|
||||
"homepage": "https://tomasvotruba.com"
|
||||
},
|
||||
{
|
||||
"name": "Jan Mikes",
|
||||
"email": "j.mikes@me.com",
|
||||
"homepage": "https://janmikes.cz"
|
||||
}
|
||||
],
|
||||
"bin": [
|
||||
"bin/rector"
|
||||
{ "name": "Tomas Votruba", "email": "tomas.vot@gmail.com", "homepage": "https://tomasvotruba.com" },
|
||||
{ "name": "Jan Mikes", "email": "j.mikes@me.com", "homepage": "https://janmikes.cz" }
|
||||
],
|
||||
"bin": ["bin/rector"],
|
||||
"require": {
|
||||
"php": "^7.2.4",
|
||||
"ext-json": "*",
|
||||
|
@ -153,7 +138,8 @@
|
|||
"Rector\\MockistaToMockery\\": "rules/mockista-to-mockery/src",
|
||||
"Rector\\NetteKdyby\\": "rules/nette-kdyby/src",
|
||||
"Rector\\NetteUtilsCodeQuality\\": "rules/nette-utils-code-quality/src",
|
||||
"Rector\\NetteCodeQuality\\": "rules/nette-code-quality/src"
|
||||
"Rector\\NetteCodeQuality\\": "rules/nette-code-quality/src",
|
||||
"Rector\\Decomplex\\": "rules/decomplex/src"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
|
@ -229,7 +215,8 @@
|
|||
"Rector\\Compiler\\Tests\\": "compiler/tests",
|
||||
"Rector\\NetteKdyby\\Tests\\": "rules/nette-kdyby/tests",
|
||||
"Rector\\NetteUtilsCodeQuality\\Tests\\": "rules/nette-utils-code-quality/tests",
|
||||
"Rector\\NetteCodeQuality\\Tests\\": "rules/nette-code-quality/tests"
|
||||
"Rector\\NetteCodeQuality\\Tests\\": "rules/nette-code-quality/tests",
|
||||
"Rector\\Decomplex\\Tests\\": "rules/decomplex/tests"
|
||||
},
|
||||
"classmap": [
|
||||
"rules/cakephp/tests/Rector/Name/ImplicitShortClassNameUseStatementRector/Source",
|
||||
|
|
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Decomplex\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
/**
|
||||
* @see \Rector\Decomplex\Tests\Rector\MethodCall\UseMessageVariableForSprintfInSymfonyStyleRector\UseMessageVariableForSprintfInSymfonyStyleRectorTest
|
||||
*/
|
||||
final class UseMessageVariableForSprintfInSymfonyStyleRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition(
|
||||
'Decouple $message property from sprintf() calls in $this->smyfonyStyle->method()',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(SymfonyStyle $symfonyStyle)
|
||||
{
|
||||
$symfonyStyle->info(sprintf('Hi %s', 'Tom'));
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(SymfonyStyle $symfonyStyle)
|
||||
{
|
||||
$message = sprintf('Hi %s', 'Tom');
|
||||
$symfonyStyle->info($message);
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [MethodCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isObjectType($node, SymfonyStyle::class)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! isset($node->args[0])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$argValue = $node->args[0]->value;
|
||||
if (! $this->isFuncCallName($argValue, 'sprintf')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$messageVariable = new Variable('message');
|
||||
$assign = new Assign($messageVariable, $argValue);
|
||||
$this->addNodeBeforeNode($assign, $node);
|
||||
|
||||
$node->args[0]->value = $messageVariable;
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Decomplex\Tests\Rector\MethodCall\UseMessageVariableForSprintfInSymfonyStyleRector\Fixture;
|
||||
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(SymfonyStyle $symfonyStyle)
|
||||
{
|
||||
$symfonyStyle->info(sprintf('Hi %s', 'Tom'));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Decomplex\Tests\Rector\MethodCall\UseMessageVariableForSprintfInSymfonyStyleRector\Fixture;
|
||||
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(SymfonyStyle $symfonyStyle)
|
||||
{
|
||||
$message = sprintf('Hi %s', 'Tom');
|
||||
$symfonyStyle->info($message);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Decomplex\Tests\Rector\MethodCall\UseMessageVariableForSprintfInSymfonyStyleRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Decomplex\Rector\MethodCall\UseMessageVariableForSprintfInSymfonyStyleRector;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class UseMessageVariableForSprintfInSymfonyStyleRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return UseMessageVariableForSprintfInSymfonyStyleRector::class;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user