From e08aea0139b8a664a4a96a57fbc7e9637ee6f685 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 19:48:43 +0100 Subject: [PATCH 01/12] add ArgumentReplacerItemRecipe, use typed config structure --- src/Rector/Dynamic/ArgumentReplacerRector.php | 66 ++++++----- .../ArgumentReplacerItemRecipe.php | 107 ++++++++++++++++++ .../ArgumentReplacerRector/config/rector.yml | 56 +++++---- 3 files changed, 179 insertions(+), 50 deletions(-) create mode 100644 src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php diff --git a/src/Rector/Dynamic/ArgumentReplacerRector.php b/src/Rector/Dynamic/ArgumentReplacerRector.php index e340b8a3236..fd220265d84 100644 --- a/src/Rector/Dynamic/ArgumentReplacerRector.php +++ b/src/Rector/Dynamic/ArgumentReplacerRector.php @@ -8,20 +8,20 @@ use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Identifier; use PhpParser\Node\Param; use PhpParser\Node\Stmt\ClassMethod; use Rector\NodeAnalyzer\ClassMethodAnalyzer; use Rector\NodeAnalyzer\MethodCallAnalyzer; use Rector\NodeAnalyzer\StaticMethodCallAnalyzer; use Rector\Rector\AbstractRector; +use Rector\Rector\Dynamic\Configuration\ArgumentReplacerItemRecipe; final class ArgumentReplacerRector extends AbstractRector { /** - * @var mixed[] + * @var ArgumentReplacerItemRecipe[] */ - private $argumentChangesMethodAndClass = []; + private $argumentReplacerItemRecipes = []; /** * @var MethodCallAnalyzer @@ -29,9 +29,9 @@ final class ArgumentReplacerRector extends AbstractRector private $methodCallAnalyzer; /** - * @var mixed[][] + * @var argumentReplacerItemRecipe[] */ - private $activeArgumentChangesByPosition = []; + private $activeArgumentReplacerItemRecipes = []; /** * @var ClassMethodAnalyzer @@ -58,7 +58,7 @@ final class ArgumentReplacerRector extends AbstractRector StaticMethodCallAnalyzer $staticMethodCallAnalyzer, ConstExprEvaluator $constExprEvaluator ) { - $this->argumentChangesMethodAndClass = $argumentChangesByMethodAndType; + $this->loadArgumentReplacerItemRecipes($argumentChangesByMethodAndType); $this->methodCallAnalyzer = $methodCallAnalyzer; $this->classMethodAnalyzer = $classMethodAnalyzer; $this->staticMethodCallAnalyzer = $staticMethodCallAnalyzer; @@ -67,9 +67,9 @@ final class ArgumentReplacerRector extends AbstractRector public function isCandidate(Node $node): bool { - $this->activeArgumentChangesByPosition = $this->matchArgumentChanges($node); + $this->activeArgumentReplacerItemRecipes = $this->matchArgumentChanges($node); - return (bool) $this->activeArgumentChangesByPosition; + return (bool) $this->activeArgumentReplacerItemRecipes; } /** @@ -79,25 +79,23 @@ final class ArgumentReplacerRector extends AbstractRector { $argumentsOrParameters = $this->getNodeArgumentsOrParameters($node); - foreach ($this->activeArgumentChangesByPosition as $position => $argumentChange) { - $key = key($argumentChange); - $value = array_shift($argumentChange); + foreach ($this->activeArgumentReplacerItemRecipes as $argumentReplacerItemRecipe) { + // @todo constants and own methods + if ($argumentReplacerItemRecipe->getType() === 'removed') { + unset($argumentsOrParameters[$argumentReplacerItemRecipe->getPosition()]); - if ($key === '~') { - if ($value === null) { // remove argument - unset($argumentsOrParameters[$position]); - } else { // new default value - $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue($value); - } - } else { - // replace old value with new one + } elseif ($argumentReplacerItemRecipe->getType() === 'changed') { + $argumentsOrParameters[$argumentReplacerItemRecipe->getPosition()] = BuilderHelpers::normalizeValue($argumentReplacerItemRecipe->getDefaultValue()); + } elseif ($argumentReplacerItemRecipe->getType() === 'replace_default_value') { /** @var Arg $argumentOrParameter */ - $argumentOrParameter = $argumentsOrParameters[$position]; - + $argumentOrParameter = $argumentsOrParameters[$argumentReplacerItemRecipe->getPosition()]; $resolvedValue = $this->constExprEvaluator->evaluateDirectly($argumentOrParameter->value); - if ($resolvedValue === $key) { - $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue($value); + $replaceMap = $argumentReplacerItemRecipe->getReplaceMap(); + foreach ($replaceMap as $oldValue => $newValue) { + if ($resolvedValue === $oldValue) { + $argumentsOrParameters[$argumentReplacerItemRecipe->getPosition()] = BuilderHelpers::normalizeValue($newValue); + } } } } @@ -108,7 +106,7 @@ final class ArgumentReplacerRector extends AbstractRector } /** - * @return mixed[][] + * @return ArgumentReplacerItemRecipe[] */ private function matchArgumentChanges(Node $node): array { @@ -116,17 +114,16 @@ final class ArgumentReplacerRector extends AbstractRector return []; } - foreach ($this->argumentChangesMethodAndClass as $type => $argumentChangesByMethod) { - $methods = array_keys($argumentChangesByMethod); - if ($this->isTypeAndMethods($node, $type, $methods)) { - /** @var Identifier $identifierNode */ - $identifierNode = $node->name; + $argumentReplacerItemRecipes = []; - return $argumentChangesByMethod[$identifierNode->toString()]; + foreach ($this->argumentReplacerItemRecipes as $argumentReplacerItemRecipe) { + + if ($this->isTypeAndMethods($node, $argumentReplacerItemRecipe->getClass(), [$argumentReplacerItemRecipe->getMethod()])) { + $argumentReplacerItemRecipes[] = $argumentReplacerItemRecipe; } } - return []; + return $argumentReplacerItemRecipes; } /** @@ -173,4 +170,11 @@ final class ArgumentReplacerRector extends AbstractRector return $this->classMethodAnalyzer->isTypeAndMethods($node, $type, $methods); } + + private function loadArgumentReplacerItemRecipes(array $configurationArrays): void + { + foreach ($configurationArrays as $configurationArray) { + $this->argumentReplacerItemRecipes[] = ArgumentReplacerItemRecipe::createFromArray($configurationArray); + } + } } diff --git a/src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php b/src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php new file mode 100644 index 00000000000..be1877a3958 --- /dev/null +++ b/src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php @@ -0,0 +1,107 @@ +class = $class; + $this->method = $method; + $this->position = $position; + $this->type = $type; + $this->defaultValue = $defaultValue; + $this->replaceMap = $replaceMap; + } + + public static function createFromArray(array $data): self + { + // @todo: make exceptions clear for end user + Assert::keyExists($data, 'class'); + Assert::keyExists($data, 'method'); + Assert::keyExists($data, 'position'); + Assert::keyExists($data, 'type'); + + if ($data['type'] === 'replace_default_value') { + Assert::keyExists($data, 'replace_map'); + } + + return new self( + $data['class'], + $data['method'], + $data['position'], + $data['type'], + $data['default_value'] ?? null, + $data['replace_map'] ?? [] + ); + } + + public function getClass(): string + { + return $this->class; + } + + public function getMethod(): string + { + return $this->method; + } + + public function getPosition(): int + { + return $this->position; + } + + public function getType(): string + { + return $this->type; + } + + /** + * @return mixed|null + */ + public function getDefaultValue() + { + return $this->defaultValue; + } + + /** + * @return string[] + */ + public function getReplaceMap(): array + { + return $this->replaceMap; + } +} diff --git a/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml b/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml index 657580e27be..6567eb6ff4e 100644 --- a/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml +++ b/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml @@ -1,23 +1,41 @@ rectors: Rector\Rector\Dynamic\ArgumentReplacerRector: - 'Symfony\Component\DependencyInjection\ContainerBuilder': - 'compile': - 0: - # added default value - '~': false - 'addCompilerPass': - 2: - # added default value - '~': 0 + # value object approach + - + class: 'Symfony\Component\DependencyInjection\ContainerBuilder' + method: 'addCompilerPass' + position: 2 + type: 'added' + default_value: 0 - 'Doctrine\ORM\Persisters\Entity\AbstractEntityInheritancePersister': - 'getSelectJoinColumnSQL': - 4: - # remove completely - '~': ~ + # added default value + - + class: 'Symfony\Component\DependencyInjection\ContainerBuilder' + method: 'compile' + type: 'changed' + position: 0 + default_value: false - 'Symfony\Component\DependencyInjection\Definition': - 'setScope': - 0: - # replace by new value - 'Symfony\Component\DependencyInjection\ContainerBuilder::SCOPE_PROTOTYPE': false + # added default value + - + class: 'Symfony\Component\DependencyInjection\ContainerBuilder' + method: 'addCompilerPass' + type: 'changed' + position: 0 + default_value: 0 + + # remove argument + - + class: 'Doctrine\ORM\Persisters\Entity\AbstractEntityInheritancePersister' + method: 'getSelectJoinColumnSQL' + position: 4 + type: 'remove' + + # replace default value + - + class: 'Symfony\Component\DependencyInjection\Definition' + method: 'setScope' + position: 0 + type: 'replace_default_value' + replace_map: + 'Symfony\Component\DependencyInjection\ContainerBuilder::SCOPE_PROTOTYPE': false From 25df68f9ee6ca15e56211d1447684ecae27a2b29 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 19:50:57 +0100 Subject: [PATCH 02/12] fix tests --- .../ArgumentReplacerRectorTest.php | 6 +++--- .../Dynamic/ArgumentReplacerRector/config/rector.yml | 12 ++---------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/Rector/Dynamic/ArgumentReplacerRector/ArgumentReplacerRectorTest.php b/tests/Rector/Dynamic/ArgumentReplacerRector/ArgumentReplacerRectorTest.php index ba7367b1005..110062196ee 100644 --- a/tests/Rector/Dynamic/ArgumentReplacerRector/ArgumentReplacerRectorTest.php +++ b/tests/Rector/Dynamic/ArgumentReplacerRector/ArgumentReplacerRectorTest.php @@ -21,9 +21,9 @@ final class ArgumentReplacerRectorTest extends AbstractConfigurableRectorTestCas public function provideWrongToFixedFiles(): array { return [ - [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'], - [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'], - [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'], +// [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'], +// [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'], +// [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'], [__DIR__ . '/Wrong/wrong4.php.inc', __DIR__ . '/Correct/correct4.php.inc'], ]; } diff --git a/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml b/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml index 6567eb6ff4e..aa6a069cf50 100644 --- a/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml +++ b/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml @@ -1,13 +1,5 @@ rectors: Rector\Rector\Dynamic\ArgumentReplacerRector: - # value object approach - - - class: 'Symfony\Component\DependencyInjection\ContainerBuilder' - method: 'addCompilerPass' - position: 2 - type: 'added' - default_value: 0 - # added default value - class: 'Symfony\Component\DependencyInjection\ContainerBuilder' @@ -20,8 +12,8 @@ rectors: - class: 'Symfony\Component\DependencyInjection\ContainerBuilder' method: 'addCompilerPass' + position: 2 type: 'changed' - position: 0 default_value: 0 # remove argument @@ -29,7 +21,7 @@ rectors: class: 'Doctrine\ORM\Persisters\Entity\AbstractEntityInheritancePersister' method: 'getSelectJoinColumnSQL' position: 4 - type: 'remove' + type: 'removed' # replace default value - From 1d76891f1427ad61c8e9a91a5f54657d40d87c6a Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 19:58:09 +0100 Subject: [PATCH 03/12] cleaning --- src/Rector/Dynamic/ArgumentReplacerRector.php | 59 ++++++++++++------- .../ArgumentReplacerItemRecipe.php | 13 +++- .../ArgumentReplacerRectorTest.php | 6 +- 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/Rector/Dynamic/ArgumentReplacerRector.php b/src/Rector/Dynamic/ArgumentReplacerRector.php index fd220265d84..d161840a1d6 100644 --- a/src/Rector/Dynamic/ArgumentReplacerRector.php +++ b/src/Rector/Dynamic/ArgumentReplacerRector.php @@ -18,10 +18,24 @@ use Rector\Rector\Dynamic\Configuration\ArgumentReplacerItemRecipe; final class ArgumentReplacerRector extends AbstractRector { + /** + * @var string + */ + private const TYPE_REMOVED = 'removed'; + + /** + * @var string + */ + private const TYPE_CHANGED = 'changed'; + + /** + * @var string + */ + private const TYPE_REPLACED_DEFAULT_VALUE = 'replace_default_value'; /** * @var ArgumentReplacerItemRecipe[] */ - private $argumentReplacerItemRecipes = []; + private $argumentReplacerRecipes = []; /** * @var MethodCallAnalyzer @@ -80,21 +94,28 @@ final class ArgumentReplacerRector extends AbstractRector $argumentsOrParameters = $this->getNodeArgumentsOrParameters($node); foreach ($this->activeArgumentReplacerItemRecipes as $argumentReplacerItemRecipe) { - // @todo constants and own methods - if ($argumentReplacerItemRecipe->getType() === 'removed') { - unset($argumentsOrParameters[$argumentReplacerItemRecipe->getPosition()]); + $type = $argumentReplacerItemRecipe->getType(); + $position = $argumentReplacerItemRecipe->getPosition(); - } elseif ($argumentReplacerItemRecipe->getType() === 'changed') { - $argumentsOrParameters[$argumentReplacerItemRecipe->getPosition()] = BuilderHelpers::normalizeValue($argumentReplacerItemRecipe->getDefaultValue()); - } elseif ($argumentReplacerItemRecipe->getType() === 'replace_default_value') { + if ($type === self::TYPE_REMOVED) { + unset($argumentsOrParameters[$position]); + continue; + } + + if ($type === self::TYPE_CHANGED) { + $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue($argumentReplacerItemRecipe->getDefaultValue()); + continue; + } + + if ($type === self::TYPE_REPLACED_DEFAULT_VALUE) { /** @var Arg $argumentOrParameter */ - $argumentOrParameter = $argumentsOrParameters[$argumentReplacerItemRecipe->getPosition()]; + $argumentOrParameter = $argumentsOrParameters[$position]; $resolvedValue = $this->constExprEvaluator->evaluateDirectly($argumentOrParameter->value); $replaceMap = $argumentReplacerItemRecipe->getReplaceMap(); foreach ($replaceMap as $oldValue => $newValue) { if ($resolvedValue === $oldValue) { - $argumentsOrParameters[$argumentReplacerItemRecipe->getPosition()] = BuilderHelpers::normalizeValue($newValue); + $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue($newValue); } } } @@ -116,10 +137,9 @@ final class ArgumentReplacerRector extends AbstractRector $argumentReplacerItemRecipes = []; - foreach ($this->argumentReplacerItemRecipes as $argumentReplacerItemRecipe) { - - if ($this->isTypeAndMethods($node, $argumentReplacerItemRecipe->getClass(), [$argumentReplacerItemRecipe->getMethod()])) { - $argumentReplacerItemRecipes[] = $argumentReplacerItemRecipe; + foreach ($this->argumentReplacerRecipes as $argumentReplacerRecipe) { + if ($this->isTypeAndMethod($node, $argumentReplacerRecipe->getClass(), $argumentReplacerRecipe->getMethod())) { + $argumentReplacerItemRecipes[] = $argumentReplacerRecipe; } } @@ -155,26 +175,23 @@ final class ArgumentReplacerRector extends AbstractRector } } - /** - * @param string[] $methods - */ - private function isTypeAndMethods(Node $node, string $type, array $methods): bool + private function isTypeAndMethod(Node $node, string $type, string $method): bool { - if ($this->methodCallAnalyzer->isTypeAndMethods($node, $type, $methods)) { + if ($this->methodCallAnalyzer->isTypeAndMethods($node, $type, [$method])) { return true; } - if ($this->staticMethodCallAnalyzer->isTypeAndMethods($node, $type, $methods)) { + if ($this->staticMethodCallAnalyzer->isTypeAndMethods($node, $type, [$method])) { return true; } - return $this->classMethodAnalyzer->isTypeAndMethods($node, $type, $methods); + return $this->classMethodAnalyzer->isTypeAndMethods($node, $type, [$method]); } private function loadArgumentReplacerItemRecipes(array $configurationArrays): void { foreach ($configurationArrays as $configurationArray) { - $this->argumentReplacerItemRecipes[] = ArgumentReplacerItemRecipe::createFromArray($configurationArray); + $this->argumentReplacerRecipes[] = ArgumentReplacerItemRecipe::createFromArray($configurationArray); } } } diff --git a/src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php b/src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php index be1877a3958..27654fbb20e 100644 --- a/src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php +++ b/src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php @@ -37,8 +37,14 @@ final class ArgumentReplacerItemRecipe * @param mixed $defaultValue * @param string[] $replaceMap */ - private function __construct(string $class, string $method, int $position, string $type, $defaultValue = null, array $replaceMap) - { + private function __construct( + string $class, + string $method, + int $position, + string $type, + $defaultValue = null, + array $replaceMap + ) { $this->class = $class; $this->method = $method; $this->position = $position; @@ -47,6 +53,9 @@ final class ArgumentReplacerItemRecipe $this->replaceMap = $replaceMap; } + /** + * @param mixed[] $data + */ public static function createFromArray(array $data): self { // @todo: make exceptions clear for end user diff --git a/tests/Rector/Dynamic/ArgumentReplacerRector/ArgumentReplacerRectorTest.php b/tests/Rector/Dynamic/ArgumentReplacerRector/ArgumentReplacerRectorTest.php index 110062196ee..ba7367b1005 100644 --- a/tests/Rector/Dynamic/ArgumentReplacerRector/ArgumentReplacerRectorTest.php +++ b/tests/Rector/Dynamic/ArgumentReplacerRector/ArgumentReplacerRectorTest.php @@ -21,9 +21,9 @@ final class ArgumentReplacerRectorTest extends AbstractConfigurableRectorTestCas public function provideWrongToFixedFiles(): array { return [ -// [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'], -// [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'], -// [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'], + [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'], + [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'], + [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'], [__DIR__ . '/Wrong/wrong4.php.inc', __DIR__ . '/Correct/correct4.php.inc'], ]; } From a270d174c98aebdfa65fbac2335f986f7fbdcb75 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 20:14:57 +0100 Subject: [PATCH 04/12] remove Item part from Recipe, move TYPE_* constants to Recipe --- src/Rector/Dynamic/ArgumentReplacerRector.php | 30 +++++-------------- ...mRecipe.php => ArgumentReplacerRecipe.php} | 17 ++++++++++- 2 files changed, 24 insertions(+), 23 deletions(-) rename src/Rector/Dynamic/Configuration/{ArgumentReplacerItemRecipe.php => ArgumentReplacerRecipe.php} (88%) diff --git a/src/Rector/Dynamic/ArgumentReplacerRector.php b/src/Rector/Dynamic/ArgumentReplacerRector.php index d161840a1d6..8cc69926a91 100644 --- a/src/Rector/Dynamic/ArgumentReplacerRector.php +++ b/src/Rector/Dynamic/ArgumentReplacerRector.php @@ -14,26 +14,12 @@ use Rector\NodeAnalyzer\ClassMethodAnalyzer; use Rector\NodeAnalyzer\MethodCallAnalyzer; use Rector\NodeAnalyzer\StaticMethodCallAnalyzer; use Rector\Rector\AbstractRector; -use Rector\Rector\Dynamic\Configuration\ArgumentReplacerItemRecipe; +use Rector\Rector\Dynamic\Configuration\ArgumentReplacerRecipe; final class ArgumentReplacerRector extends AbstractRector { /** - * @var string - */ - private const TYPE_REMOVED = 'removed'; - - /** - * @var string - */ - private const TYPE_CHANGED = 'changed'; - - /** - * @var string - */ - private const TYPE_REPLACED_DEFAULT_VALUE = 'replace_default_value'; - /** - * @var ArgumentReplacerItemRecipe[] + * @var ArgumentReplacerRecipe[] */ private $argumentReplacerRecipes = []; @@ -43,7 +29,7 @@ final class ArgumentReplacerRector extends AbstractRector private $methodCallAnalyzer; /** - * @var argumentReplacerItemRecipe[] + * @var ArgumentReplacerRecipe[] */ private $activeArgumentReplacerItemRecipes = []; @@ -97,17 +83,17 @@ final class ArgumentReplacerRector extends AbstractRector $type = $argumentReplacerItemRecipe->getType(); $position = $argumentReplacerItemRecipe->getPosition(); - if ($type === self::TYPE_REMOVED) { + if ($type === ArgumentReplacerRecipe::TYPE_REMOVED) { unset($argumentsOrParameters[$position]); continue; } - if ($type === self::TYPE_CHANGED) { + if ($type === ArgumentReplacerRecipe::TYPE_CHANGED) { $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue($argumentReplacerItemRecipe->getDefaultValue()); continue; } - if ($type === self::TYPE_REPLACED_DEFAULT_VALUE) { + if ($type === ArgumentReplacerRecipe::TYPE_REPLACED_DEFAULT_VALUE) { /** @var Arg $argumentOrParameter */ $argumentOrParameter = $argumentsOrParameters[$position]; $resolvedValue = $this->constExprEvaluator->evaluateDirectly($argumentOrParameter->value); @@ -127,7 +113,7 @@ final class ArgumentReplacerRector extends AbstractRector } /** - * @return ArgumentReplacerItemRecipe[] + * @return ArgumentReplacerRecipe[] */ private function matchArgumentChanges(Node $node): array { @@ -191,7 +177,7 @@ final class ArgumentReplacerRector extends AbstractRector private function loadArgumentReplacerItemRecipes(array $configurationArrays): void { foreach ($configurationArrays as $configurationArray) { - $this->argumentReplacerRecipes[] = ArgumentReplacerItemRecipe::createFromArray($configurationArray); + $this->argumentReplacerRecipes[] = ArgumentReplacerRecipe::createFromArray($configurationArray); } } } diff --git a/src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php similarity index 88% rename from src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php rename to src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php index 27654fbb20e..fb77d6039cd 100644 --- a/src/Rector/Dynamic/Configuration/ArgumentReplacerItemRecipe.php +++ b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php @@ -4,8 +4,23 @@ namespace Rector\Rector\Dynamic\Configuration; use Webmozart\Assert\Assert; -final class ArgumentReplacerItemRecipe +final class ArgumentReplacerRecipe { + /** + * @var string + */ + public const TYPE_REMOVED = 'removed'; + + /** + * @var string + */ + public const TYPE_CHANGED = 'changed'; + + /** + * @var string + */ + public const TYPE_REPLACED_DEFAULT_VALUE = 'replace_default_value'; + /** * @var string */ From 3bfad4e873461d1ab98c4d8f2ec9e4d8099edd57 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 20:25:40 +0100 Subject: [PATCH 05/12] fix cs --- src/Rector/Dynamic/ArgumentReplacerRector.php | 10 +++++-- .../Configuration/ArgumentReplacerRecipe.php | 27 +++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/Rector/Dynamic/ArgumentReplacerRector.php b/src/Rector/Dynamic/ArgumentReplacerRector.php index 8cc69926a91..3d4c9c99f20 100644 --- a/src/Rector/Dynamic/ArgumentReplacerRector.php +++ b/src/Rector/Dynamic/ArgumentReplacerRector.php @@ -124,7 +124,7 @@ final class ArgumentReplacerRector extends AbstractRector $argumentReplacerItemRecipes = []; foreach ($this->argumentReplacerRecipes as $argumentReplacerRecipe) { - if ($this->isTypeAndMethod($node, $argumentReplacerRecipe->getClass(), $argumentReplacerRecipe->getMethod())) { + if ($this->isRecipeMatch($node, $argumentReplacerRecipe)) { $argumentReplacerItemRecipes[] = $argumentReplacerRecipe; } } @@ -161,8 +161,11 @@ final class ArgumentReplacerRector extends AbstractRector } } - private function isTypeAndMethod(Node $node, string $type, string $method): bool + private function isRecipeMatch(Node $node, ArgumentReplacerRecipe $argumentReplacerRecipe): bool { + $type = $argumentReplacerRecipe->getClass(); + $method = $argumentReplacerRecipe->getMethod(); + if ($this->methodCallAnalyzer->isTypeAndMethods($node, $type, [$method])) { return true; } @@ -174,6 +177,9 @@ final class ArgumentReplacerRector extends AbstractRector return $this->classMethodAnalyzer->isTypeAndMethods($node, $type, [$method]); } + /** + * @param mixed[] $configurationArrays + */ private function loadArgumentReplacerItemRecipes(array $configurationArrays): void { foreach ($configurationArrays as $configurationArray) { diff --git a/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php index fb77d6039cd..369fe4ebbd2 100644 --- a/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php +++ b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php @@ -25,14 +25,17 @@ final class ArgumentReplacerRecipe * @var string */ private $class; + /** * @var string */ private $method; + /** * @var int */ private $position; + /** * @var string */ @@ -74,14 +77,7 @@ final class ArgumentReplacerRecipe public static function createFromArray(array $data): self { // @todo: make exceptions clear for end user - Assert::keyExists($data, 'class'); - Assert::keyExists($data, 'method'); - Assert::keyExists($data, 'position'); - Assert::keyExists($data, 'type'); - - if ($data['type'] === 'replace_default_value') { - Assert::keyExists($data, 'replace_map'); - } + self::validateArrayData($data); return new self( $data['class'], @@ -93,6 +89,21 @@ final class ArgumentReplacerRecipe ); } + /** + * @param mixed[] $data + */ + private static function validateArrayData(array $data): void + { + Assert::keyExists($data, 'class'); + Assert::keyExists($data, 'method'); + Assert::keyExists($data, 'position'); + Assert::keyExists($data, 'type'); + + if ($data['type'] === 'replace_default_value') { + Assert::keyExists($data, 'replace_map'); + } + } + public function getClass(): string { return $this->class; From 7eb603e12b1d54b5502edb3a9d0ed10372184243 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 20:26:32 +0100 Subject: [PATCH 06/12] fix cs --- src/Rector/Dynamic/ArgumentReplacerRector.php | 4 ++- .../Configuration/ArgumentReplacerRecipe.php | 30 +++++++++---------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Rector/Dynamic/ArgumentReplacerRector.php b/src/Rector/Dynamic/ArgumentReplacerRector.php index 3d4c9c99f20..cf933491da1 100644 --- a/src/Rector/Dynamic/ArgumentReplacerRector.php +++ b/src/Rector/Dynamic/ArgumentReplacerRector.php @@ -89,7 +89,9 @@ final class ArgumentReplacerRector extends AbstractRector } if ($type === ArgumentReplacerRecipe::TYPE_CHANGED) { - $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue($argumentReplacerItemRecipe->getDefaultValue()); + $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue( + $argumentReplacerItemRecipe->getDefaultValue() + ); continue; } diff --git a/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php index 369fe4ebbd2..d1f15ac99fd 100644 --- a/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php +++ b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php @@ -89,21 +89,6 @@ final class ArgumentReplacerRecipe ); } - /** - * @param mixed[] $data - */ - private static function validateArrayData(array $data): void - { - Assert::keyExists($data, 'class'); - Assert::keyExists($data, 'method'); - Assert::keyExists($data, 'position'); - Assert::keyExists($data, 'type'); - - if ($data['type'] === 'replace_default_value') { - Assert::keyExists($data, 'replace_map'); - } - } - public function getClass(): string { return $this->class; @@ -139,4 +124,19 @@ final class ArgumentReplacerRecipe { return $this->replaceMap; } + + /** + * @param mixed[] $data + */ + private static function validateArrayData(array $data): void + { + Assert::keyExists($data, 'class'); + Assert::keyExists($data, 'method'); + Assert::keyExists($data, 'position'); + Assert::keyExists($data, 'type'); + + if ($data['type'] === 'replace_default_value') { + Assert::keyExists($data, 'replace_map'); + } + } } From 3393dcc5fd53c6188077aa8dcc71db55c894d6a1 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 21:58:33 +0100 Subject: [PATCH 07/12] drop Item --- src/Rector/Dynamic/ArgumentReplacerRector.php | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Rector/Dynamic/ArgumentReplacerRector.php b/src/Rector/Dynamic/ArgumentReplacerRector.php index cf933491da1..e0e483b8a3d 100644 --- a/src/Rector/Dynamic/ArgumentReplacerRector.php +++ b/src/Rector/Dynamic/ArgumentReplacerRector.php @@ -31,7 +31,7 @@ final class ArgumentReplacerRector extends AbstractRector /** * @var ArgumentReplacerRecipe[] */ - private $activeArgumentReplacerItemRecipes = []; + private $activeArgumentReplacerRecipes = []; /** * @var ClassMethodAnalyzer @@ -58,7 +58,7 @@ final class ArgumentReplacerRector extends AbstractRector StaticMethodCallAnalyzer $staticMethodCallAnalyzer, ConstExprEvaluator $constExprEvaluator ) { - $this->loadArgumentReplacerItemRecipes($argumentChangesByMethodAndType); + $this->loadArgumentReplacerRecipes($argumentChangesByMethodAndType); $this->methodCallAnalyzer = $methodCallAnalyzer; $this->classMethodAnalyzer = $classMethodAnalyzer; $this->staticMethodCallAnalyzer = $staticMethodCallAnalyzer; @@ -67,9 +67,9 @@ final class ArgumentReplacerRector extends AbstractRector public function isCandidate(Node $node): bool { - $this->activeArgumentReplacerItemRecipes = $this->matchArgumentChanges($node); + $this->activeArgumentReplacerRecipes = $this->matchArgumentChanges($node); - return (bool) $this->activeArgumentReplacerItemRecipes; + return (bool) $this->activeArgumentReplacerRecipes; } /** @@ -79,9 +79,9 @@ final class ArgumentReplacerRector extends AbstractRector { $argumentsOrParameters = $this->getNodeArgumentsOrParameters($node); - foreach ($this->activeArgumentReplacerItemRecipes as $argumentReplacerItemRecipe) { - $type = $argumentReplacerItemRecipe->getType(); - $position = $argumentReplacerItemRecipe->getPosition(); + foreach ($this->activeArgumentReplacerRecipes as $argumentReplacerRecipe) { + $type = $argumentReplacerRecipe->getType(); + $position = $argumentReplacerRecipe->getPosition(); if ($type === ArgumentReplacerRecipe::TYPE_REMOVED) { unset($argumentsOrParameters[$position]); @@ -90,7 +90,7 @@ final class ArgumentReplacerRector extends AbstractRector if ($type === ArgumentReplacerRecipe::TYPE_CHANGED) { $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue( - $argumentReplacerItemRecipe->getDefaultValue() + $argumentReplacerRecipe->getDefaultValue() ); continue; } @@ -100,7 +100,7 @@ final class ArgumentReplacerRector extends AbstractRector $argumentOrParameter = $argumentsOrParameters[$position]; $resolvedValue = $this->constExprEvaluator->evaluateDirectly($argumentOrParameter->value); - $replaceMap = $argumentReplacerItemRecipe->getReplaceMap(); + $replaceMap = $argumentReplacerRecipe->getReplaceMap(); foreach ($replaceMap as $oldValue => $newValue) { if ($resolvedValue === $oldValue) { $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue($newValue); @@ -123,15 +123,15 @@ final class ArgumentReplacerRector extends AbstractRector return []; } - $argumentReplacerItemRecipes = []; + $argumentReplacerRecipes = []; foreach ($this->argumentReplacerRecipes as $argumentReplacerRecipe) { if ($this->isRecipeMatch($node, $argumentReplacerRecipe)) { - $argumentReplacerItemRecipes[] = $argumentReplacerRecipe; + $argumentReplacerRecipes[] = $argumentReplacerRecipe; } } - return $argumentReplacerItemRecipes; + return $argumentReplacerRecipes; } /** @@ -182,7 +182,7 @@ final class ArgumentReplacerRector extends AbstractRector /** * @param mixed[] $configurationArrays */ - private function loadArgumentReplacerItemRecipes(array $configurationArrays): void + private function loadArgumentReplacerRecipes(array $configurationArrays): void { foreach ($configurationArrays as $configurationArray) { $this->argumentReplacerRecipes[] = ArgumentReplacerRecipe::createFromArray($configurationArray); From 8894fe7f42f36826ef98df2f8151de7ac1e54a91 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 22:05:37 +0100 Subject: [PATCH 08/12] improve complexity --- src/Rector/Dynamic/ArgumentReplacerRector.php | 81 +++++++++++-------- .../Configuration/ArgumentReplacerRecipe.php | 2 +- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/src/Rector/Dynamic/ArgumentReplacerRector.php b/src/Rector/Dynamic/ArgumentReplacerRector.php index e0e483b8a3d..9ad241e6e66 100644 --- a/src/Rector/Dynamic/ArgumentReplacerRector.php +++ b/src/Rector/Dynamic/ArgumentReplacerRector.php @@ -78,37 +78,7 @@ final class ArgumentReplacerRector extends AbstractRector public function refactor(Node $node): ?Node { $argumentsOrParameters = $this->getNodeArgumentsOrParameters($node); - - foreach ($this->activeArgumentReplacerRecipes as $argumentReplacerRecipe) { - $type = $argumentReplacerRecipe->getType(); - $position = $argumentReplacerRecipe->getPosition(); - - if ($type === ArgumentReplacerRecipe::TYPE_REMOVED) { - unset($argumentsOrParameters[$position]); - continue; - } - - if ($type === ArgumentReplacerRecipe::TYPE_CHANGED) { - $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue( - $argumentReplacerRecipe->getDefaultValue() - ); - continue; - } - - if ($type === ArgumentReplacerRecipe::TYPE_REPLACED_DEFAULT_VALUE) { - /** @var Arg $argumentOrParameter */ - $argumentOrParameter = $argumentsOrParameters[$position]; - $resolvedValue = $this->constExprEvaluator->evaluateDirectly($argumentOrParameter->value); - - $replaceMap = $argumentReplacerRecipe->getReplaceMap(); - foreach ($replaceMap as $oldValue => $newValue) { - if ($resolvedValue === $oldValue) { - $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue($newValue); - } - } - } - } - + $argumentsOrParameters = $this->processArgumentsOrParamters($argumentsOrParameters); $this->setNodeArgumentsOrParameters($node, $argumentsOrParameters); return $node; @@ -126,7 +96,7 @@ final class ArgumentReplacerRector extends AbstractRector $argumentReplacerRecipes = []; foreach ($this->argumentReplacerRecipes as $argumentReplacerRecipe) { - if ($this->isRecipeMatch($node, $argumentReplacerRecipe)) { + if ($this->isNodeToRecipeMatch($node, $argumentReplacerRecipe)) { $argumentReplacerRecipes[] = $argumentReplacerRecipe; } } @@ -163,7 +133,7 @@ final class ArgumentReplacerRector extends AbstractRector } } - private function isRecipeMatch(Node $node, ArgumentReplacerRecipe $argumentReplacerRecipe): bool + private function isNodeToRecipeMatch(Node $node, ArgumentReplacerRecipe $argumentReplacerRecipe): bool { $type = $argumentReplacerRecipe->getClass(); $method = $argumentReplacerRecipe->getMethod(); @@ -188,4 +158,49 @@ final class ArgumentReplacerRector extends AbstractRector $this->argumentReplacerRecipes[] = ArgumentReplacerRecipe::createFromArray($configurationArray); } } + + /** + * @param mixed[] $argumentsOrParameters + * @return mixed[] + */ + private function processArgumentsOrParamters(array $argumentsOrParameters): array + { + foreach ($this->activeArgumentReplacerRecipes as $argumentReplacerRecipe) { + $type = $argumentReplacerRecipe->getType(); + $position = $argumentReplacerRecipe->getPosition(); + + if ($type === ArgumentReplacerRecipe::TYPE_REMOVED) { + unset($argumentsOrParameters[$position]); + } elseif ($type === ArgumentReplacerRecipe::TYPE_CHANGED) { + $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue( + $argumentReplacerRecipe->getDefaultValue() + ); + } elseif ($type === ArgumentReplacerRecipe::TYPE_REPLACED_DEFAULT_VALUE) { + $argumentsOrParameters[$position] = $this->processReplacedDefaultValue( + $argumentsOrParameters[$position], + $argumentReplacerRecipe + ); + } + } + + return $argumentsOrParameters; + } + + /** + * @param Arg|Param $argumentOrParameter + * @return Arg|Param + */ + private function processReplacedDefaultValue($argumentOrParameter, ArgumentReplacerRecipe $argumentReplacerRecipe) + { + $resolvedValue = $this->constExprEvaluator->evaluateDirectly($argumentOrParameter->value); + + $replaceMap = $argumentReplacerRecipe->getReplaceMap(); + foreach ($replaceMap as $oldValue => $newValue) { + if ($resolvedValue === $oldValue) { + return new Arg(BuilderHelpers::normalizeValue($newValue)); + } + } + + return $argumentOrParameter; + } } diff --git a/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php index d1f15ac99fd..fcc3469e229 100644 --- a/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php +++ b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php @@ -135,7 +135,7 @@ final class ArgumentReplacerRecipe Assert::keyExists($data, 'position'); Assert::keyExists($data, 'type'); - if ($data['type'] === 'replace_default_value') { + if ($data['type'] === self::TYPE_REPLACED_DEFAULT_VALUE) { Assert::keyExists($data, 'replace_map'); } } From c9b31dba64c0a777e67591584eb66921dc26c36b Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 22:16:05 +0100 Subject: [PATCH 09/12] update current Contrib configs --- .../Configuration/ArgumentReplacerRecipe.php | 28 ++++++++++++---- src/config/level/doctrine/dotrine25.yml | 9 ++--- src/config/level/symfony/symfony33.yml | 33 +++++++++++-------- .../ArgumentReplacerRector/config/rector.yml | 2 +- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php index fcc3469e229..cfe81d32472 100644 --- a/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php +++ b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php @@ -2,7 +2,8 @@ namespace Rector\Rector\Dynamic\Configuration; -use Webmozart\Assert\Assert; +use Rector\Exception\Rector\InvalidRectorConfigurationException; +use Rector\Rector\Dynamic\ArgumentReplacerRector; final class ArgumentReplacerRecipe { @@ -76,7 +77,6 @@ final class ArgumentReplacerRecipe */ public static function createFromArray(array $data): self { - // @todo: make exceptions clear for end user self::validateArrayData($data); return new self( @@ -89,6 +89,19 @@ final class ArgumentReplacerRecipe ); } + private static function ensureHasKey(array $data, string $key): void + { + if (isset($data[$key])) { + return; + } + + throw new InvalidRectorConfigurationException(sprintf( + 'Configuration for "%s" Rector should have "%s" key, but is missing.', + ArgumentReplacerRector::class, + $key + )); + } + public function getClass(): string { return $this->class; @@ -130,13 +143,14 @@ final class ArgumentReplacerRecipe */ private static function validateArrayData(array $data): void { - Assert::keyExists($data, 'class'); - Assert::keyExists($data, 'method'); - Assert::keyExists($data, 'position'); - Assert::keyExists($data, 'type'); + self::ensureHasKey($data, 'class'); + self::ensureHasKey($data, 'class'); + self::ensureHasKey($data, 'method'); + self::ensureHasKey($data, 'position'); + self::ensureHasKey($data, 'type'); if ($data['type'] === self::TYPE_REPLACED_DEFAULT_VALUE) { - Assert::keyExists($data, 'replace_map'); + self::ensureHasKey($data, 'replace_map'); } } } diff --git a/src/config/level/doctrine/dotrine25.yml b/src/config/level/doctrine/dotrine25.yml index 2df1c6e1561..e16fdf3490e 100644 --- a/src/config/level/doctrine/dotrine25.yml +++ b/src/config/level/doctrine/dotrine25.yml @@ -8,7 +8,8 @@ rectors: 'em': 'Doctrine\ORM\EntityManagerInterface' Rector\Rector\Dynamic\ArgumentReplacerRector: - 'Doctrine\ORM\Persisters\Entity\AbstractEntityInheritancePersister': - 'getSelectJoinColumnSQL': - 4: - '~': ~ + - + class: 'Doctrine\ORM\Persisters\Entity\AbstractEntityInheritancePersister' + method: 'getSelectJoinColumnSQL' + position: 4 + type: 'removed' diff --git a/src/config/level/symfony/symfony33.yml b/src/config/level/symfony/symfony33.yml index eb414d7c71d..d73e0f4a60e 100644 --- a/src/config/level/symfony/symfony33.yml +++ b/src/config/level/symfony/symfony33.yml @@ -1,20 +1,25 @@ rectors: # dependency-injection Rector\Rector\Dynamic\ArgumentReplacerRector: - 'Symfony\Component\DependencyInjection\ContainerBuilder': - 'compile': - 0: - # added default value - '~': false - 'addCompilerPass': - 2: - # added default value - '~': 0 - 'Symfony\Component\DependencyInjection\Compiler\ServiceReferenceGraph': - 'connect': - 6: - # added default value - '~': false + # added default value + - + class: 'Symfony\Component\DependencyInjection\ContainerBuilder' + method: 'compile' + type: 'changed' + position: 2 + default_value: 0 + - + class: 'Symfony\Component\DependencyInjection\ContainerBuilder' + method: 'addCompilerPass' + type: 'changed' + position: 2 + default_value: 0 + - + class: 'Symfony\Component\DependencyInjection\Compiler\ServiceReferenceGraph' + method: 'connect' + position: 6 + type: 'changed' + default_value: false Rector\Rector\Contrib\Symfony\Console\ConsoleExceptionToErrorEventConstantRector: ~ diff --git a/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml b/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml index aa6a069cf50..6a253c66e83 100644 --- a/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml +++ b/tests/Rector/Dynamic/ArgumentReplacerRector/config/rector.yml @@ -12,8 +12,8 @@ rectors: - class: 'Symfony\Component\DependencyInjection\ContainerBuilder' method: 'addCompilerPass' - position: 2 type: 'changed' + position: 2 default_value: 0 # remove argument From 5ec341e68ff91eb1b644157b183e476550f86294 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 22:21:27 +0100 Subject: [PATCH 10/12] we know it is Arg --- src/Rector/Dynamic/ArgumentReplacerRector.php | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/Rector/Dynamic/ArgumentReplacerRector.php b/src/Rector/Dynamic/ArgumentReplacerRector.php index 9ad241e6e66..5845d1483ab 100644 --- a/src/Rector/Dynamic/ArgumentReplacerRector.php +++ b/src/Rector/Dynamic/ArgumentReplacerRector.php @@ -75,10 +75,11 @@ final class ArgumentReplacerRector extends AbstractRector /** * @param MethodCall|StaticCall|ClassMethod $node */ - public function refactor(Node $node): ?Node + public function refactor(Node $node): Node { $argumentsOrParameters = $this->getNodeArgumentsOrParameters($node); - $argumentsOrParameters = $this->processArgumentsOrParamters($argumentsOrParameters); + $argumentsOrParameters = $this->processArgumentNodes($argumentsOrParameters); + $this->setNodeArgumentsOrParameters($node, $argumentsOrParameters); return $node; @@ -160,39 +161,35 @@ final class ArgumentReplacerRector extends AbstractRector } /** - * @param mixed[] $argumentsOrParameters + * @param mixed[] $argumentNodes * @return mixed[] */ - private function processArgumentsOrParamters(array $argumentsOrParameters): array + private function processArgumentNodes(array $argumentNodes): array { foreach ($this->activeArgumentReplacerRecipes as $argumentReplacerRecipe) { $type = $argumentReplacerRecipe->getType(); $position = $argumentReplacerRecipe->getPosition(); if ($type === ArgumentReplacerRecipe::TYPE_REMOVED) { - unset($argumentsOrParameters[$position]); + unset($argumentNodes[$position]); } elseif ($type === ArgumentReplacerRecipe::TYPE_CHANGED) { - $argumentsOrParameters[$position] = BuilderHelpers::normalizeValue( + $argumentNodes[$position] = BuilderHelpers::normalizeValue( $argumentReplacerRecipe->getDefaultValue() ); } elseif ($type === ArgumentReplacerRecipe::TYPE_REPLACED_DEFAULT_VALUE) { - $argumentsOrParameters[$position] = $this->processReplacedDefaultValue( - $argumentsOrParameters[$position], + $argumentNodes[$position] = $this->processReplacedDefaultValue( + $argumentNodes[$position], $argumentReplacerRecipe ); } } - return $argumentsOrParameters; + return $argumentNodes; } - /** - * @param Arg|Param $argumentOrParameter - * @return Arg|Param - */ - private function processReplacedDefaultValue($argumentOrParameter, ArgumentReplacerRecipe $argumentReplacerRecipe) + private function processReplacedDefaultValue(Arg $argNode, ArgumentReplacerRecipe $argumentReplacerRecipe): Arg { - $resolvedValue = $this->constExprEvaluator->evaluateDirectly($argumentOrParameter->value); + $resolvedValue = $this->constExprEvaluator->evaluateDirectly($argNode->value); $replaceMap = $argumentReplacerRecipe->getReplaceMap(); foreach ($replaceMap as $oldValue => $newValue) { @@ -201,6 +198,6 @@ final class ArgumentReplacerRector extends AbstractRector } } - return $argumentOrParameter; + return $argNode; } } From a80d12587794925ebfd432918ea87b6ea7f84267 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 22:24:34 +0100 Subject: [PATCH 11/12] fix cs --- .../Configuration/ArgumentReplacerRecipe.php | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php index cfe81d32472..0e6a3199328 100644 --- a/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php +++ b/src/Rector/Dynamic/Configuration/ArgumentReplacerRecipe.php @@ -89,19 +89,6 @@ final class ArgumentReplacerRecipe ); } - private static function ensureHasKey(array $data, string $key): void - { - if (isset($data[$key])) { - return; - } - - throw new InvalidRectorConfigurationException(sprintf( - 'Configuration for "%s" Rector should have "%s" key, but is missing.', - ArgumentReplacerRector::class, - $key - )); - } - public function getClass(): string { return $this->class; @@ -138,6 +125,22 @@ final class ArgumentReplacerRecipe return $this->replaceMap; } + /** + * @param mixed[] $data + */ + private static function ensureHasKey(array $data, string $key): void + { + if (isset($data[$key])) { + return; + } + + throw new InvalidRectorConfigurationException(sprintf( + 'Configuration for "%s" Rector should have "%s" key, but is missing.', + ArgumentReplacerRector::class, + $key + )); + } + /** * @param mixed[] $data */ From 0967ec60527711748d38bda71aee728b94543a37 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 13 Feb 2018 22:28:08 +0100 Subject: [PATCH 12/12] update README --- README.md | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b2593d09150..47423d4facc 100644 --- a/README.md +++ b/README.md @@ -208,23 +208,26 @@ rectors: 'code': 'string' ``` -### Change a argument value +### Change argument value or remove argument ```yml rectors: Rector\Rector\Dynamic\ArgumentReplacerRector: - # class - 'Symfony\Component\DependencyInjection\ContainerBuilder': - # method - 'compile': - # argument position - 0: - # added default value - '~': false - # or remove completely - '~': ~ - # or replace by new value - 'Symfony\Component\DependencyInjection\ContainerBuilder\ContainerBuilder::SCOPE_PROTOTYPE': false + - + class: 'Symfony\Component\DependencyInjection\ContainerBuilder' + method: 'compile' + position: 0 + # change default value + type: 'changed' + default_value: false + + # or remove + type: 'removed' + + # or replace old default value by new one + type: 'replace_default_value' + replace_map: + 'Symfony\Component\DependencyInjection\ContainerBuilder::SCOPE_PROTOTYPE': false ``` ### Replace the underscore naming `_` with namespaces `\`