Skip to content

Commit

Permalink
Apply node->isFirstCallable() check early before ->getArgs() when pos…
Browse files Browse the repository at this point in the history
…sible on CallLike (#3038)
  • Loading branch information
samsonasik committed Nov 7, 2022
1 parent 84361b8 commit 10346ee
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public static function select(
): ParametersAcceptor {
$variants = $reflection->getVariants();

if ($callLike->isFirstClassCallable()) {
return ParametersAcceptorSelector::selectSingle($variants);
}

return count($variants) > 1
? ParametersAcceptorSelector::selectFromArgs($scope, $callLike->getArgs(), $variants)
: ParametersAcceptorSelector::selectSingle($variants);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function resolve(Expr $expr, string $currentFilePath): mixed
return dirname($currentFilePath);
}

$args = $expr->getArgs();
$args = $expr->isFirstClassCallable() ? [] : $expr->getArgs();
$arguments = [];
foreach ($args as $arg) {
$arguments[] = $this->constExprEvaluator->evaluateDirectly($arg->value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$args = $node->getArgs();
$secondArg = $args[1];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function refactor(Node $node): ?array
return null;
}

if ($funcCall->isFirstClassCallable()) {
return null;
}

if ($this->hasArraySpread($funcCall)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function refactor(Node $node): ?Node
return $this->compactConverter->convertToArray($node);
}

if ($node->isFirstClassCallable()) {
return null;
}

$firstArg = $node->getArgs()[0];

$firstValue = $firstArg->value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$args = $node->getArgs();
$firstArgValue = $args[0]->value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ private function isUsedInAssignExpr(CallLike | Expr $expr, Assign $assign): bool
return $this->isUsedInPreviousAssign($assign, $expr);
}

if ($expr->isFirstClassCallable()) {
return false;
}

foreach ($expr->getArgs() as $arg) {
$variable = $arg->value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ private function removeCallerArgs(Class_ $class, ClassMethod $classMethod, array
*/
private function cleanupArgs(MethodCall $methodCall, array $keysArg): void
{
if ($methodCall->isFirstClassCallable()) {
return;
}

$args = $methodCall->getArgs();
foreach (array_keys($args) as $key) {
if (in_array($key, $keysArg, true)) {
Expand Down
2 changes: 1 addition & 1 deletion rules/Naming/Matcher/VariableAndCallAssignMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function match(Assign $assign): ?VariableAndCallAssign
}

$isVariableFoundInCallArgs = (bool) $this->betterNodeFinder->findFirst(
$call->getArgs(),
$call->isFirstClassCallable() ? [] : $call->getArgs(),
fn (Node $subNode): bool =>
$subNode instanceof Variable && $this->nodeNameResolver->isName($subNode, $variableName)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\VariadicPlaceholder;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Transform\ValueObject\StaticCallRecipe;
Expand Down Expand Up @@ -113,7 +114,9 @@ private function createStaticCall(FuncCall $fileGetContentsFuncCall): StaticCall
return new StaticCall(
$fullyQualified,
$this->staticCallRecipe->getMethodName(),
$fileGetContentsFuncCall->getArgs()
$fileGetContentsFuncCall->isFirstClassCallable()
? [new VariadicPlaceholder()]
: $fileGetContentsFuncCall->getArgs()
);
}

Expand Down

0 comments on commit 10346ee

Please sign in to comment.