Skip to content

Commit

Permalink
Early-return NullSafeMethodCall in NodeNameResolver (#5840)
Browse files Browse the repository at this point in the history
* Support NullsafeMethodCall in NodeNameResolver

* Simplify with CallLike

* fix

* doc

* simplify

* simplify
  • Loading branch information
staabm committed May 2, 2024
1 parent 5193d43 commit c7d2ca3
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -71,14 +74,6 @@ public function isNames(Node $node, array $names): bool
*/
public function isName(Node | array $node, string $name): bool
{
if ($node instanceof MethodCall) {
return false;
}

if ($node instanceof StaticCall) {
return false;
}

$nodes = is_array($node) ? $node : [$node];

foreach ($nodes as $node) {
Expand All @@ -100,11 +95,7 @@ public function isCaseSensitiveName(Node $node, string $name): bool
return false;
}

if ($node instanceof MethodCall) {
return false;
}

if ($node instanceof StaticCall) {
if ($node instanceof CallLike && !$node instanceof FuncCall) {
return false;
}

Expand Down Expand Up @@ -143,7 +134,10 @@ public function getName(Node | string $node): ?string
return $namespacedName;
}

if (($node instanceof MethodCall || $node instanceof StaticCall) && $this->isCallOrIdentifier($node->name)) {
if (
($node instanceof MethodCall || $node instanceof StaticCall || $node instanceof NullsafeMethodCall)
&& $this->isCallOrIdentifier($node->name)
) {
return null;
}

Expand Down Expand Up @@ -242,7 +236,7 @@ private function isCallOrIdentifier(Expr|Identifier $node): bool

private function isSingleName(Node $node, string $desiredName): bool
{
if ($node instanceof MethodCall) {
if ($node instanceof CallLike && !$node instanceof FuncCall) {
// method call cannot have a name, only the variable or method name
return false;
}
Expand Down

0 comments on commit c7d2ca3

Please sign in to comment.