mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
[Rector] add CompilerAddPassPriorityRector
This commit is contained in:
parent
16cc687066
commit
d7fc7dcb27
@ -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);
|
||||
}
|
||||
}
|
||||
|
75
src/Rector/Contrib/Symfony/CompilerAddPassPriorityRector.php
Normal file
75
src/Rector/Contrib/Symfony/CompilerAddPassPriorityRector.php
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -0,0 +1,4 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
$compiler = new \Symfony\Component\DependencyInjection\Compiler\Compiler();
|
||||
$compiler->addPass($somePass, 5, 0);
|
@ -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];
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
$compiler = new \Symfony\Component\DependencyInjection\Compiler\Compiler();
|
||||
$compiler->addPass($somePass, 5);
|
Loading…
x
Reference in New Issue
Block a user