From d7fc7dcb273ebe55a15b3aef61fe3c8566175507 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Wed, 20 Sep 2017 18:18:42 +0200 Subject: [PATCH] [Rector] add CompilerAddPassPriorityRector --- src/NodeFactory/NodeFactory.php | 40 ++++++---- .../Symfony/CompilerAddPassPriorityRector.php | 75 +++++++++++++++++++ ...ntainerBuilderCompileEnvArgumentRector.php | 19 +++-- src/config/level/symfony/symfony40.yml | 1 + .../Correct/correct.php.inc | 4 + .../CompileAddPassPriorityRector/Test.php | 25 +++++++ .../Wrong/wrong.php.inc | 4 + 7 files changed, 147 insertions(+), 21 deletions(-) create mode 100644 src/Rector/Contrib/Symfony/CompilerAddPassPriorityRector.php create mode 100644 tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Correct/correct.php.inc create mode 100644 tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Test.php create mode 100644 tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Wrong/wrong.php.inc diff --git a/src/NodeFactory/NodeFactory.php b/src/NodeFactory/NodeFactory.php index 98f88990ed6..bf55a392b65 100644 --- a/src/NodeFactory/NodeFactory.php +++ b/src/NodeFactory/NodeFactory.php @@ -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); + } } diff --git a/src/Rector/Contrib/Symfony/CompilerAddPassPriorityRector.php b/src/Rector/Contrib/Symfony/CompilerAddPassPriorityRector.php new file mode 100644 index 00000000000..b78c14f553b --- /dev/null +++ b/src/Rector/Contrib/Symfony/CompilerAddPassPriorityRector.php @@ -0,0 +1,75 @@ +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; + } +} diff --git a/src/Rector/Contrib/Symfony/ContainerBuilderCompileEnvArgumentRector.php b/src/Rector/Contrib/Symfony/ContainerBuilderCompileEnvArgumentRector.php index 35385a46173..971542b5dc6 100644 --- a/src/Rector/Contrib/Symfony/ContainerBuilderCompileEnvArgumentRector.php +++ b/src/Rector/Contrib/Symfony/ContainerBuilderCompileEnvArgumentRector.php @@ -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; diff --git a/src/config/level/symfony/symfony40.yml b/src/config/level/symfony/symfony40.yml index ae235a3c7e4..6f3ce451b0b 100644 --- a/src/config/level/symfony/symfony40.yml +++ b/src/config/level/symfony/symfony40.yml @@ -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 diff --git a/tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Correct/correct.php.inc b/tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Correct/correct.php.inc new file mode 100644 index 00000000000..b7850c30ad0 --- /dev/null +++ b/tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Correct/correct.php.inc @@ -0,0 +1,4 @@ +addPass($somePass, 5, 0); diff --git a/tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Test.php b/tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Test.php new file mode 100644 index 00000000000..d2fe1a2a4f7 --- /dev/null +++ b/tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Test.php @@ -0,0 +1,25 @@ +doTestFileMatchesExpectedContent( + __DIR__ . '/Wrong/wrong.php.inc', + __DIR__ . '/Correct/correct.php.inc' + ); + } + + /** + * @return string[] + */ + protected function getRectorClasses(): array + { + return [CompilerAddPassPriorityRector::class]; + } +} diff --git a/tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Wrong/wrong.php.inc b/tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Wrong/wrong.php.inc new file mode 100644 index 00000000000..d686ed50858 --- /dev/null +++ b/tests/Rector/Contrib/Symfony/CompileAddPassPriorityRector/Wrong/wrong.php.inc @@ -0,0 +1,4 @@ +addPass($somePass, 5);