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\Transform\Rector\String_;
|
2019-02-09 23:12:10 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Transform\ValueObject\StringToClassConstant;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-05-02 02:21:56 +00:00
|
|
|
use RectorPrefix202405\Webmozart\Assert\Assert;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Transform\Rector\String_\StringToClassConstantRector\StringToClassConstantRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class StringToClassConstantRector extends AbstractRector implements ConfigurableRectorInterface
|
2019-02-09 23:12:10 +00:00
|
|
|
{
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
2019-02-09 23:12:10 +00:00
|
|
|
/**
|
2020-08-26 10:54:53 +00:00
|
|
|
* @var StringToClassConstant[]
|
2019-02-09 23:12:10 +00:00
|
|
|
*/
|
2020-07-29 23:39:41 +00:00
|
|
|
private $stringsToClassConstants = [];
|
2023-09-20 12:53:23 +00:00
|
|
|
public function __construct(ValueResolver $valueResolver)
|
|
|
|
{
|
|
|
|
$this->valueResolver = $valueResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-02-09 23:12:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes strings to specific constants', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2019-02-09 23:12:10 +00:00
|
|
|
final class SomeSubscriber
|
|
|
|
{
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return ['compiler.post_dump' => 'compile'];
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-02-09 23:12:10 +00:00
|
|
|
final class SomeSubscriber
|
|
|
|
{
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return [\Yet\AnotherClass::CONSTANT => 'compile'];
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2022-06-07 09:46:15 +00:00
|
|
|
, [new StringToClassConstant('compiler.post_dump', 'Yet\\AnotherClass', 'CONSTANT')])]);
|
2019-02-09 23:12:10 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-02-09 23:12:10 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-02-09 23:12:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [String_::class];
|
2019-02-09 23:12:10 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param String_ $node
|
2019-02-09 23:12:10 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-02-09 23:12:10 +00:00
|
|
|
{
|
2020-08-26 10:54:53 +00:00
|
|
|
foreach ($this->stringsToClassConstants as $stringToClassConstant) {
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isValue($node, $stringToClassConstant->getString())) {
|
2019-02-09 23:12:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return $this->nodeFactory->createClassConstFetch($stringToClassConstant->getClass(), $stringToClassConstant->getConstant());
|
2019-02-09 23:12:10 +00:00
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
2021-05-02 10:46:55 +00:00
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-05-02 10:46:55 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2020-07-29 23:39:41 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allIsAOf($configuration, StringToClassConstant::class);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->stringsToClassConstants = $configuration;
|
2020-07-29 23:39:41 +00:00
|
|
|
}
|
2019-02-09 23:12:10 +00:00
|
|
|
}
|