|
14 | 14 | use Rector\Core\RectorDefinition\RectorDefinition; |
15 | 15 |
|
16 | 16 | /** |
17 | | - * Pass true to 3rd parameter of in_array when no value provided |
| 17 | + * Pass strict to function parameter on specific position argument when no value provided |
18 | 18 | */ |
19 | | -final class PassTrueToThirdParameterInArrayRector extends AbstractRector |
| 19 | +final class PassStrictParameterToFunctionParameterRector extends AbstractRector |
20 | 20 | { |
| 21 | + private const FUNCTION_WITH_ARG_POSITION = [ |
| 22 | + // position start from 0 |
| 23 | + 'in_array' => 2, |
| 24 | + ]; |
| 25 | + |
21 | 26 | public function getDefinition(): RectorDefinition |
22 | 27 | { |
23 | | - return new RectorDefinition('Pass true to 3rd parameter of in_array if no value provided', [ |
| 28 | + return new RectorDefinition('Pass strict to function parameter on specific position argument when no value provided', [ |
24 | 29 | new CodeSample( |
25 | 30 | <<<'CODE_SAMPLE' |
26 | 31 | in_array('a', $array); |
@@ -52,18 +57,23 @@ public function refactor(Node $node): ?Node |
52 | 57 | return null; |
53 | 58 | } |
54 | 59 |
|
55 | | - if ($name->toString() !== 'in_array') |
| 60 | + $functions = array_keys(self::FUNCTION_WITH_ARG_POSITION); |
| 61 | + $currentFunctionName = $name->toString(); |
| 62 | + |
| 63 | + if (! in_array($currentFunctionName, $functions, true)) |
56 | 64 | { |
57 | 65 | return null; |
58 | 66 | } |
59 | 67 |
|
60 | | - if (isset($node->args[2])) |
| 68 | + $position = $functions[$currentFunctionName]; |
| 69 | + |
| 70 | + if (isset($node->args[$position])) |
61 | 71 | { |
62 | 72 | return null; |
63 | 73 | } |
64 | 74 |
|
65 | | - $name = new Name('true'); |
66 | | - $node->args[2] = new Arg(new ConstFetch($name)); |
| 75 | + $name = new Name('true'); |
| 76 | + $node->args[$position] = new Arg(new ConstFetch($name)); |
67 | 77 |
|
68 | 78 | return $node; |
69 | 79 | } |
|
0 commit comments