|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\CodeQuality\Rector\Attribute; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Arg; |
| 8 | +use PhpParser\Node\Attribute; |
| 9 | +use PhpParser\Node\Identifier; |
| 10 | +use PHPStan\Reflection\MethodReflection; |
| 11 | +use PHPStan\Reflection\ParameterReflection; |
| 12 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 13 | +use Rector\Rector\AbstractRector; |
| 14 | +use Rector\Reflection\ReflectionResolver; |
| 15 | +use Rector\ValueObject\PhpVersionFeature; |
| 16 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 19 | +/** |
| 20 | + * @see \Rector\Tests\CodeQuality\Rector\Attribute\ExplicitAttributeNamedArgsRector\ExplicitAttributeNamedArgsRectorTest |
| 21 | + */ |
| 22 | +final class ExplicitAttributeNamedArgsRector extends AbstractRector implements MinPhpVersionInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @readonly |
| 26 | + */ |
| 27 | + private ReflectionResolver $reflectionResolver; |
| 28 | + public function __construct(ReflectionResolver $reflectionResolver) |
| 29 | + { |
| 30 | + $this->reflectionResolver = $reflectionResolver; |
| 31 | + } |
| 32 | + public function getRuleDefinition(): RuleDefinition |
| 33 | + { |
| 34 | + return new RuleDefinition('Convert positional arguments on attributes into named arguments, using the attribute constructor parameter names', [new CodeSample(<<<'CODE_SAMPLE' |
| 35 | +#[SomeAttribute(SomeClass::class, null, ['home'])] |
| 36 | +class SomeClass |
| 37 | +{ |
| 38 | +} |
| 39 | +CODE_SAMPLE |
| 40 | +, <<<'CODE_SAMPLE' |
| 41 | +#[SomeAttribute(value: SomeClass::class, only: null, except: ['home'])] |
| 42 | +class SomeClass |
| 43 | +{ |
| 44 | +} |
| 45 | +CODE_SAMPLE |
| 46 | +)]); |
| 47 | + } |
| 48 | + /** |
| 49 | + * @return array<class-string<Node>> |
| 50 | + */ |
| 51 | + public function getNodeTypes(): array |
| 52 | + { |
| 53 | + return [Attribute::class]; |
| 54 | + } |
| 55 | + /** |
| 56 | + * @param Attribute $node |
| 57 | + */ |
| 58 | + public function refactor(Node $node): ?Node |
| 59 | + { |
| 60 | + $methodReflection = $this->reflectionResolver->resolveConstructorReflectionFromAttribute($node); |
| 61 | + if (!$methodReflection instanceof MethodReflection) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + $extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors($methodReflection->getVariants()); |
| 65 | + $parameters = $extendedParametersAcceptor->getParameters(); |
| 66 | + $namesToApply = $this->resolveArgNamesToApply($node->args, $parameters); |
| 67 | + if ($namesToApply === []) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + foreach ($namesToApply as $position => $name) { |
| 71 | + $node->args[$position]->name = new Identifier($name); |
| 72 | + } |
| 73 | + return $node; |
| 74 | + } |
| 75 | + public function provideMinPhpVersion(): int |
| 76 | + { |
| 77 | + return PhpVersionFeature::NAMED_ARGUMENTS; |
| 78 | + } |
| 79 | + /** |
| 80 | + * Resolve the positional arguments to name, as a position => parameter-name map, or [] when |
| 81 | + * nothing should change. Naming an argument forces every later positional argument to be named |
| 82 | + * too (PHP forbids a positional argument after a named one). So if any argument maps to a |
| 83 | + * variadic parameter, or to no parameter at all (overflow past a variadic), the whole attribute |
| 84 | + * is left untouched rather than producing invalid PHP. |
| 85 | + * |
| 86 | + * @param Arg[] $args |
| 87 | + * @param ParameterReflection[] $parameters |
| 88 | + * @return array<int, string> |
| 89 | + */ |
| 90 | + private function resolveArgNamesToApply(array $args, array $parameters): array |
| 91 | + { |
| 92 | + $namesToApply = []; |
| 93 | + foreach ($args as $position => $arg) { |
| 94 | + // already named |
| 95 | + if ($arg->name instanceof Identifier) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + $parameter = $parameters[$position] ?? null; |
| 99 | + // no matching parameter, e.g. overflow past a variadic |
| 100 | + if ($parameter === null) { |
| 101 | + return []; |
| 102 | + } |
| 103 | + // naming a variadic would rebind it or strand later positional arguments |
| 104 | + if ($parameter->isVariadic()) { |
| 105 | + return []; |
| 106 | + } |
| 107 | + $namesToApply[$position] = $parameter->getName(); |
| 108 | + } |
| 109 | + return $namesToApply; |
| 110 | + } |
| 111 | +} |
0 commit comments