2021-01-27 14:31:55 +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\String_;
|
2021-01-27 14:31:55 +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-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;
|
2021-01-27 14:31:55 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://github.com/symfony/symfony/pull/35858
|
2021-01-27 14:31:55 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Renaming\Rector\String_\RenameStringRector\RenameStringRectorTest
|
2021-01-27 14:31:55 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RenameStringRector extends AbstractRector implements ConfigurableRectorInterface
|
2021-01-27 14:31:55 +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;
|
2021-01-27 14:31:55 +00:00
|
|
|
/**
|
2021-10-28 15:29:52 +00:00
|
|
|
* @var array<string, string>
|
2021-01-27 14:31:55 +00:00
|
|
|
*/
|
|
|
|
private $stringChanges = [];
|
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
|
2021-01-27 14:31:55 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change string value', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2021-01-27 14:31:55 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return 'ROLE_PREVIOUS_ADMIN';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-01-27 14:31:55 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return 'IS_IMPERSONATOR';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-12-06 21:12:03 +00:00
|
|
|
, ['ROLE_PREVIOUS_ADMIN' => 'IS_IMPERSONATOR'])]);
|
2021-01-27 14:31:55 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-01-27 14:31:55 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-01-27 14:31:55 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [String_::class];
|
2021-01-27 14:31:55 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param String_ $node
|
2021-01-27 14:31:55 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2021-01-27 14:31:55 +00:00
|
|
|
{
|
|
|
|
foreach ($this->stringChanges as $oldValue => $newValue) {
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isValue($node, $oldValue)) {
|
2021-01-27 14:31:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new String_($newValue);
|
2021-01-27 14:31:55 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-01-27 14:31:55 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2021-01-27 14:31:55 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allString(\array_keys($configuration));
|
|
|
|
Assert::allString($configuration);
|
2022-02-18 01:46:17 +00:00
|
|
|
$this->stringChanges = $configuration;
|
2021-01-27 14:31:55 +00:00
|
|
|
}
|
|
|
|
}
|