Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RenameMethodRector should handle NullsafeMethodCall #5444

Merged
merged 1 commit into from
Jan 7, 2024
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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