savinmikhail /
AddNamedArgumentsRector
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SavinMikhail\AddNamedArgumentsRector\Config; |
||
| 6 | |||
| 7 | use PhpParser\Node; |
||
| 8 | use PhpParser\Node\Expr\FuncCall; |
||
| 9 | use PhpParser\Node\Expr\MethodCall; |
||
| 10 | use PhpParser\Node\Expr\New_; |
||
| 11 | use PhpParser\Node\Expr\StaticCall; |
||
| 12 | use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass; |
||
| 13 | use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum; |
||
| 14 | use PHPStan\Reflection\ClassReflection; |
||
| 15 | use ReflectionFunctionAbstract; |
||
| 16 | use SavinMikhail\AddNamedArgumentsRector\Reflection\Reflection; |
||
| 17 | |||
| 18 | final readonly class DefaultStrategy implements ConfigStrategy |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 19 | { |
||
| 20 | 13 | public static function shouldApply( |
|
| 21 | FuncCall|StaticCall|MethodCall|New_ $node, |
||
| 22 | array $parameters, |
||
| 23 | ?ClassReflection $classReflection = null, |
||
| 24 | ): bool { |
||
| 25 | 13 | if (!self::areArgumentsSuitable($node->args, $parameters)) { |
|
| 26 | 4 | return false; |
|
| 27 | } |
||
| 28 | |||
| 29 | 9 | if ($classReflection !== null && !self::classAllowsNamedArguments($classReflection)) { |
|
| 30 | 1 | return false; |
|
| 31 | } |
||
| 32 | |||
| 33 | 8 | if (!self::functionAllowsNamedArguments($node, $classReflection)) { |
|
| 34 | 1 | return false; |
|
| 35 | } |
||
| 36 | |||
| 37 | 8 | return true; |
|
| 38 | } |
||
| 39 | |||
| 40 | 5 | private static function classAllowsNamedArguments(ClassReflection $classReflection): bool |
|
| 41 | { |
||
| 42 | 5 | $reflectionClass = $classReflection->getNativeReflection(); |
|
| 43 | |||
| 44 | 5 | while ($reflectionClass) { |
|
| 45 | 5 | if (self::hasNoNamedArgumentsTag($reflectionClass)) { |
|
| 46 | 1 | return false; |
|
| 47 | } |
||
| 48 | // Check if the class has @no-named-arguments annotation, even in the parent classes |
||
| 49 | 5 | $reflectionClass = $reflectionClass->getParentClass(); |
|
| 50 | } |
||
| 51 | |||
| 52 | 4 | if ($classReflection->isInterface()) { |
|
| 53 | // 🚨 Stop rule, cuz in runtime might be resolved any implementation of the interface, and the names of arguments might differ |
||
| 54 | return false; |
||
| 55 | } |
||
| 56 | |||
| 57 | 4 | return true; |
|
| 58 | } |
||
| 59 | |||
| 60 | 13 | private static function areArgumentsSuitable(array $args, array $parameters): bool |
|
| 61 | { |
||
| 62 | 13 | foreach ($args as $index => $arg) { |
|
| 63 | 13 | if (!isset($parameters[$index])) { |
|
| 64 | 2 | return false; |
|
| 65 | } |
||
| 66 | |||
| 67 | // Skip variadic parameters (...$param) |
||
| 68 | 11 | if ($parameters[$index]->isVariadic()) { |
|
| 69 | 1 | return false; |
|
| 70 | } |
||
| 71 | |||
| 72 | 11 | if ($arg instanceof Node\VariadicPlaceholder) { |
|
| 73 | return false; |
||
| 74 | } |
||
| 75 | |||
| 76 | // Skip unpacking arguments (...$var) |
||
| 77 | 11 | if ($arg instanceof Node\Arg && $arg->unpack) { |
|
| 78 | 1 | return false; |
|
| 79 | } |
||
| 80 | |||
| 81 | // Allow already named arguments as long as they reference the parameter in the same position |
||
| 82 | 10 | if ($arg->name !== null) { |
|
| 83 | 2 | $argName = $arg->name instanceof Node\Identifier ? $arg->name->toString() : null; |
|
| 84 | |||
| 85 | 2 | if ($argName !== $parameters[$index]->getName()) { |
|
| 86 | return false; |
||
| 87 | } |
||
| 88 | |||
| 89 | 2 | continue; |
|
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | 9 | return true; |
|
| 94 | } |
||
| 95 | |||
| 96 | 9 | private static function hasNoNamedArgumentsTag(ReflectionFunctionAbstract|ReflectionClass|ReflectionEnum $reflection): bool |
|
| 97 | { |
||
| 98 | 9 | $docComment = $reflection->getDocComment(); |
|
| 99 | |||
| 100 | 9 | return $docComment !== false && str_contains(haystack: $docComment, needle: '@no-named-arguments'); |
|
| 101 | } |
||
| 102 | |||
| 103 | 8 | private static function functionAllowsNamedArguments( |
|
| 104 | FuncCall|StaticCall|MethodCall|New_ $node, |
||
| 105 | ?ClassReflection $classReflection = null, |
||
| 106 | ): bool { |
||
| 107 | 8 | $functionReflection = Reflection::getFunctionReflection(node: $node, classReflection: $classReflection); |
|
| 108 | 8 | if ($functionReflection === null) { |
|
| 109 | 1 | return false; // 🚨 Stop rule if method doesn't exist (likely a @method annotation) |
|
| 110 | } |
||
| 111 | |||
| 112 | 8 | if (self::hasNoNamedArgumentsTag($functionReflection)) { |
|
| 113 | return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | 8 | return true; |
|
| 117 | } |
||
| 118 | } |
||
| 119 |