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
99 changes: 0 additions & 99 deletions bench/rename_static_method_bucket_bench.php

This file was deleted.

1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ parameters:
paths:
- rules/Php81/Rector/FuncCall/NullToStrictStringFuncCallArgRector.php
- rules/Removing/Rector/Class_/RemoveTraitUseRector.php
- rules/Renaming/Rector/StaticCall/RenameStaticMethodRector.php
- rules/Transform/Rector/String_/StringToClassConstantRector.php

# lack of generic array in nikic/php-parser
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

50 changes: 8 additions & 42 deletions rules/Renaming/Rector/StaticCall/RenameStaticMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\Renaming\ValueObject\RenameStaticMethod;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Renaming\Rector\StaticCall\RenameStaticMethodRector\RenameStaticMethodRectorTest
* @deprecated This rule is deprecated, as too niche, never used and needs project context. Write a custom rule for the exact project case instead.
*/
final class RenameStaticMethodRector extends AbstractRector implements ConfigurableRectorInterface
final class RenameStaticMethodRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var array<string, RenameStaticMethod[]>
*/
private array $staticMethodRenamesByOldMethod = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Turn method names to new ones', [
Expand All @@ -49,44 +43,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}

foreach ($this->staticMethodRenamesByOldMethod[strtolower($methodName)] ?? [] as $staticMethodRename) {
if (! $this->isObjectType($node->class, $staticMethodRename->getOldObjectType())) {
continue;
}

return $this->rename($node, $staticMethodRename);
}

return null;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as too niche and part of no set; write a custom rule for the exact project case instead',
self::class
));
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, RenameStaticMethod::class);

// bucket by lowercased old method name (method names are case-insensitive)
$this->staticMethodRenamesByOldMethod = [];
foreach ($configuration as $staticMethodRename) {
$this->staticMethodRenamesByOldMethod[strtolower($staticMethodRename->getOldMethod())][] = $staticMethodRename;
}
}

private function rename(StaticCall $staticCall, RenameStaticMethod $renameStaticMethod): StaticCall
{
$staticCall->name = new Identifier($renameStaticMethod->getNewMethod());

if ($renameStaticMethod->hasClassChanged()) {
$staticCall->class = new FullyQualified($renameStaticMethod->getNewClass());
}

return $staticCall;
}
}
3 changes: 3 additions & 0 deletions rules/Renaming/ValueObject/RenameStaticMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PHPStan\Type\ObjectType;
use Rector\Validation\RectorAssert;

/**
* @api used in deprecated RenameStaticMethodRector configs
*/
final readonly class RenameStaticMethod
{
public function __construct(
Expand Down
Loading