|
5 | 5 | namespace Rector\CodeQuality\Rector\CallLike; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Arg; |
9 | 8 | use PhpParser\Node\Expr\CallLike; |
10 | | -use PhpParser\Node\Identifier; |
11 | | -use PHPStan\Reflection\FunctionReflection; |
12 | | -use PHPStan\Reflection\MethodReflection; |
13 | | -use PHPStan\Reflection\ParameterReflection; |
14 | | -use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper; |
| 9 | +use Rector\NodeAnalyzer\CallLikeArgumentNameAdder; |
15 | 10 | use Rector\PhpParser\Node\Value\ValueResolver; |
16 | | -use Rector\PHPStan\ScopeFetcher; |
17 | 11 | use Rector\Rector\AbstractRector; |
18 | | -use Rector\Reflection\ReflectionResolver; |
19 | 12 | use Rector\ValueObject\PhpVersionFeature; |
20 | 13 | use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
21 | 14 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
|
27 | 20 | final class AddNameToBooleanArgumentRector extends AbstractRector implements MinPhpVersionInterface |
28 | 21 | { |
29 | 22 | public function __construct( |
30 | | - private readonly ReflectionResolver $reflectionResolver, |
| 23 | + private readonly CallLikeArgumentNameAdder $callLikeArgumentNameAdder, |
31 | 24 | private readonly ValueResolver $valueResolver, |
32 | 25 | ) { |
33 | 26 | } |
@@ -63,148 +56,14 @@ public function getNodeTypes(): array |
63 | 56 | */ |
64 | 57 | public function refactor(Node $node): ?Node |
65 | 58 | { |
66 | | - if ($this->shouldSkip($node)) { |
67 | | - return null; |
68 | | - } |
69 | | - |
70 | | - $reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node); |
71 | | - if (! $reflection instanceof FunctionReflection && ! $reflection instanceof MethodReflection) { |
72 | | - return null; |
73 | | - } |
74 | | - |
75 | | - $scope = ScopeFetcher::fetch($node); |
76 | | - $args = $node->getArgs(); |
77 | | - $parameters = ParametersAcceptorSelectorVariantsWrapper::select($reflection, $node, $scope) |
78 | | - ->getParameters(); |
79 | | - |
80 | | - $position = $this->resolveFirstPositionToName($args, $parameters); |
81 | | - if ($position === null) { |
82 | | - return null; |
83 | | - } |
84 | | - |
85 | | - $wasChanged = false; |
86 | | - $counter = count($args); |
87 | | - for ($i = $position; $i < $counter; ++$i) { |
88 | | - $arg = $args[$i]; |
89 | | - if ($arg->name instanceof Identifier) { |
90 | | - continue; |
91 | | - } |
92 | | - |
93 | | - $parameterReflection = $this->resolveParameterReflection($arg, $i, $parameters); |
94 | | - if (! $parameterReflection instanceof ParameterReflection) { |
95 | | - return null; |
96 | | - } |
97 | | - |
98 | | - $arg->name = new Identifier($parameterReflection->getName()); |
99 | | - $wasChanged = true; |
100 | | - } |
101 | | - |
102 | | - if (! $wasChanged) { |
103 | | - return null; |
104 | | - } |
105 | | - |
106 | | - return $node; |
| 59 | + return $this->callLikeArgumentNameAdder->addNamesToArgs( |
| 60 | + $node, |
| 61 | + fn ($expr): bool => $this->valueResolver->isTrueOrFalse($expr), |
| 62 | + ); |
107 | 63 | } |
108 | 64 |
|
109 | 65 | public function provideMinPhpVersion(): int |
110 | 66 | { |
111 | 67 | return PhpVersionFeature::NAMED_ARGUMENTS; |
112 | 68 | } |
113 | | - |
114 | | - private function shouldSkip(CallLike $callLike): bool |
115 | | - { |
116 | | - if ($callLike->isFirstClassCallable()) { |
117 | | - return true; |
118 | | - } |
119 | | - |
120 | | - $args = $callLike->getArgs(); |
121 | | - if ($args === []) { |
122 | | - return true; |
123 | | - } |
124 | | - |
125 | | - foreach ($args as $arg) { |
126 | | - if ($arg->unpack) { |
127 | | - return true; |
128 | | - } |
129 | | - } |
130 | | - |
131 | | - return false; |
132 | | - } |
133 | | - |
134 | | - /** |
135 | | - * @param Arg[] $args |
136 | | - * @param ParameterReflection[] $parameters |
137 | | - */ |
138 | | - private function resolveFirstPositionToName(array $args, array $parameters): ?int |
139 | | - { |
140 | | - foreach ($args as $position => $arg) { |
141 | | - if ($arg->name instanceof Identifier) { |
142 | | - continue; |
143 | | - } |
144 | | - |
145 | | - if (! $this->valueResolver->isTrueOrFalse($arg->value)) { |
146 | | - continue; |
147 | | - } |
148 | | - |
149 | | - if ($this->canNameArgsFromPosition($args, $parameters, $position)) { |
150 | | - return $position; |
151 | | - } |
152 | | - } |
153 | | - |
154 | | - return null; |
155 | | - } |
156 | | - |
157 | | - /** |
158 | | - * @param Arg[] $args |
159 | | - * @param ParameterReflection[] $parameters |
160 | | - */ |
161 | | - private function canNameArgsFromPosition(array $args, array $parameters, int $position): bool |
162 | | - { |
163 | | - $count = count($args); |
164 | | - for ($i = $position; $i < $count; ++$i) { |
165 | | - $arg = $args[$i]; |
166 | | - if ($arg->name instanceof Identifier) { |
167 | | - continue; |
168 | | - } |
169 | | - |
170 | | - $parameterReflection = $this->resolveParameterReflection($arg, $i, $parameters); |
171 | | - if (! $parameterReflection instanceof ParameterReflection) { |
172 | | - return false; |
173 | | - } |
174 | | - |
175 | | - if ($parameterReflection->isVariadic()) { |
176 | | - return false; |
177 | | - } |
178 | | - } |
179 | | - |
180 | | - return true; |
181 | | - } |
182 | | - |
183 | | - /** |
184 | | - * @param ParameterReflection[] $parameters |
185 | | - */ |
186 | | - private function resolveParameterReflection(Arg $arg, int $position, array $parameters): ?ParameterReflection |
187 | | - { |
188 | | - if ($arg->name instanceof Identifier) { |
189 | | - foreach ($parameters as $parameter) { |
190 | | - if ($parameter->getName() === $arg->name->toString()) { |
191 | | - return $parameter; |
192 | | - } |
193 | | - } |
194 | | - |
195 | | - return null; |
196 | | - } |
197 | | - |
198 | | - $parameter = $parameters[$position] ?? null; |
199 | | - if ($parameter instanceof ParameterReflection) { |
200 | | - return $parameter; |
201 | | - } |
202 | | - |
203 | | - $lastParameter = end($parameters); |
204 | | - if ($lastParameter instanceof ParameterReflection && $lastParameter->isVariadic()) { |
205 | | - return $lastParameter; |
206 | | - } |
207 | | - |
208 | | - return null; |
209 | | - } |
210 | 69 | } |
0 commit comments