Skip to content

Commit

Permalink
fix: RenameMethodRector should handle NullsafeMethodCall (#5444)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Jan 7, 2024
1 parent 7f2155d commit b71c326
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
@@ -0,0 +1,35 @@
<?php

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

class RenameNullSafeMethodCall
{
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 RenameNullSafeMethodCall
{
private function createHtml()
{
$html = new \Nette\Utils\Html();
$html?->addHtml('someContent');

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

?>
11 changes: 6 additions & 5 deletions rules/Renaming/Rector/MethodCall/RenameMethodRector.php
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -68,11 +69,11 @@ public function getRuleDefinition(): RuleDefinition
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class, Class_::class, Interface_::class];
return [MethodCall::class, NullsafeMethodCall::class, StaticCall::class, Class_::class, Interface_::class];
}

/**
* @param MethodCall|StaticCall|Class_|Interface_ $node
* @param MethodCall|NullsafeMethodCall|StaticCall|Class_|Interface_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
Expand All @@ -94,7 +95,7 @@ public function configure(array $configuration): void
}

private function shouldSkipClassMethod(
MethodCall | StaticCall $call,
MethodCall|NullsafeMethodCall|StaticCall $call,
MethodCallRenameInterface $methodCallRename
): bool {
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($call);
Expand Down Expand Up @@ -212,8 +213,8 @@ private function shouldSkipRename(
}

private function refactorMethodCallAndStaticCall(
StaticCall|MethodCall $call
): ArrayDimFetch|null|MethodCall|StaticCall {
StaticCall|MethodCall|NullsafeMethodCall $call
): ArrayDimFetch|null|MethodCall|StaticCall|NullsafeMethodCall {
$callName = $this->getName($call->name);
if ($callName === null) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/NodeTypeResolver/NodeTypeResolver.php
Expand Up @@ -301,7 +301,7 @@ public function getFullyQualifiedClassName(TypeWithClassName $typeWithClassName)

public function isMethodStaticCallOrClassMethodObjectType(Node $node, ObjectType $objectType): bool
{
if ($node instanceof MethodCall) {
if ($node instanceof MethodCall || $node instanceof Expr\NullsafeMethodCall) {
// method call is variable return
return $this->isObjectType($node->var, $objectType);
}
Expand Down
5 changes: 3 additions & 2 deletions src/PhpParser/AstResolver.php
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
Expand Down Expand Up @@ -174,9 +175,9 @@ public function resolveClassMethod(string $className, string $methodName): ?Clas
return $classMethod;
}

public function resolveClassMethodFromCall(MethodCall | StaticCall $call): ?ClassMethod
public function resolveClassMethodFromCall(MethodCall | StaticCall | NullsafeMethodCall $call): ?ClassMethod
{
$callerStaticType = $call instanceof MethodCall
$callerStaticType = ($call instanceof MethodCall || $call instanceof NullsafeMethodCall)
? $this->nodeTypeResolver->getType($call->var)
: $this->nodeTypeResolver->getType($call->class);

Expand Down
3 changes: 2 additions & 1 deletion src/Reflection/ReflectionResolver.php
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
Expand Down Expand Up @@ -76,7 +77,7 @@ public function resolveClassReflection(?Node $node): ?ClassReflection
}

public function resolveClassReflectionSourceObject(
MethodCall|StaticCall|PropertyFetch|StaticPropertyFetch $node
MethodCall|NullsafeMethodCall|StaticCall|PropertyFetch|StaticPropertyFetch $node
): ?ClassReflection {
if ($node instanceof PropertyFetch || $node instanceof StaticPropertyFetch) {
$objectType = $node instanceof PropertyFetch
Expand Down
2 changes: 1 addition & 1 deletion src/Testing/PHPUnit/AbstractRectorTestCase.php
Expand Up @@ -204,7 +204,7 @@ private function includePreloadFilesAndScoperAutoload(): void
if (file_exists(__DIR__ . '/../../../preload.php')) {
if (file_exists(__DIR__ . '/../../../vendor')) {
require_once __DIR__ . '/../../../preload.php';
// test case in rector split package
// test case in rector split package
} elseif (file_exists(__DIR__ . '/../../../../../../vendor')) {
require_once __DIR__ . '/../../../preload-split-package.php';
}
Expand Down

0 comments on commit b71c326

Please sign in to comment.