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