From 710e215c9c7956202a5a38b34c6afdf17e897dfa Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 6 Jun 2018 11:59:15 +0200 Subject: [PATCH] make RemoveConfiguratorConstantsRector generic --- .../RemoveConfiguratorConstantsRector.php | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/packages/Nette/src/Rector/Bootstrap/RemoveConfiguratorConstantsRector.php b/packages/Nette/src/Rector/Bootstrap/RemoveConfiguratorConstantsRector.php index c735493520f..1853b93b904 100644 --- a/packages/Nette/src/Rector/Bootstrap/RemoveConfiguratorConstantsRector.php +++ b/packages/Nette/src/Rector/Bootstrap/RemoveConfiguratorConstantsRector.php @@ -20,14 +20,40 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector */ private $nodeFactory; - public function __construct(NodeFactory $nodeFactory) - { + /** + * @var string + */ + private $oldClassConstant; + + /** + * @var string + */ + private $class; + + /** + * @var string[] + */ + private $oldConstantToNewValue = []; + + /** + * @param string[] $oldConstantToNewValue + */ + public function __construct( + NodeFactory $nodeFactory, + string $class = 'Nette\Configurator', + array $oldConstantToNewValue = [ + 'DEVELOPMENT' => 'development', + 'PRODUCTION' => 'production' + ] + ) { $this->nodeFactory = $nodeFactory; + $this->class = $class; + $this->oldConstantToNewValue = $oldConstantToNewValue; } public function getDefinition(): RectorDefinition { - return new RectorDefinition('Turns properties with @inject to private properties and constructor injection', [ + return new RectorDefinition('Replaces constant by value', [ new CodeSample('$value === Nette\Configurator::DEVELOPMENT', '$value === "development"'), ]); } @@ -40,11 +66,11 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector $className = $this->getClassNameFromClassConstFetch($node); - if ($className !== 'Nette\Configurator') { + if ($className !== $this->class) { return false; } - return in_array((string) $node->name, ['DEVELOPMENT', 'PRODUCTION'], true); + return in_array((string) $node->name, array_keys($this->oldConstantToNewValue), true); } /** @@ -52,12 +78,9 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector */ public function refactor(Node $classConstFetchNode): ?Node { - /** @var Identifier $constantName */ - $constantName = $classConstFetchNode->name; + $newValue = $this->oldConstantToNewValue[$classConstFetchNode->name->toString()]; - $originalConstantValue = $constantName->toLowerString(); - - return $this->nodeFactory->createString($originalConstantValue); + return $this->nodeFactory->createString($newValue); } private function getClassNameFromClassConstFetch(ClassConstFetch $classConstFetchNode): string