Skip to content

Commit

Permalink
Reverts Use MethodCallRename directly (#3080)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 19, 2022
1 parent 24b9eb7 commit 6c7caa6
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 14 deletions.
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,3 @@ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;

class RenameMethodCall
{
private function createHtml()
{
$html = new \Nette\Utils\Html();
$html->add('someContent');

$anotherHtml = $html;
$anotherHtml->add('someContent');
}
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;

class RenameMethodCall
{
private function createHtml()
{
$html = new \Nette\Utils\Html();
$html->addHtml('someContent');

$anotherHtml = $html;
$anotherHtml->addHtml('someContent');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;

class RenameMethodCallWithArrayKey
{
private function createHtml()
{
$html = new \Nette\Utils\Html();
$html->addToArray('someContent');
}
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Fixture;

class RenameMethodCallWithArrayKey
{
private function createHtml()
{
$html = new \Nette\Utils\Html();
$html->addToHtmlArray('someContent')['hey'];
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 @@ -21,5 +22,7 @@
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\ValueObject\MethodCallRename;
use Rector\Renaming\Contract\MethodCallRenameInterface;

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

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

/**
* @return MethodCallRename[]
* @return MethodCallRenameInterface[]
*/
public function getMethodCallRenames(): array
{
Expand Down
18 changes: 18 additions & 0 deletions rules/Renaming/Contract/MethodCallRenameInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Rector\Renaming\Contract;

use PHPStan\Type\ObjectType;

interface MethodCallRenameInterface
{
public function getClass(): string;

public function getObjectType(): ObjectType;

public function getOldMethod(): string;

public function getNewMethod(): string;
}
19 changes: 14 additions & 5 deletions rules/Renaming/Rector/MethodCall/RenameMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

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 @@ -18,7 +20,9 @@
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 @@ -29,7 +33,7 @@
final class RenameMethodRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface
{
/**
* @var MethodCallRename[]
* @var MethodCallRenameInterface[]
*/
private array $methodCallRenames = [];

Expand Down Expand Up @@ -96,6 +100,11 @@ 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 @@ -107,15 +116,15 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, MethodCallRename::class);
Assert::allIsAOf($configuration, MethodCallRenameInterface::class);

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

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

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

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

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

final class MethodCallRename
final class MethodCallRename implements MethodCallRenameInterface
{
public function __construct(
private readonly string $class,
Expand Down
51 changes: 51 additions & 0 deletions rules/Renaming/ValueObject/MethodCallRenameWithArrayKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Rector\Renaming\ValueObject;

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

final class MethodCallRenameWithArrayKey implements MethodCallRenameInterface
{
public function __construct(
private readonly string $class,
private readonly string $oldMethod,
private readonly string $newMethod,
private readonly mixed $arrayKey
) {
RectorAssert::className($class);
RectorAssert::methodName($oldMethod);
RectorAssert::methodName($newMethod);
}

public function getClass(): string
{
return $this->class;
}

public function getObjectType(): ObjectType
{
return new ObjectType($this->class);
}

public function getOldMethod(): string
{
return $this->oldMethod;
}

public function getNewMethod(): string
{
return $this->newMethod;
}

/**
* @return mixed
*/
public function getArrayKey()
{
return $this->arrayKey;
}
}

0 comments on commit 6c7caa6

Please sign in to comment.