rector/rules/Transform/Rector/String_/StringToClassConstantRector.php

98 lines
2.7 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
2019-02-09 23:12:10 +00:00
namespace Rector\Transform\Rector\String_;
2019-02-09 23:12:10 +00:00
use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
2020-07-29 23:39:41 +00:00
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Transform\ValueObject\StringToClassConstant;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
2019-02-09 23:12:10 +00:00
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\Tests\Transform\Rector\String_\StringToClassConstantRector\StringToClassConstantRectorTest
2019-09-03 09:11:45 +00:00
*/
2020-07-29 23:39:41 +00:00
final class StringToClassConstantRector extends AbstractRector implements ConfigurableRectorInterface
2019-02-09 23:12:10 +00:00
{
/**
2020-07-29 23:39:41 +00:00
* @var string
2019-02-09 23:12:10 +00:00
*/
2020-07-29 23:39:41 +00:00
public const STRINGS_TO_CLASS_CONSTANTS = 'strings_to_class_constants';
2019-02-09 23:12:10 +00:00
/**
* @var StringToClassConstant[]
2019-02-09 23:12:10 +00:00
*/
2020-07-29 23:39:41 +00:00
private $stringsToClassConstants = [];
2019-02-09 23:12:10 +00:00
public function getRuleDefinition(): RuleDefinition
2019-02-09 23:12:10 +00:00
{
return new RuleDefinition('Changes strings to specific constants', [
2019-02-09 23:12:10 +00:00
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
2019-02-09 23:12:10 +00:00
final class SomeSubscriber
{
public static function getSubscribedEvents()
{
return ['compiler.post_dump' => 'compile'];
}
}
CODE_SAMPLE
2019-02-09 23:12:10 +00:00
,
<<<'CODE_SAMPLE'
2019-02-09 23:12:10 +00:00
final class SomeSubscriber
{
public static function getSubscribedEvents()
{
return [\Yet\AnotherClass::CONSTANT => 'compile'];
}
}
CODE_SAMPLE
2019-02-09 23:12:10 +00:00
,
[
self::STRINGS_TO_CLASS_CONSTANTS => [
new StringToClassConstant('compiler.post_dump', 'Yet\AnotherClass', 'CONSTANT'),
],
2019-02-09 23:12:10 +00:00
]
),
]);
}
/**
* @return array<class-string<Node>>
2019-02-09 23:12:10 +00:00
*/
public function getNodeTypes(): array
{
return [String_::class];
}
/**
* @param String_ $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->stringsToClassConstants as $stringToClassConstant) {
2021-01-30 23:20:05 +00:00
if (! $this->valueResolver->isValue($node, $stringToClassConstant->getString())) {
2019-02-09 23:12:10 +00:00
continue;
}
2021-01-30 21:41:25 +00:00
return $this->nodeFactory->createClassConstFetch(
$stringToClassConstant->getClass(),
$stringToClassConstant->getConstant()
);
2019-02-09 23:12:10 +00:00
}
return $node;
}
2020-07-29 23:39:41 +00:00
public function configure(array $configuration): void
{
$stringToClassConstants = $configuration[self::STRINGS_TO_CLASS_CONSTANTS] ?? [];
Assert::allIsInstanceOf($stringToClassConstants, StringToClassConstant::class);
$this->stringsToClassConstants = $stringToClassConstants;
2020-07-29 23:39:41 +00:00
}
2019-02-09 23:12:10 +00:00
}