|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PhpParser\NodeVisitor; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\Expr\Array_; |
| 10 | +use PhpParser\Node\Expr\CallLike; |
| 11 | +use PhpParser\Node\Identifier; |
| 12 | +use PhpParser\NodeVisitorAbstract; |
| 13 | +use PHPStan\Reflection\FunctionReflection; |
| 14 | +use PHPStan\Reflection\MethodReflection; |
| 15 | +use PHPStan\Reflection\ParameterReflection; |
| 16 | +use PHPStan\Type\ObjectType; |
| 17 | +use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface; |
| 18 | +use Rector\NodeTypeResolver\Node\AttributeKey; |
| 19 | +use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper; |
| 20 | +use Rector\PHPStan\ScopeFetcher; |
| 21 | +use Rector\Reflection\ReflectionResolver; |
| 22 | + |
| 23 | +/** |
| 24 | + * Mark array arguments passed to a parameter that cannot hold a Closure, |
| 25 | + * e.g. "array" or "string|array|null", so an array callable is kept as is |
| 26 | + * |
| 27 | + * @see https://github.com/rectorphp/rector/issues/9563 |
| 28 | + */ |
| 29 | +final class ArgNotAcceptingClosureNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface |
| 30 | +{ |
| 31 | + public function __construct( |
| 32 | + private readonly ReflectionResolver $reflectionResolver |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + public function enterNode(Node $node): ?Node |
| 37 | + { |
| 38 | + if (! $node instanceof CallLike) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + if ($node->isFirstClassCallable()) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + $args = $node->getArgs(); |
| 47 | + if (! array_any($args, static fn (Arg $arg): bool => $arg->value instanceof Array_)) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + $functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node); |
| 52 | + if (! $functionLikeReflection instanceof FunctionReflection && ! $functionLikeReflection instanceof MethodReflection) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + $parameterReflections = ParametersAcceptorSelectorVariantsWrapper::select( |
| 57 | + $functionLikeReflection, |
| 58 | + $node, |
| 59 | + ScopeFetcher::fetch($node) |
| 60 | + )->getParameters(); |
| 61 | + |
| 62 | + $closureObjectType = new ObjectType('Closure'); |
| 63 | + |
| 64 | + foreach ($args as $position => $arg) { |
| 65 | + if (! $arg->value instanceof Array_) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + $parameterReflection = $this->matchParameterReflection($arg, $position, $parameterReflections); |
| 70 | + if (! $parameterReflection instanceof ParameterReflection) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if (! $parameterReflection->getType()->accepts($closureObjectType, true)->no()) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + $arg->value->setAttribute(AttributeKey::IS_ARG_NOT_ACCEPTING_CLOSURE, true); |
| 79 | + } |
| 80 | + |
| 81 | + return $node; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param ParameterReflection[] $parameterReflections |
| 86 | + */ |
| 87 | + private function matchParameterReflection( |
| 88 | + Arg $arg, |
| 89 | + int $position, |
| 90 | + array $parameterReflections |
| 91 | + ): ?ParameterReflection { |
| 92 | + if ($arg->name instanceof Identifier) { |
| 93 | + $argName = $arg->name->toString(); |
| 94 | + |
| 95 | + foreach ($parameterReflections as $parameterReflection) { |
| 96 | + if ($parameterReflection->getName() === $argName) { |
| 97 | + return $parameterReflection; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + if (isset($parameterReflections[$position])) { |
| 105 | + return $parameterReflections[$position]; |
| 106 | + } |
| 107 | + |
| 108 | + $lastParameterReflection = end($parameterReflections); |
| 109 | + if ($lastParameterReflection instanceof ParameterReflection && $lastParameterReflection->isVariadic()) { |
| 110 | + return $lastParameterReflection; |
| 111 | + } |
| 112 | + |
| 113 | + return null; |
| 114 | + } |
| 115 | +} |
0 commit comments