mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
Make PropertyAssignToMethodCallRector many-configurable
This commit is contained in:
parent
f3654b1719
commit
5f98af9333
@ -20,23 +20,17 @@ final class RenameClassConstantsUseToStringsRector extends AbstractRector
|
||||
private $nodeFactory;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @var string[][]
|
||||
*/
|
||||
private $class;
|
||||
private $oldConstantsToNewValuesByType = [];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
* @param string[][] $oldConstantsToNewValuesByType
|
||||
*/
|
||||
private $oldConstantToNewValue = [];
|
||||
|
||||
/**
|
||||
* @param string[] $oldConstantToNewValue
|
||||
*/
|
||||
public function __construct(NodeFactory $nodeFactory, string $class, array $oldConstantToNewValue)
|
||||
public function __construct(NodeFactory $nodeFactory, array $oldConstantsToNewValuesByType)
|
||||
{
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->class = $class;
|
||||
$this->oldConstantToNewValue = $oldConstantToNewValue;
|
||||
$this->oldConstantsToNewValuesByType = $oldConstantsToNewValuesByType;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
@ -46,8 +40,7 @@ final class RenameClassConstantsUseToStringsRector extends AbstractRector
|
||||
'$value === Nette\Configurator::DEVELOPMENT',
|
||||
'$value === "development"',
|
||||
[
|
||||
'$class' => 'Nette\Configurator',
|
||||
'$oldConstantToNewValue' => [
|
||||
'Nette\Configurator' => [
|
||||
'DEVELOPMENT' => 'development',
|
||||
'PRODUCTION' => 'production',
|
||||
],
|
||||
@ -70,16 +63,21 @@ final class RenameClassConstantsUseToStringsRector extends AbstractRector
|
||||
public function refactor(Node $classConstFetchNode): ?Node
|
||||
{
|
||||
$className = $this->getClassNameFromClassConstFetch($classConstFetchNode);
|
||||
if ($className !== $this->class) {
|
||||
return null;
|
||||
}
|
||||
if (array_key_exists((string) $classConstFetchNode->name, $this->oldConstantToNewValue) === false) {
|
||||
return null;
|
||||
}
|
||||
$newValue = $this->oldConstantToNewValue[(string) $classConstFetchNode->name];
|
||||
|
||||
foreach ($this->oldConstantsToNewValuesByType as $type => $oldConstantsToNewValues) {
|
||||
if ($className !== $type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($oldConstantsToNewValues as $oldConstant => $newValue) {
|
||||
if ((string) $classConstFetchNode->name === $oldConstant) {
|
||||
return $this->nodeFactory->createString($newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $classConstFetchNode;
|
||||
}
|
||||
|
||||
private function getClassNameFromClassConstFetch(ClassConstFetch $classConstFetchNode): string
|
||||
{
|
||||
|
@ -1,6 +1,5 @@
|
||||
services:
|
||||
Rector\Rector\Constant\RenameClassConstantsUseToStringsRector:
|
||||
$class: 'Nette\Configurator'
|
||||
$oldConstantToNewValue:
|
||||
Nette\Configurator:
|
||||
'DEVELOPMENT': 'development'
|
||||
'PRODUCTION': 'production'
|
||||
|
Loading…
x
Reference in New Issue
Block a user