2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Renaming\Rector\ConstFetch;
|
2019-02-19 14:53:31 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\ConstFetch;
|
|
|
|
use PhpParser\Node\Name;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\Validation\RectorAssert;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-04-01 16:51:34 +00:00
|
|
|
use RectorPrefix202404\Webmozart\Assert\Assert;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Renaming\Rector\ConstFetch\RenameConstantRector\RenameConstantRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RenameConstantRector extends AbstractRector implements ConfigurableRectorInterface
|
2019-02-19 14:53:31 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-04-13 16:01:31 +00:00
|
|
|
* @var array<string, string>
|
2019-02-19 14:53:31 +00:00
|
|
|
*/
|
2020-07-29 13:55:33 +00:00
|
|
|
private $oldToNewConstants = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-02-19 14:53:31 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Replace constant by new ones', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2019-02-19 14:53:31 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return MYSQL_ASSOC;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-02-19 14:53:31 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return MYSQLI_ASSOC;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-12-06 21:12:03 +00:00
|
|
|
, ['MYSQL_ASSOC' => 'MYSQLI_ASSOC', 'OLD_CONSTANT' => 'NEW_CONSTANT'])]);
|
2019-02-19 14:53:31 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-02-19 14:53:31 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-02-19 14:53:31 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ConstFetch::class];
|
2019-02-19 14:53:31 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ConstFetch $node
|
2019-02-19 14:53:31 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-02-19 14:53:31 +00:00
|
|
|
{
|
|
|
|
foreach ($this->oldToNewConstants as $oldConstant => $newConstant) {
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node->name, $oldConstant)) {
|
2019-02-19 14:53:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$node->name = new Name($newConstant);
|
2021-04-13 16:01:31 +00:00
|
|
|
return $node;
|
2019-02-19 14:53:31 +00:00
|
|
|
}
|
2021-04-13 16:01:31 +00:00
|
|
|
return null;
|
2019-02-19 14:53:31 +00:00
|
|
|
}
|
2021-04-13 16:01:31 +00:00
|
|
|
/**
|
2021-11-27 13:43:28 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-04-13 16:01:31 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2020-07-29 13:55:33 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allString(\array_keys($configuration));
|
|
|
|
Assert::allString($configuration);
|
2022-07-16 12:31:38 +00:00
|
|
|
foreach ($configuration as $oldConstant => $newConstant) {
|
|
|
|
RectorAssert::constantName($oldConstant);
|
|
|
|
RectorAssert::constantName($newConstant);
|
|
|
|
}
|
2022-02-18 01:46:17 +00:00
|
|
|
/** @var array<string, string> $configuration */
|
|
|
|
$this->oldToNewConstants = $configuration;
|
2020-07-29 13:55:33 +00:00
|
|
|
}
|
2019-02-19 14:53:31 +00:00
|
|
|
}
|