2017-10-20 18:29:23 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Rector\Dynamic;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\ClassConstFetch;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use Rector\NodeAnalyzer\ClassConstAnalyzer;
|
2017-12-25 19:10:47 +00:00
|
|
|
use Rector\NodeChanger\MethodNameChanger;
|
2017-10-20 18:29:23 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
|
|
|
|
final class ClassConstantReplacerRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* class => [
|
|
|
|
* OLD_CONSTANT => NEW_CONSTANT
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $oldToNewConstantsByClass = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ClassConstAnalyzer
|
|
|
|
*/
|
|
|
|
private $classConstAnalyzer;
|
|
|
|
|
2017-12-25 19:10:47 +00:00
|
|
|
/**
|
|
|
|
* @var MethodNameChanger
|
|
|
|
*/
|
|
|
|
private $methodNameChanger;
|
|
|
|
|
2017-10-20 18:29:23 +00:00
|
|
|
/**
|
2017-11-06 12:58:51 +00:00
|
|
|
* @var string|null
|
2017-10-20 18:29:23 +00:00
|
|
|
*/
|
|
|
|
private $activeType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $oldToNewConstantsByClass
|
|
|
|
*/
|
2017-12-25 19:10:47 +00:00
|
|
|
public function __construct(array $oldToNewConstantsByClass, ClassConstAnalyzer $classConstAnalyzer, MethodNameChanger $methodNameChanger)
|
2017-10-20 18:29:23 +00:00
|
|
|
{
|
|
|
|
$this->oldToNewConstantsByClass = $oldToNewConstantsByClass;
|
|
|
|
$this->classConstAnalyzer = $classConstAnalyzer;
|
2017-12-25 19:10:47 +00:00
|
|
|
$this->methodNameChanger = $methodNameChanger;
|
2017-10-20 18:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isCandidate(Node $node): bool
|
|
|
|
{
|
|
|
|
$this->activeType = null;
|
|
|
|
|
|
|
|
foreach ($this->oldToNewConstantsByClass as $type => $oldToNewConstants) {
|
|
|
|
$matchedType = $this->classConstAnalyzer->matchTypes($node, $this->getTypes());
|
|
|
|
if ($matchedType) {
|
|
|
|
$this->activeType = $matchedType;
|
2017-10-20 18:35:04 +00:00
|
|
|
|
2017-10-20 18:29:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ClassConstFetch $classConstFetchNode
|
|
|
|
*/
|
|
|
|
public function refactor(Node $classConstFetchNode): ?Node
|
|
|
|
{
|
|
|
|
$configuration = $this->oldToNewConstantsByClass[$this->activeType];
|
2017-12-10 00:28:51 +00:00
|
|
|
|
|
|
|
/** @var Identifier $identifierNode */
|
|
|
|
$identifierNode = $classConstFetchNode->name;
|
|
|
|
|
|
|
|
$constantName = $identifierNode->toString();
|
2017-10-20 18:29:23 +00:00
|
|
|
|
|
|
|
$newConstantName = $configuration[$constantName];
|
|
|
|
|
2017-12-25 19:10:47 +00:00
|
|
|
if (! isset($newConstantName)) {
|
|
|
|
return $classConstFetchNode;
|
|
|
|
}
|
2017-10-20 18:29:23 +00:00
|
|
|
|
2017-12-25 19:10:47 +00:00
|
|
|
return $this->methodNameChanger->renameNode($classConstFetchNode, $newConstantName);
|
2017-10-20 18:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
private function getTypes(): array
|
|
|
|
{
|
|
|
|
return array_keys($this->oldToNewConstantsByClass);
|
|
|
|
}
|
|
|
|
}
|