Skip to content

Commit

Permalink
Use MethodCallRename directly (#3077)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 18, 2022
1 parent 0d16e13 commit 31bf9e4
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 156 deletions.
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -781,3 +781,7 @@ parameters:
-
path: packages/NodeTypeResolver/DependencyInjection/PHPStanServicesFactory.php
message: '#Offset (.*?)includes(.*?) always exists and is not nullable#'

-
message: '#Value object "MethodCallRename" should be named "RenameMethod" instead to respect used service#'
path: config/set/gmagick-to-imagick.php

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\AbstractType;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\CustomType;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\DifferentInterface;
Expand All @@ -22,7 +21,5 @@
new MethodCallRename(Foo::class, 'old', 'new'),
new MethodCallRename(NewInterface::class, 'some_old', 'some_new'),
new MethodCallRename(DifferentInterface::class, 'renameMe', 'toNewVersion'),
// with array key
new MethodCallRenameWithArrayKey('Nette\Utils\Html', 'addToArray', 'addToHtmlArray', 'hey'),
]);
};
8 changes: 4 additions & 4 deletions rules/Renaming/Collector/MethodCallRenameCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

namespace Rector\Renaming\Collector;

use Rector\Renaming\Contract\MethodCallRenameInterface;
use Rector\Renaming\ValueObject\MethodCallRename;

final class MethodCallRenameCollector
{
/**
* @var MethodCallRenameInterface[]
* @var MethodCallRename[]
*/
private array $methodCallRenames = [];

/**
* @param MethodCallRenameInterface[] $methodCallRenames
* @param MethodCallRename[] $methodCallRenames
*/
public function addMethodCallRenames(array $methodCallRenames): void
{
$this->methodCallRenames = array_merge($this->methodCallRenames, $methodCallRenames);
}

/**
* @return MethodCallRenameInterface[]
* @return MethodCallRename[]
*/
public function getMethodCallRenames(): array
{
Expand Down
18 changes: 0 additions & 18 deletions rules/Renaming/Contract/MethodCallRenameInterface.php

This file was deleted.

19 changes: 5 additions & 14 deletions rules/Renaming/Rector/MethodCall/RenameMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Rector\Renaming\Rector\MethodCall;

use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
Expand All @@ -20,9 +18,7 @@
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Renaming\Collector\MethodCallRenameCollector;
use Rector\Renaming\Contract\MethodCallRenameInterface;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
Expand All @@ -33,7 +29,7 @@
final class RenameMethodRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface
{
/**
* @var MethodCallRenameInterface[]
* @var MethodCallRename[]
*/
private array $methodCallRenames = [];

Expand Down Expand Up @@ -100,11 +96,6 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
}

$node->name = new Identifier($methodCallRename->getNewMethod());

if ($methodCallRename instanceof MethodCallRenameWithArrayKey && ! $node instanceof ClassMethod) {
return new ArrayDimFetch($node, BuilderHelpers::normalizeValue($methodCallRename->getArrayKey()));
}

return $node;
}

Expand All @@ -116,15 +107,15 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, MethodCallRenameInterface::class);
Assert::allIsAOf($configuration, MethodCallRename::class);

$this->methodCallRenames = $configuration;
$this->methodCallRenameCollector->addMethodCallRenames($configuration);
}

private function shouldSkipClassMethod(
MethodCall | StaticCall | ClassMethod $node,
MethodCallRenameInterface $methodCallRename
MethodCallRename $methodCallRename
): bool {
if (! $node instanceof ClassMethod) {
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($node);
Expand Down Expand Up @@ -156,7 +147,7 @@ private function shouldSkipClassMethod(

private function shouldSkipForAlreadyExistingClassMethod(
ClassMethod $classMethod,
MethodCallRenameInterface $methodCallRename
MethodCallRename $methodCallRename
): bool {
$classLike = $this->betterNodeFinder->findParentType($classMethod, ClassLike::class);
if (! $classLike instanceof ClassLike) {
Expand All @@ -167,7 +158,7 @@ private function shouldSkipForAlreadyExistingClassMethod(
}

private function shouldKeepForParentInterface(
MethodCallRenameInterface $methodCallRename,
MethodCallRename $methodCallRename,
ClassMethod|StaticCall|MethodCall $node,
?ClassReflection $classReflection
): bool {
Expand Down
3 changes: 1 addition & 2 deletions rules/Renaming/ValueObject/MethodCallRename.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
use Rector\Renaming\Contract\MethodCallRenameInterface;

final class MethodCallRename implements MethodCallRenameInterface
final class MethodCallRename
{
public function __construct(
private readonly string $class,
Expand Down
51 changes: 0 additions & 51 deletions rules/Renaming/ValueObject/MethodCallRenameWithArrayKey.php

This file was deleted.

0 comments on commit 31bf9e4

Please sign in to comment.