Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

37 changes: 8 additions & 29 deletions rules/Renaming/Rector/String_/RenameStringRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,18 @@

use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Renaming\Rector\String_\RenameStringRector\RenameStringRectorTest
* @deprecated This rule is deprecated, as it renames every matching string literal blindly and was only used for 1 case now covered by dedicated PreviousAdminRoleToIsImpersonatorRector in rector-symfony. Write a custom rule for the exact project case instead.
*/
final class RenameStringRector extends AbstractRector implements ConfigurableRectorInterface
final class RenameStringRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var array<string, string>
*/
private array $stringChanges = [];

public function __construct(
private readonly ValueResolver $valueResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change string value', [
Expand Down Expand Up @@ -72,27 +62,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$value = $this->valueResolver->getValue($node);
if (! is_string($value)) {
return null;
}

$newValue = $this->stringChanges[$value] ?? null;
if ($newValue === null) {
return null;
}

return new String_($newValue);
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as it renames every matching string literal blindly; write a custom rule for the exact project case instead',
self::class
));
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allString(array_keys($configuration));
Assert::allString($configuration);

$this->stringChanges = $configuration;
}
}
Loading