2021-01-27 14:31:55 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-01-27 14:31:55 +00:00
|
|
|
namespace Rector\Renaming\Rector\String_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2022-02-18 00:42:23 +00:00
|
|
|
use RectorPrefix20220218\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
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
final class RenameStringRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
|
2021-01-27 14:31:55 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-10 00:25:09 +00:00
|
|
|
* @deprecated
|
2021-01-27 14:31:55 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const STRING_CHANGES = 'string_changes';
|
|
|
|
/**
|
2021-10-28 15:29:52 +00:00
|
|
|
* @var array<string, string>
|
2021-01-27 14:31:55 +00:00
|
|
|
*/
|
|
|
|
private $stringChanges = [];
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
2021-01-27 14:31:55 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change string value', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return [\PhpParser\Node\Scalar\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
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\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;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PhpParser\Node\Scalar\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
|
|
|
{
|
2021-11-28 17:01:20 +00:00
|
|
|
$stringChanges = $configuration[self::STRING_CHANGES] ?? $configuration;
|
2022-02-18 00:42:23 +00:00
|
|
|
\RectorPrefix20220218\Webmozart\Assert\Assert::isArray($stringChanges);
|
|
|
|
\RectorPrefix20220218\Webmozart\Assert\Assert::allString(\array_keys($stringChanges));
|
|
|
|
\RectorPrefix20220218\Webmozart\Assert\Assert::allString($stringChanges);
|
2021-10-28 15:29:52 +00:00
|
|
|
$this->stringChanges = $stringChanges;
|
2021-01-27 14:31:55 +00:00
|
|
|
}
|
|
|
|
}
|