[Rector] add CompilerAddPassPriorityRector

This commit is contained in:
TomasVotruba 2017-09-20 18:18:42 +02:00
parent 16cc687066
commit d7fc7dcb27
7 changed files with 147 additions and 21 deletions

View File

@ -163,27 +163,41 @@ final class NodeFactory
}
/**
* @param mixed $argument
* @param mixed $value
*/
private function createTypeFromScalar($argument): Expr
private function createTypeFromScalar($value): Expr
{
if (is_string($argument)) {
$argument = new String_($argument);
} elseif (is_bool($argument)) {
$argument = $this->createInternalConstant($argument === true ? 'true' : 'false');
} else {
throw new NotImplementedException(sprintf(
'Not implemented yet. Go to "%s()" and add check for "%s".',
__METHOD__,
(is_string($argument) && class_exists($argument)) ? get_class($argument) : $argument
));
if (is_numeric($value)) {
return new Node\Scalar\LNumber($value);
}
return $argument;
if (is_string($value)) {
return new String_($value);
}
if (is_bool($value)) {
$value = $this->createInternalConstant($value === true ? 'true' : 'false');
}
throw new NotImplementedException(sprintf(
'Not implemented yet. Go to "%s()" and add check for "%s".',
__METHOD__,
(is_string($value) && class_exists($value)) ? get_class($value) : $value
));
}
private function createInternalConstant(string $value): ConstFetch
{
return new ConstFetch(new Name($value));
}
/**
* @param mixed $argument
*/
public function createArg($argument): Arg
{
$value = $this->createTypeFromScalar($argument);
return new Arg($value);
}
}

View File

@ -0,0 +1,75 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
/**
* Covers @see \Symfony\Component\DependencyInjection\Compiler\Compiler::addPass()
*
* Before:
* - add($pass)
* - add($pass, $type)
* - add($pass, $type, 0)
*
* After:
* ?
*/
final class CompilerAddPassPriorityRector extends AbstractRector
{
/**
* @var MethodCallAnalyzer
*/
private $methodCallAnalyzer;
/**
* @var NodeFactory
*/
private $nodeFactory;
public function __construct(MethodCallAnalyzer $methodCallAnalyzer, NodeFactory $nodeFactory)
{
$this->methodCallAnalyzer = $methodCallAnalyzer;
$this->nodeFactory = $nodeFactory;
}
public function isCandidate(Node $node): bool
{
if ($this->methodCallAnalyzer->isMethodCallTypeAndMethods(
$node,
'Symfony\Component\DependencyInjection\Compiler\Compiler',
['addPass']
) === false) {
return false;
}
/** @var MethodCall $node */
$args = $node->args;
// has 3 arguments, all is done
return count($args) !== 3;
}
/**
* @param MethodCall $methodCallNode
* @return null|Node
*/
public function refactor(Node $methodCallNode): ?Node
{
$arguments = $methodCallNode->args;
if (count($arguments) === 2) {
// add 3rd one
$arguments[] = $this->nodeFactory->createArg(0);
$methodCallNode->args = $arguments;
}
return $methodCallNode;
}
}

View File

@ -27,11 +27,21 @@ final class ContainerBuilderCompileEnvArgumentRector extends AbstractRector
public function isCandidate(Node $node): bool
{
return $this->methodCallAnalyzer->isMethodCallTypeAndMethods(
$isMethodCall = $this->methodCallAnalyzer->isMethodCallTypeAndMethods(
$node,
'Symfony\Component\DependencyInjection\ContainerBuilder',
['compile']
);
if ($isMethodCall === false) {
return false;
}
/** @var Node\Expr\MethodCall $node */
$arguments = $node->args;
// already has an argument
return count($arguments) !== 1;
}
/**
@ -39,13 +49,6 @@ final class ContainerBuilderCompileEnvArgumentRector extends AbstractRector
*/
public function refactor(Node $methodCallNode): ?Node
{
$arguments = $methodCallNode->args;
// already has an argument
if (count($arguments) === 1) {
return null;
}
$methodCallNode->args = $this->nodeFactory->createArgs([true]);
return $methodCallNode;

View File

@ -6,3 +6,4 @@ rectors:
- Rector\Rector\Contrib\Symfony\StringFormTypeToClassRector
- Rector\Rector\Contrib\Symfony\VarDumperTestTraitMethodArgsRector
- Rector\Rector\Contrib\Symfony\ContainerBuilderCompileEnvArgumentRector
- Rector\Rector\Contrib\Symfony\CompilerAddPassPriorityRector

View File

@ -0,0 +1,4 @@
<?php declare(strict_types=1);
$compiler = new \Symfony\Component\DependencyInjection\Compiler\Compiler();
$compiler->addPass($somePass, 5, 0);

View File

@ -0,0 +1,25 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Contrib\Symfony\CompileAddPassPriorityRector;
use Rector\Rector\Contrib\Symfony\CompilerAddPassPriorityRector;
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 [CompilerAddPassPriorityRector::class];
}
}

View File

@ -0,0 +1,4 @@
<?php declare(strict_types=1);
$compiler = new \Symfony\Component\DependencyInjection\Compiler\Compiler();
$compiler->addPass($somePass, 5);