diff --git a/rules/Carbon/Rector/MethodCall/DateTimeMethodCallToCarbonRector.php b/rules/Carbon/Rector/MethodCall/DateTimeMethodCallToCarbonRector.php index ab95c35497f..c8ff7a17982 100644 --- a/rules/Carbon/Rector/MethodCall/DateTimeMethodCallToCarbonRector.php +++ b/rules/Carbon/Rector/MethodCall/DateTimeMethodCallToCarbonRector.php @@ -5,6 +5,7 @@ namespace Rector\Carbon\Rector\MethodCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Name; @@ -88,7 +89,11 @@ public function refactor(Node $node): ?Node return null; } - $firstArg = $new->getArgs()[0]; + $firstArg = $new->getArg('datetime', 0); + if (! $firstArg instanceof Arg) { + return null; + } + if (! $firstArg->value instanceof String_) { return null; } diff --git a/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php b/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php index f493b0dae68..3da3b3d10a2 100644 --- a/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php +++ b/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php @@ -5,6 +5,7 @@ namespace Rector\Carbon\Rector\New_; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\StaticCall; @@ -80,9 +81,9 @@ private function refactorWithClass(New_ $new, string $className): MethodCall|Sta } if (count($new->getArgs()) === 1) { - $firstArg = $new->getArgs()[0]; + $firstArg = $new->getArg('datetime', 0); - if ($firstArg->value instanceof String_) { + if ($firstArg instanceof Arg && $firstArg->value instanceof String_) { return $this->carbonCallFactory->createFromDateTimeString($carbonFullyQualified, $firstArg->value); } } diff --git a/rules/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php b/rules/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php index 7c10897a868..ed6bc1b5633 100644 --- a/rules/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php +++ b/rules/CodeQuality/Rector/BooleanAnd/SimplifyEmptyArrayCheckRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\BooleanAnd; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BinaryOp\Identical; @@ -57,8 +58,12 @@ public function refactor(Node $node): ?Node /** @var FuncCall $isArrayExpr */ $isArrayExpr = $twoNodeMatch->getFirstExpr(); - $firstArgValue = $isArrayExpr->getArgs()[0] - ->value; + $firstArg = $isArrayExpr->getArg('value', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + $firstArgValue = $firstArg->value; /** @var Empty_ $emptyOrNotIdenticalNode */ $emptyOrNotIdenticalNode = $twoNodeMatch->getSecondExpr(); @@ -96,12 +101,12 @@ function (Node $node): bool { return false; } - if (isset($node->getArgs()[0])) { - return $node->getArgs()[0] - ->value instanceof Variable; + $firstArg = $node->getArg('value', 0); + if (! $firstArg instanceof Arg) { + return false; } - return false; + return $firstArg->value instanceof Variable; }, // empty(...) function (Node $node): bool { diff --git a/rules/CodeQuality/Rector/Concat/DirnameDirConcatStringToDirectStringPathRector.php b/rules/CodeQuality/Rector/Concat/DirnameDirConcatStringToDirectStringPathRector.php index a7e6072ba6a..dec67f53fc4 100644 --- a/rules/CodeQuality/Rector/Concat/DirnameDirConcatStringToDirectStringPathRector.php +++ b/rules/CodeQuality/Rector/Concat/DirnameDirConcatStringToDirectStringPathRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\Concat; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\BinaryOp\Concat; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Scalar\MagicConst\Dir; @@ -74,7 +75,10 @@ public function refactor(Node $node): ?Concat return null; } - $firstArg = $dirnameFuncCall->getArgs()[0]; + $firstArg = $dirnameFuncCall->getArg('path', 0); + if (! $firstArg instanceof Arg) { + return null; + } if (! $firstArg->value instanceof Dir) { return null; } diff --git a/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php b/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php index 8495c853b75..6042ae9e60f 100644 --- a/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php +++ b/rules/CodeQuality/Rector/FuncCall/CompactToVariablesRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\FuncCall; @@ -86,7 +87,10 @@ public function refactor(Node $node): ?Node return null; } - $firstArg = $node->getArgs()[0]; + $firstArg = $node->getArg('var_name', 0); + if (! $firstArg instanceof Arg) { + return null; + } $firstValue = $firstArg->value; $firstValueStaticType = $this->getType($firstValue); diff --git a/rules/CodeQuality/Rector/FuncCall/IsAWithStringWithThirdArgumentRector.php b/rules/CodeQuality/Rector/FuncCall/IsAWithStringWithThirdArgumentRector.php index 9c0c2c82c45..93d96919fc0 100644 --- a/rules/CodeQuality/Rector/FuncCall/IsAWithStringWithThirdArgumentRector.php +++ b/rules/CodeQuality/Rector/FuncCall/IsAWithStringWithThirdArgumentRector.php @@ -67,11 +67,14 @@ public function refactor(Node $node): ?Node return null; } - if (isset($node->getArgs()[2])) { + if ($node->getArg('allow_string', 2) instanceof Arg) { return null; } - $firstArg = $node->getArgs()[0]; + $firstArg = $node->getArg('object_or_class', 0); + if (! $firstArg instanceof Arg) { + return null; + } $firstArgumentStaticType = $this->getType($firstArg->value); if (! $firstArgumentStaticType->isString()->yes()) { diff --git a/rules/CodeQuality/Rector/FuncCall/RemoveSoleValueSprintfRector.php b/rules/CodeQuality/Rector/FuncCall/RemoveSoleValueSprintfRector.php index e2d9516be85..ccf269f58c6 100644 --- a/rules/CodeQuality/Rector/FuncCall/RemoveSoleValueSprintfRector.php +++ b/rules/CodeQuality/Rector/FuncCall/RemoveSoleValueSprintfRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Scalar\String_; use Rector\Rector\AbstractRector; @@ -70,7 +71,10 @@ public function refactor(Node $node): ?Node return null; } - $firstArg = $node->getArgs()[0]; + $firstArg = $node->getArg('format', 0); + if (! $firstArg instanceof Arg) { + return null; + } $maskArgument = $firstArg->value; if (! $maskArgument instanceof String_) { return null; @@ -80,7 +84,10 @@ public function refactor(Node $node): ?Node return null; } - $secondArg = $node->getArgs()[1]; + $secondArg = $node->getArg('values', 1); + if (! $secondArg instanceof Arg) { + return null; + } $valueArgument = $secondArg->value; $valueType = $this->getType($valueArgument); diff --git a/rules/CodeQuality/Rector/FuncCall/SimplifyFuncGetArgsCountRector.php b/rules/CodeQuality/Rector/FuncCall/SimplifyFuncGetArgsCountRector.php index d5abdc22e76..87df327b6ba 100644 --- a/rules/CodeQuality/Rector/FuncCall/SimplifyFuncGetArgsCountRector.php +++ b/rules/CodeQuality/Rector/FuncCall/SimplifyFuncGetArgsCountRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; @@ -44,7 +45,10 @@ public function refactor(Node $node): ?Node return null; } - $firstArg = $node->getArgs()[0]; + $firstArg = $node->getArg('value', 0); + if (! $firstArg instanceof Arg) { + return null; + } if (! $firstArg->value instanceof FuncCall) { return null; diff --git a/rules/CodeQuality/Rector/FuncCall/SimplifyInArrayValuesRector.php b/rules/CodeQuality/Rector/FuncCall/SimplifyInArrayValuesRector.php index d3d65e1ddd5..59dfd23712a 100644 --- a/rules/CodeQuality/Rector/FuncCall/SimplifyInArrayValuesRector.php +++ b/rules/CodeQuality/Rector/FuncCall/SimplifyInArrayValuesRector.php @@ -62,11 +62,12 @@ public function refactor(Node $node): ?Node return null; } - if (! isset($node->getArgs()[0])) { + $arrayArg = $innerFunCall->getArg('array', 0); + if (! $arrayArg instanceof Arg) { return null; } - $node->args[1] = $innerFunCall->getArgs()[0]; + $node->args[1] = $arrayArg; return $node; } diff --git a/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php b/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php index 473c59ae096..14d13b1ca79 100644 --- a/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php +++ b/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php @@ -17,6 +17,7 @@ use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; +use Webmozart\Assert\Assert; /** * @see \Rector\Tests\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector\SingleInArrayToCompareRectorTest @@ -127,7 +128,7 @@ private function resolveArrayItem(FuncCall $funcCall): ?ArrayItem return null; } - if (! isset($funcCall->getArgs()[0])) { + if (! $funcCall->getArg('needle', 0) instanceof Arg) { return null; } @@ -138,10 +139,11 @@ private function processCompare(ArrayItem $firstArrayItem, FuncCall $funcCall, b { $firstArrayItemValue = $firstArrayItem->value; - $firstArg = $funcCall->getArgs()[0]; + $firstArg = $funcCall->getArg('needle', 0); + Assert::isInstanceOf($firstArg, Arg::class); // strict - if (isset($funcCall->getArgs()[2])) { + if ($funcCall->getArg('strict', 2) instanceof Arg) { return $isNegated ? new NotIdentical($firstArg->value, $firstArrayItemValue) : new Identical($firstArg->value, $firstArrayItemValue); diff --git a/rules/CodeQuality/Rector/FuncCall/UnwrapSprintfOneArgumentRector.php b/rules/CodeQuality/Rector/FuncCall/UnwrapSprintfOneArgumentRector.php index ee8530a894f..563204797ff 100644 --- a/rules/CodeQuality/Rector/FuncCall/UnwrapSprintfOneArgumentRector.php +++ b/rules/CodeQuality/Rector/FuncCall/UnwrapSprintfOneArgumentRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Scalar\String_; use Rector\Rector\AbstractRector; @@ -56,8 +57,9 @@ public function refactor(Node $node): ?Node return null; } - $firstArg = $node->getArgs()[0]; - if ($firstArg->unpack) { + // unpacked argument is not resolved by getArg() + $firstArg = $node->getArg('format', 0); + if (! $firstArg instanceof Arg) { return null; } diff --git a/rules/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php b/rules/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php index 2e46aa47b04..7a80ac769dd 100644 --- a/rules/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php +++ b/rules/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php @@ -5,6 +5,7 @@ namespace Rector\CodeQuality\Rector\Identical; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Greater; use PhpParser\Node\Expr\BinaryOp\Identical; @@ -99,8 +100,12 @@ public function refactor(Node $node): ?Node return null; } - $variable = $funcCall->getArgs()[0] - ->value; + $firstArg = $funcCall->getArg('string', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + $variable = $firstArg->value; // Needs string cast if variable type is not string // see https://github.com/rectorphp/rector/issues/6700 diff --git a/rules/CodeQuality/Rector/Ternary/TernaryImplodeToImplodeRector.php b/rules/CodeQuality/Rector/Ternary/TernaryImplodeToImplodeRector.php index 5b75fd0baef..17450691e15 100644 --- a/rules/CodeQuality/Rector/Ternary/TernaryImplodeToImplodeRector.php +++ b/rules/CodeQuality/Rector/Ternary/TernaryImplodeToImplodeRector.php @@ -97,7 +97,7 @@ public function refactor(Node $node): ?Node } $function = $node->else; - $secondArg = $function->getArgs()[1] ?? null; + $secondArg = $function->getArg('array', 1); if (! $secondArg instanceof Arg) { return null; diff --git a/rules/CodingStyle/Rector/FuncCall/CallUserFuncArrayToVariadicRector.php b/rules/CodingStyle/Rector/FuncCall/CallUserFuncArrayToVariadicRector.php index 5c1d982fb97..6d57799c06f 100644 --- a/rules/CodingStyle/Rector/FuncCall/CallUserFuncArrayToVariadicRector.php +++ b/rules/CodingStyle/Rector/FuncCall/CallUserFuncArrayToVariadicRector.php @@ -78,10 +78,18 @@ public function refactor(Node $node): ?Node return null; } - $firstArgValue = $node->getArgs()[0] - ->value; - $secondArgValue = $node->getArgs()[1] - ->value; + $firstArg = $node->getArg('callback', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + $secondArg = $node->getArg('args', 1); + if (! $secondArg instanceof Arg) { + return null; + } + + $firstArgValue = $firstArg->value; + $secondArgValue = $secondArg->value; if ($firstArgValue instanceof String_) { $functionName = $this->valueResolver->getValue($firstArgValue); diff --git a/rules/CodingStyle/Rector/FuncCall/CallUserFuncToMethodCallRector.php b/rules/CodingStyle/Rector/FuncCall/CallUserFuncToMethodCallRector.php index b67e15a342c..77d8dc57a0c 100644 --- a/rules/CodingStyle/Rector/FuncCall/CallUserFuncToMethodCallRector.php +++ b/rules/CodingStyle/Rector/FuncCall/CallUserFuncToMethodCallRector.php @@ -5,6 +5,7 @@ namespace Rector\CodingStyle\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; @@ -72,12 +73,12 @@ public function refactor(Node $node): ?Node return null; } - if (! isset($node->getArgs()[0])) { + $firstArg = $node->getArg('callback', 0); + if (! $firstArg instanceof Arg) { return null; } - $firstArgValue = $node->getArgs()[0] - ->value; + $firstArgValue = $firstArg->value; if (! $firstArgValue instanceof Array_) { return null; } diff --git a/rules/CodingStyle/Rector/FuncCall/ConsistentImplodeRector.php b/rules/CodingStyle/Rector/FuncCall/ConsistentImplodeRector.php index 07a8128bf3f..7a6da1a9bab 100644 --- a/rules/CodingStyle/Rector/FuncCall/ConsistentImplodeRector.php +++ b/rules/CodingStyle/Rector/FuncCall/ConsistentImplodeRector.php @@ -77,8 +77,13 @@ public function refactor(Node $node): ?Node } if (count($node->getArgs()) === 1) { + $arrayArg = $node->getArg('array', 0); + if (! $arrayArg instanceof Arg) { + return null; + } + // complete default value '' - $node->args[1] = $node->getArgs()[0]; + $node->args[1] = $arrayArg; $node->args[0] = new Arg(new String_('')); return $node; @@ -88,7 +93,10 @@ public function refactor(Node $node): ?Node return null; } - $firstArg = $node->getArgs()[0]; + $firstArg = $node->getArg('separator', 0); + if (! $firstArg instanceof Arg) { + return null; + } $firstArgumentValue = $firstArg->value; $firstArgumentType = $this->getType($firstArgumentValue); @@ -96,7 +104,10 @@ public function refactor(Node $node): ?Node return null; } - $secondArg = $node->getArgs()[1]; + $secondArg = $node->getArg('array', 1); + if (! $secondArg instanceof Arg) { + return null; + } if ($this->stringTypeAnalyzer->isStringOrUnionStringOnlyType($secondArg->value)) { $node->args = array_reverse($node->getArgs()); diff --git a/rules/DeadCode/NodeAnalyzer/PropertyWriteonlyAnalyzer.php b/rules/DeadCode/NodeAnalyzer/PropertyWriteonlyAnalyzer.php index 8b0bc41e746..70967253576 100644 --- a/rules/DeadCode/NodeAnalyzer/PropertyWriteonlyAnalyzer.php +++ b/rules/DeadCode/NodeAnalyzer/PropertyWriteonlyAnalyzer.php @@ -32,7 +32,7 @@ public function hasClassDynamicPropertyNames(Class_ $class): bool $node, 'get_object_vars' ) && ! $node->isFirstClassCallable()) { - $firstArg = $node->getArgs()[0] ?? null; + $firstArg = $node->getArg('object', 0); if ($firstArg instanceof Arg && $firstArg->value instanceof Variable && $firstArg->value->name === 'this') { return true; } diff --git a/rules/DeadCode/NodeManipulator/CountManipulator.php b/rules/DeadCode/NodeManipulator/CountManipulator.php index c6974e1758d..200ca8c690c 100644 --- a/rules/DeadCode/NodeManipulator/CountManipulator.php +++ b/rules/DeadCode/NodeManipulator/CountManipulator.php @@ -4,6 +4,7 @@ namespace Rector\DeadCode\NodeManipulator; +use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Greater; use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual; @@ -109,12 +110,12 @@ private function isCountWithExpression(Expr $node, Expr $expr): bool return false; } - if (! isset($node->getArgs()[0])) { + $firstArg = $node->getArg('value', 0); + if (! $firstArg instanceof Arg) { return false; } - $countedExpr = $node->getArgs()[0] - ->value; + $countedExpr = $firstArg->value; if ($this->nodeComparator->areNodesEqual($countedExpr, $expr)) { $exprType = $this->nodeTypeResolver->getNativeType($expr); diff --git a/rules/DeadCode/Rector/BinaryOp/RemoveRedundantTypeCheckRector.php b/rules/DeadCode/Rector/BinaryOp/RemoveRedundantTypeCheckRector.php index 72eee05b76b..284afbbe19d 100644 --- a/rules/DeadCode/Rector/BinaryOp/RemoveRedundantTypeCheckRector.php +++ b/rules/DeadCode/Rector/BinaryOp/RemoveRedundantTypeCheckRector.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\BinaryOp; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\Expr\BinaryOp\BooleanOr; @@ -164,7 +165,12 @@ private function refactorBooleanAnd(BooleanAnd $booleanAnd): ?Expr } // the type is already narrowed by the truthy check on the left - $checkedType = $this->getNativeType($funcCall->getArgs()[0]->value); + $firstArg = $funcCall->getArg('value', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + $checkedType = $this->getNativeType($firstArg->value); if (! $this->isAlwaysMatchingType($funcCallName, $checkedType)) { return null; } @@ -189,7 +195,12 @@ private function matchTypeCheckFuncCall(Expr $expr, Expr $expectedExpr): ?FuncCa return null; } - if (! $this->nodeComparator->areNodesEqual($expr->getArgs()[0]->value, $expectedExpr)) { + $firstArg = $expr->getArg('value', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + if (! $this->nodeComparator->areNodesEqual($firstArg->value, $expectedExpr)) { return null; } diff --git a/rules/DeadCode/Rector/FuncCall/RemoveFilterVarOnExactTypeRector.php b/rules/DeadCode/Rector/FuncCall/RemoveFilterVarOnExactTypeRector.php index ce1bb384f2f..63a56a849ab 100644 --- a/rules/DeadCode/Rector/FuncCall/RemoveFilterVarOnExactTypeRector.php +++ b/rules/DeadCode/Rector/FuncCall/RemoveFilterVarOnExactTypeRector.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use Rector\Rector\AbstractRector; @@ -61,10 +62,18 @@ public function refactor(Node $node): ?Node return null; } - $firstArgValue = $node->getArgs()[0] - ->value; - $secondArgValue = $node->getArgs()[1] - ->value; + $firstArg = $node->getArg('value', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + $secondArg = $node->getArg('filter', 1); + if (! $secondArg instanceof Arg) { + return null; + } + + $firstArgValue = $firstArg->value; + $secondArgValue = $secondArg->value; if (! $secondArgValue instanceof ConstFetch) { return null; diff --git a/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php b/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php index 52ec85ca149..d2681f2b3c6 100644 --- a/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php +++ b/rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php @@ -5,6 +5,7 @@ namespace Rector\DeadCode\Rector\Node; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\FuncCall; @@ -121,7 +122,12 @@ public function refactor(Node $node): ?Node $stmt->expr, 'extract' ) && ! $stmt->expr->isFirstClassCallable()) { - $appendExtractValues = $this->valueResolver->getValue($stmt->expr->getArgs()[0]->value); + $extractArg = $stmt->expr->getArg('array', 0); + if (! $extractArg instanceof Arg) { + break; + } + + $appendExtractValues = $this->valueResolver->getValue($extractArg->value); if (! is_array($appendExtractValues)) { // nothing can do as value is dynamic break; diff --git a/rules/DeadCode/Rector/StmtsAwareInterface/RemoveDeadInstanceOfAssertRector.php b/rules/DeadCode/Rector/StmtsAwareInterface/RemoveDeadInstanceOfAssertRector.php index 3df8ac5cc37..4a81eafcc73 100644 --- a/rules/DeadCode/Rector/StmtsAwareInterface/RemoveDeadInstanceOfAssertRector.php +++ b/rules/DeadCode/Rector/StmtsAwareInterface/RemoveDeadInstanceOfAssertRector.php @@ -6,6 +6,7 @@ use PhpParser\Comment\Doc; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Instanceof_; @@ -131,8 +132,12 @@ private function isDeadInstanceOfAssert(Expr $expr): bool return false; } - $instanceof = $expr->getArgs()[0] - ->value; + $firstArg = $expr->getArg('assertion', 0); + if (! $firstArg instanceof Arg) { + return false; + } + + $instanceof = $firstArg->value; if (! $instanceof instanceof Instanceof_) { return false; } diff --git a/rules/Php53/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php b/rules/Php53/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php index d3e50236969..7993955c1fe 100644 --- a/rules/Php53/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php +++ b/rules/Php53/Rector/FuncCall/DirNameFileConstantToDirConstantRector.php @@ -5,6 +5,7 @@ namespace Rector\Php53\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Scalar\MagicConst\Dir; use PhpParser\Node\Scalar\MagicConst\File; @@ -71,12 +72,11 @@ public function refactor(Node $node): ?Node return null; } - if (! isset($node->getArgs()[0])) { + $firstArg = $node->getArg('path', 0); + if (! $firstArg instanceof Arg) { return null; } - $firstArg = $node->getArgs()[0]; - if (! $firstArg->value instanceof File) { return null; } diff --git a/rules/Php55/Rector/FuncCall/PregReplaceEModifierRector.php b/rules/Php55/Rector/FuncCall/PregReplaceEModifierRector.php index 431c95fc820..8f0c46229b8 100644 --- a/rules/Php55/Rector/FuncCall/PregReplaceEModifierRector.php +++ b/rules/Php55/Rector/FuncCall/PregReplaceEModifierRector.php @@ -91,7 +91,10 @@ public function refactor(Node $node): ?Node return null; } - $firstArgument = $node->getArgs()[0]; + $firstArgument = $node->getArg('pattern', 0); + if (! $firstArgument instanceof Arg) { + return null; + } $firstArgumentValue = $firstArgument->value; $patternWithoutEExpr = $this->regexMatcher->resolvePatternExpressionWithoutEIfFound($firstArgumentValue); @@ -99,7 +102,10 @@ public function refactor(Node $node): ?Node return null; } - $secondArgument = $node->getArgs()[1]; + $secondArgument = $node->getArg('replacement', 1); + if (! $secondArgument instanceof Arg) { + return null; + } $anonymousFunction = $this->createAnonymousFunction($secondArgument); if (! $anonymousFunction instanceof Closure) { diff --git a/rules/Php70/Rector/FuncCall/EregToPregMatchRector.php b/rules/Php70/Rector/FuncCall/EregToPregMatchRector.php index f98fcab1fc3..d990951a928 100644 --- a/rules/Php70/Rector/FuncCall/EregToPregMatchRector.php +++ b/rules/Php70/Rector/FuncCall/EregToPregMatchRector.php @@ -103,7 +103,7 @@ private function shouldSkipFuncCall(FuncCall $funcCall): bool return true; } - return ! isset($funcCall->getArgs()[0]); + return ! $funcCall->getArg('pattern', 0) instanceof Arg; } private function processStringPattern(FuncCall $funcCall, String_ $string, string $functionName): void @@ -111,7 +111,8 @@ private function processStringPattern(FuncCall $funcCall, String_ $string, strin $pattern = $string->value; $pattern = $this->eregToPcreTransformer->transform($pattern, $this->isCaseInsensitiveFunction($functionName)); - $firstArg = $funcCall->getArgs()[0]; + $firstArg = $funcCall->getArg('pattern', 0); + Assert::isInstanceOf($firstArg, Arg::class); Assert::isInstanceOf($firstArg->value, String_::class); $firstArg->value->value = $pattern; } @@ -168,7 +169,8 @@ private function processSplitLimitArgument(FuncCall $funcCall, string $functionN private function createTernaryWithStrlenOfFirstMatch(FuncCall $funcCall): Ternary { - $thirdArg = $funcCall->getArgs()[2]; + $thirdArg = $funcCall->getArg('regs', 2); + Assert::isInstanceOf($thirdArg, Arg::class); $arrayDimFetch = new ArrayDimFetch($thirdArg->value, new Int_(0)); $strlenFuncCall = $this->nodeFactory->createFuncCall('strlen', [$arrayDimFetch]); @@ -200,7 +202,7 @@ private function isEregFuncCallWithThreeArgs(Expr $expr): bool return false; } - return isset($expr->getArgs()[2]); + return $expr->getArg('regs', 2) instanceof Arg; } private function refactorFuncCall(FuncCall $funcCall): ?FuncCall @@ -212,7 +214,11 @@ private function refactorFuncCall(FuncCall $funcCall): ?FuncCall /** @var string $functionName */ $functionName = $this->getName($funcCall); - $firstArg = $funcCall->getArgs()[0]; + $firstArg = $funcCall->getArg('pattern', 0); + if (! $firstArg instanceof Arg) { + return null; + } + $patternExpr = $firstArg->value; if ($patternExpr instanceof String_) { diff --git a/rules/Php71/IsArrayAndDualCheckToAble.php b/rules/Php71/IsArrayAndDualCheckToAble.php index 774860d96d1..492e1a45558 100644 --- a/rules/Php71/IsArrayAndDualCheckToAble.php +++ b/rules/Php71/IsArrayAndDualCheckToAble.php @@ -64,12 +64,11 @@ public function processBooleanOr(BooleanOr $booleanOr, string $type, string $new return null; } - if (! isset($funcCallExpr->getArgs()[0])) { + $firstArg = $funcCallExpr->getArg('value', 0); + if (! $firstArg instanceof Arg) { return null; } - $firstArg = $funcCallExpr->getArgs()[0]; - $firstExprNode = $firstArg->value; if (! $this->nodeComparator->areNodesEqual($instanceofExpr->expr, $firstExprNode)) { return null; diff --git a/rules/Php72/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php b/rules/Php72/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php index 7d471e818ff..ff712d8fded 100644 --- a/rules/Php72/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php +++ b/rules/Php72/Rector/Assign/ReplaceEachAssignmentWithKeyCurrentRector.php @@ -86,13 +86,13 @@ public function refactor(Node $node): ?array return null; } - if (! isset($eachFuncCall->getArgs()[0])) { + $firstArg = $eachFuncCall->getArg('array', 0); + if (! $firstArg instanceof Arg) { return null; } $assignVariable = $assign->var; - $eachedVariable = $eachFuncCall->getArgs()[0] - ->value; + $eachedVariable = $firstArg->value; return $this->createNewStmts($assignVariable, $eachedVariable); } diff --git a/rules/Php72/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php b/rules/Php72/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php index c9c8f3129b8..a35f97c0ade 100644 --- a/rules/Php72/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php +++ b/rules/Php72/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php @@ -102,10 +102,18 @@ public function refactor(Node $node): ?Node return null; } - $firstExpr = $node->getArgs()[0] - ->value; - $secondExpr = $node->getArgs()[1] - ->value; + $firstArg = $node->getArg('args', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + $secondArg = $node->getArg('code', 1); + if (! $secondArg instanceof Arg) { + return null; + } + + $firstExpr = $firstArg->value; + $secondExpr = $secondArg->value; $params = $this->createParamsFromString($firstExpr); $stmts = $this->parseStringToBody($secondExpr); diff --git a/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php b/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php index 609a936308f..a837817c203 100644 --- a/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php +++ b/rules/Php72/Rector/FuncCall/GetClassOnNullRector.php @@ -94,7 +94,7 @@ public function refactor(Node $node): ?Node return null; } - $firstArg = $node->getArgs()[0] ?? null; + $firstArg = $node->getArg('object', 0); if (! $firstArg instanceof Arg) { return null; } diff --git a/rules/Php72/Rector/FuncCall/StringifyDefineRector.php b/rules/Php72/Rector/FuncCall/StringifyDefineRector.php index 0d3d9a27d14..5d4cf99a079 100644 --- a/rules/Php72/Rector/FuncCall/StringifyDefineRector.php +++ b/rules/Php72/Rector/FuncCall/StringifyDefineRector.php @@ -5,6 +5,7 @@ namespace Rector\Php72\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Scalar\String_; @@ -80,12 +81,11 @@ public function refactor(Node $node): ?Node return null; } - if (! isset($node->getArgs()[0])) { + $firstArg = $node->getArg('constant_name', 0); + if (! $firstArg instanceof Arg) { return null; } - $firstArg = $node->getArgs()[0]; - if ($this->stringTypeAnalyzer->isStringOrUnionStringOnlyType($firstArg->value)) { return null; } diff --git a/rules/Php72/Rector/FuncCall/StringsAssertNakedRector.php b/rules/Php72/Rector/FuncCall/StringsAssertNakedRector.php index 7dd37df3e6f..0475c93c292 100644 --- a/rules/Php72/Rector/FuncCall/StringsAssertNakedRector.php +++ b/rules/Php72/Rector/FuncCall/StringsAssertNakedRector.php @@ -78,7 +78,10 @@ public function refactor(Node $node): ?Node return null; } - $firstArg = $node->getArgs()[0]; + $firstArg = $node->getArg('assertion', 0); + if (! $firstArg instanceof Arg) { + return null; + } $firstArgValue = $firstArg->value; if (! $firstArgValue instanceof String_) { diff --git a/rules/Php72/Rector/While_/WhileEachToForeachRector.php b/rules/Php72/Rector/While_/WhileEachToForeachRector.php index 911d884ab6f..75a339e9c4c 100644 --- a/rules/Php72/Rector/While_/WhileEachToForeachRector.php +++ b/rules/Php72/Rector/While_/WhileEachToForeachRector.php @@ -5,6 +5,7 @@ namespace Rector\Php72\Rector\While_; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Stmt\Foreach_; @@ -92,12 +93,11 @@ public function refactor(Node $node): ?Node $eachFuncCall = $listAndEach->getEachFuncCall(); $list = $listAndEach->getList(); - if (! isset($eachFuncCall->getArgs()[0])) { + $firstArg = $eachFuncCall->getArg('array', 0); + if (! $firstArg instanceof Arg) { return null; } - $firstArg = $eachFuncCall->getArgs()[0]; - $foreachedExpr = count($list->items) === 1 ? $this->nodeFactory->createFuncCall( 'array_keys', [$firstArg] diff --git a/rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php b/rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php index 4b0cb568387..f8a01a3ebfd 100644 --- a/rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php +++ b/rules/Php73/Rector/FuncCall/ArrayKeyFirstLastRector.php @@ -5,6 +5,7 @@ namespace Rector\Php73\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; use PhpParser\Node\Stmt; @@ -217,7 +218,13 @@ private function resolveKeyFuncCall(Stmt $nextStmt, FuncCall $resetOrEndFuncCall return false; } - return $this->nodeComparator->areNodesEqual($resetOrEndFuncCall->getArgs()[0], $subNode->getArgs()[0]); + $resetOrEndArg = $resetOrEndFuncCall->getArg('array', 0); + $keyArg = $subNode->getArg('array', 0); + if (! $resetOrEndArg instanceof Arg || ! $keyArg instanceof Arg) { + return false; + } + + return $this->nodeComparator->areNodesEqual($resetOrEndArg, $keyArg); }); } @@ -250,12 +257,13 @@ function (Node $subNode) use ($funcCall): bool { return true; } - return $this->nodeComparator->areNodesEqual( - $subNode->getArgs()[0] - ->value, - $funcCall->getArgs()[0] - ->value - ); + $subNodeArg = $subNode->getArg('array', 0); + $funcCallArg = $funcCall->getArg('array', 0); + if (! $subNodeArg instanceof Arg || ! $funcCallArg instanceof Arg) { + return true; + } + + return $this->nodeComparator->areNodesEqual($subNodeArg->value, $funcCallArg->value); } ); diff --git a/rules/Php74/Rector/If_/IfToNullCoalescingAssignRector.php b/rules/Php74/Rector/If_/IfToNullCoalescingAssignRector.php index f44dbbc560c..a2aca6b4684 100644 --- a/rules/Php74/Rector/If_/IfToNullCoalescingAssignRector.php +++ b/rules/Php74/Rector/If_/IfToNullCoalescingAssignRector.php @@ -170,8 +170,7 @@ private function matchNullGuardedExpr(Expr $expr): ?Expr return null; } - return $expr->getArgs()[0] - ->value; + return $expr->getArg('value', 0)?->value; } // null === $value or $value === null diff --git a/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php b/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php index 4fc39d6beba..fc262ad0167 100644 --- a/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php +++ b/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php @@ -80,7 +80,7 @@ public function refactor(Node $node): ?Node return null; } - $firstArg = $node->getArgs()[0] ?? null; + $firstArg = $node->getArg('name', 0); if (! $firstArg instanceof Arg) { return null; } @@ -92,7 +92,7 @@ public function refactor(Node $node): ?Node $new = new New_($node->class, [new Arg($firstArg->value)]); - $secondArg = $node->getArgs()[1] ?? null; + $secondArg = $node->getArg('return', 1); if (! $secondArg instanceof Arg) { return $new; } diff --git a/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrncmpMatchAndRefactor.php b/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrncmpMatchAndRefactor.php index 52ccdefc4af..a67729a6c73 100644 --- a/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrncmpMatchAndRefactor.php +++ b/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrncmpMatchAndRefactor.php @@ -5,6 +5,7 @@ namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\BinaryOp\NotEqual; @@ -79,7 +80,10 @@ private function isNeedleExprWithStrlen(StrStartsWith $strStartsWith): bool return false; } - $thirdArg = $strncmpFuncCall->getArgs()[2]; + $thirdArg = $strncmpFuncCall->getArg('length', 2); + if (! $thirdArg instanceof Arg) { + return false; + } $thirdArgExpr = $thirdArg->value; if (! $thirdArgExpr instanceof FuncCall) { @@ -91,8 +95,12 @@ private function isNeedleExprWithStrlen(StrStartsWith $strStartsWith): bool } $strlenFuncCall = $thirdArgExpr; - $strlenExpr = $strlenFuncCall->getArgs()[0] - ->value; + $strlenArg = $strlenFuncCall->getArg('string', 0); + if (! $strlenArg instanceof Arg) { + return false; + } + + $strlenExpr = $strlenArg->value; return $this->nodeComparator->areNodesEqual($needleExpr, $strlenExpr); } @@ -105,14 +113,22 @@ private function isHardcodedStringWithLNumberLength(StrStartsWith $strStartsWith return false; } - $hardcodedStringNeedle = $strncmpFuncCall->getArgs()[1] - ->value; + $needleArg = $strncmpFuncCall->getArg('string2', 1); + if (! $needleArg instanceof Arg) { + return false; + } + + $hardcodedStringNeedle = $needleArg->value; if (! $hardcodedStringNeedle instanceof String_) { return false; } - $lNumberLength = $strncmpFuncCall->getArgs()[2] - ->value; + $lengthArg = $strncmpFuncCall->getArg('length', 2); + if (! $lengthArg instanceof Arg) { + return false; + } + + $lNumberLength = $lengthArg->value; if (! $lNumberLength instanceof Int_) { return false; } diff --git a/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrposMatchAndRefactor.php b/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrposMatchAndRefactor.php index 55912c58310..1206da96806 100644 --- a/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrposMatchAndRefactor.php +++ b/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/StrposMatchAndRefactor.php @@ -5,6 +5,7 @@ namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\BinaryOp; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; @@ -75,10 +76,18 @@ private function processBinaryOpLeft(BinaryOp $binaryOp, bool $isPositive): ?Str return null; } - $haystack = $funcCall->getArgs()[0] - ->value; - $needle = $funcCall->getArgs()[1] - ->value; + $haystackArg = $funcCall->getArg('haystack', 0); + if (! $haystackArg instanceof Arg) { + return null; + } + + $needleArg = $funcCall->getArg('needle', 1); + if (! $needleArg instanceof Arg) { + return null; + } + + $haystack = $haystackArg->value; + $needle = $needleArg->value; return new StrStartsWith($funcCall, $haystack, $needle, $isPositive); } @@ -95,10 +104,18 @@ private function processBinaryOpRight(BinaryOp $binaryOp, bool $isPositive): ?St return null; } - $haystack = $funcCall->getArgs()[0] - ->value; - $needle = $funcCall->getArgs()[1] - ->value; + $haystackArg = $funcCall->getArg('haystack', 0); + if (! $haystackArg instanceof Arg) { + return null; + } + + $needleArg = $funcCall->getArg('needle', 1); + if (! $needleArg instanceof Arg) { + return null; + } + + $haystack = $haystackArg->value; + $needle = $needleArg->value; return new StrStartsWith($funcCall, $haystack, $needle, $isPositive); } diff --git a/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/SubstrMatchAndRefactor.php b/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/SubstrMatchAndRefactor.php index 344f24d4268..fc6a6e38508 100644 --- a/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/SubstrMatchAndRefactor.php +++ b/rules/Php80/MatchAndRefactor/StrStartsWithMatchAndRefactor/SubstrMatchAndRefactor.php @@ -5,6 +5,7 @@ namespace Rector\Php80\MatchAndRefactor\StrStartsWithMatchAndRefactor; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\BinaryOp\Equal; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\BinaryOp\NotEqual; @@ -36,18 +37,23 @@ public function match(Identical|NotIdentical|Equal|NotEqual $binaryOp): ?StrStar if ($binaryOp->left instanceof FuncCall && $this->nodeNameResolver->isName($binaryOp->left, 'substr')) { $funcCall = $binaryOp->left; - $haystack = $funcCall->getArgs()[0] - ->value; + $stringArg = $funcCall->getArg('string', 0); + if (! $stringArg instanceof Arg) { + return null; + } - return new StrStartsWith($funcCall, $haystack, $binaryOp->right, $isPositive); + return new StrStartsWith($funcCall, $stringArg->value, $binaryOp->right, $isPositive); } if ($binaryOp->right instanceof FuncCall && $this->nodeNameResolver->isName($binaryOp->right, 'substr')) { $funcCall = $binaryOp->right; - $haystack = $funcCall->getArgs()[0] - ->value; - return new StrStartsWith($funcCall, $haystack, $binaryOp->left, $isPositive); + $stringArg = $funcCall->getArg('string', 0); + if (! $stringArg instanceof Arg) { + return null; + } + + return new StrStartsWith($funcCall, $stringArg->value, $binaryOp->left, $isPositive); } return null; @@ -74,13 +80,20 @@ private function isStrlenWithNeedleExpr(StrStartsWith $strStartsWith): bool return false; } - $firstArg = $substrFuncCall->getArgs()[1]; + $firstArg = $substrFuncCall->getArg('offset', 1); + if (! $firstArg instanceof Arg) { + return false; + } if (! $this->valueResolver->isValue($firstArg->value, 0)) { return false; } - $secondFuncCallArgValue = $substrFuncCall->getArgs()[2] - ->value; + $lengthArg = $substrFuncCall->getArg('length', 2); + if (! $lengthArg instanceof Arg) { + return false; + } + + $secondFuncCallArgValue = $lengthArg->value; if (! $secondFuncCallArgValue instanceof FuncCall) { return false; } @@ -90,8 +103,12 @@ private function isStrlenWithNeedleExpr(StrStartsWith $strStartsWith): bool } $strlenFuncCall = $secondFuncCallArgValue; - $needleExpr = $strlenFuncCall->getArgs()[0] - ->value; + $strlenArg = $strlenFuncCall->getArg('string', 0); + if (! $strlenArg instanceof Arg) { + return false; + } + + $needleExpr = $strlenArg->value; $comparedNeedleExpr = $strStartsWith->getNeedleExpr(); return $this->nodeComparator->areNodesEqual($needleExpr, $comparedNeedleExpr); @@ -105,7 +122,10 @@ private function isHardcodedStringWithLNumberLength(StrStartsWith $strStartsWith return false; } - $secondArg = $substrFuncCall->getArgs()[1]; + $secondArg = $substrFuncCall->getArg('offset', 1); + if (! $secondArg instanceof Arg) { + return false; + } if (! $this->valueResolver->isValue($secondArg->value, 0)) { return false; } @@ -119,8 +139,12 @@ private function isHardcodedStringWithLNumberLength(StrStartsWith $strStartsWith return false; } - $lNumberLength = $substrFuncCall->getArgs()[2] - ->value; + $lengthArg = $substrFuncCall->getArg('length', 2); + if (! $lengthArg instanceof Arg) { + return false; + } + + $lNumberLength = $lengthArg->value; if (! $lNumberLength instanceof Int_) { return false; } diff --git a/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php b/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php index 6f2677a6865..73813b6f616 100644 --- a/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php +++ b/rules/Php80/Rector/FuncCall/ClassOnObjectRector.php @@ -5,6 +5,7 @@ namespace Rector\Php80\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; @@ -70,13 +71,16 @@ public function refactor(Node $node): ?Node return null; } - if (! isset($node->getArgs()[0])) { + if ($node->getArgs() === []) { return new ClassConstFetch(new Name('self'), 'class'); } - $object = $node->getArgs()[0] - ->value; - return new ClassConstFetch($object, 'class'); + $firstArg = $node->getArg('object', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + return new ClassConstFetch($firstArg->value, 'class'); } public function provideMinPhpVersion(): int diff --git a/rules/Php80/Rector/FuncCall/GetHeadersArgumentFormatRector.php b/rules/Php80/Rector/FuncCall/GetHeadersArgumentFormatRector.php index acab3eadce0..d0d85f7afd0 100644 --- a/rules/Php80/Rector/FuncCall/GetHeadersArgumentFormatRector.php +++ b/rules/Php80/Rector/FuncCall/GetHeadersArgumentFormatRector.php @@ -60,7 +60,7 @@ public function refactor(Node $node): ?FuncCall return null; } - $formatArg = $node->getArgs()[1] ?? null; + $formatArg = $node->getArg('associative', 1); if (! $formatArg instanceof Arg) { return null; } diff --git a/rules/Php80/Rector/FuncCall/VersionCompareFuncCallArgumentRector.php b/rules/Php80/Rector/FuncCall/VersionCompareFuncCallArgumentRector.php index e19b69a1e22..497eac081e7 100644 --- a/rules/Php80/Rector/FuncCall/VersionCompareFuncCallArgumentRector.php +++ b/rules/Php80/Rector/FuncCall/VersionCompareFuncCallArgumentRector.php @@ -74,7 +74,7 @@ public function refactor(Node $node): ?FuncCall return null; } - $operatorArg = $node->getArgs()[2] ?? null; + $operatorArg = $node->getArg('operator', 2); if (! $operatorArg instanceof Arg) { return null; } diff --git a/rules/Php80/Rector/Identical/StrEndsWithRector.php b/rules/Php80/Rector/Identical/StrEndsWithRector.php index 9ef68a54e5a..77049d433ff 100644 --- a/rules/Php80/Rector/Identical/StrEndsWithRector.php +++ b/rules/Php80/Rector/Identical/StrEndsWithRector.php @@ -146,8 +146,12 @@ private function refactorSubstr(BinaryOp $binaryOp): FuncCall|BooleanNot|null return null; } - $needle = $substrFuncCall->getArgs()[1] - ->value; + $offsetArg = $substrFuncCall->getArg('offset', 1); + if (! $offsetArg instanceof Arg) { + return null; + } + + $needle = $offsetArg->value; if ( ! $this->isUnaryMinusStrlenFuncCallArgValue($needle, $comparedNeedleExpr) && ! $this->isHardCodedLNumberAndString($needle, $comparedNeedleExpr) @@ -155,8 +159,12 @@ private function refactorSubstr(BinaryOp $binaryOp): FuncCall|BooleanNot|null return null; } - $haystack = $substrFuncCall->getArgs()[0] - ->value; + $stringArg = $substrFuncCall->getArg('string', 0); + if (! $stringArg instanceof Arg) { + return null; + } + + $haystack = $stringArg->value; $isPositive = $binaryOp instanceof Identical || $binaryOp instanceof Equal; @@ -222,15 +230,12 @@ private function isUnaryMinusStrlenFuncCallArgValue(Expr $substrOffset, Expr $ne return false; } - if (! isset($funcCall->getArgs()[0])) { - return false; - } - - if (! $funcCall->args[0] instanceof Arg) { + $firstArg = $funcCall->getArg('string', 0); + if (! $firstArg instanceof Arg) { return false; } - return $this->nodeComparator->areNodesEqual($funcCall->args[0]->value, $needle); + return $this->nodeComparator->areNodesEqual($firstArg->value, $needle); } private function isHardCodedLNumberAndString(Expr $substrOffset, Expr $needle): bool diff --git a/rules/Php80/Rector/Ternary/GetDebugTypeRector.php b/rules/Php80/Rector/Ternary/GetDebugTypeRector.php index 6895186fdc0..cf46764b56d 100644 --- a/rules/Php80/Rector/Ternary/GetDebugTypeRector.php +++ b/rules/Php80/Rector/Ternary/GetDebugTypeRector.php @@ -82,10 +82,16 @@ public function refactor(Node $node): ?Node /** @var FuncCall|ClassConstFetch $getClassFuncCallOrClassConstFetchClass */ $getClassFuncCallOrClassConstFetchClass = $node->if; - $firstExpr = $getClassFuncCallOrClassConstFetchClass instanceof FuncCall - ? $getClassFuncCallOrClassConstFetchClass->getArgs()[0] - ->value - : $getClassFuncCallOrClassConstFetchClass->class; + if ($getClassFuncCallOrClassConstFetchClass instanceof FuncCall) { + $getClassArg = $getClassFuncCallOrClassConstFetchClass->getArg('object', 0); + if (! $getClassArg instanceof Arg) { + return null; + } + + $firstExpr = $getClassArg->value; + } else { + $firstExpr = $getClassFuncCallOrClassConstFetchClass->class; + } return $this->nodeFactory->createFuncCall('get_debug_type', [$firstExpr]); } @@ -105,7 +111,7 @@ private function shouldSkip(Ternary $ternary): bool return true; } - if (! isset($ternary->cond->getArgs()[0])) { + if (! $ternary->cond->getArg('value', 0) instanceof Arg) { return true; } @@ -154,20 +160,26 @@ private function areValuesIdentical(Ternary $ternary): bool return false; } - $firstExpr = $isObjectFuncCall->getArgs()[0] - ->value; + $firstArg = $isObjectFuncCall->getArg('value', 0); + if (! $firstArg instanceof Arg) { + return false; + } + + $firstExpr = $firstArg->value; /** @var FuncCall|ClassConstFetch $getClassFuncCallOrClassConstFetchClass */ $getClassFuncCallOrClassConstFetchClass = $ternary->if; - if ($getClassFuncCallOrClassConstFetchClass instanceof FuncCall && ! $getClassFuncCallOrClassConstFetchClass->args[0] instanceof Arg) { - return false; - } + if ($getClassFuncCallOrClassConstFetchClass instanceof FuncCall) { + $getClassArg = $getClassFuncCallOrClassConstFetchClass->getArg('object', 0); + if (! $getClassArg instanceof Arg) { + return false; + } - $secondExpr = $getClassFuncCallOrClassConstFetchClass instanceof FuncCall - ? $getClassFuncCallOrClassConstFetchClass->getArgs()[0] - ->value - : $getClassFuncCallOrClassConstFetchClass->class; + $secondExpr = $getClassArg->value; + } else { + $secondExpr = $getClassFuncCallOrClassConstFetchClass->class; + } $gettypeFuncCall = $ternary->else; diff --git a/rules/Php80/ValueObjectFactory/StrStartsWithFactory.php b/rules/Php80/ValueObjectFactory/StrStartsWithFactory.php index 5bc825f14ce..1daac53723c 100644 --- a/rules/Php80/ValueObjectFactory/StrStartsWithFactory.php +++ b/rules/Php80/ValueObjectFactory/StrStartsWithFactory.php @@ -4,6 +4,7 @@ namespace Rector\Php80\ValueObjectFactory; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use Rector\Php80\ValueObject\StrStartsWith; @@ -19,10 +20,18 @@ public function createFromFuncCall(FuncCall $funcCall, bool $isPositive): ?StrSt return null; } - $haystack = $funcCall->getArgs()[0] - ->value; - $needle = $funcCall->getArgs()[1] - ->value; + $firstArg = $funcCall->getArg('string1', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + $secondArg = $funcCall->getArg('string2', 1); + if (! $secondArg instanceof Arg) { + return null; + } + + $haystack = $firstArg->value; + $needle = $secondArg->value; return new StrStartsWith($funcCall, $haystack, $needle, $isPositive); } diff --git a/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php b/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php index f5089c0729b..06946a3b6e5 100644 --- a/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php +++ b/rules/Php81/Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.php @@ -176,7 +176,7 @@ private function refactorEqualsMethodCall(MethodCall $methodCall): ?Identical } } - $arg = $methodCall->getArgs()[0] ?? null; + $arg = $methodCall->getArg('variable', 0); if (! $arg instanceof Arg) { return null; } diff --git a/rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php b/rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php index ab1701f4b18..9b060045b00 100644 --- a/rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php +++ b/rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php @@ -66,8 +66,12 @@ public function refactor(Node $node): ?New_ return null; } - $flags = $node->getArgs()[1] - ->value; + $flagsArg = $node->getArg('flags', 1); + if (! $flagsArg instanceof Arg) { + return null; + } + + $flags = $flagsArg->value; if ($this->isSkipDotsPresent($flags)) { return null; } diff --git a/rules/Php84/Rector/FuncCall/AddEscapeArgumentRector.php b/rules/Php84/Rector/FuncCall/AddEscapeArgumentRector.php index cb89dd102aa..977f79250d9 100644 --- a/rules/Php84/Rector/FuncCall/AddEscapeArgumentRector.php +++ b/rules/Php84/Rector/FuncCall/AddEscapeArgumentRector.php @@ -63,11 +63,11 @@ public function refactor(Node $node): null|FuncCall|MethodCall $name = $this->getName($node); - if (in_array($name, ['fputcsv', 'fgetcsv'], true) && isset($node->getArgs()[4])) { + if (in_array($name, ['fputcsv', 'fgetcsv'], true) && $node->getArg('escape', 4) instanceof Arg) { return null; } - if ($name === 'str_getcsv' && isset($node->getArgs()[3])) { + if ($name === 'str_getcsv' && $node->getArg('escape', 3) instanceof Arg) { return null; } @@ -85,11 +85,11 @@ public function refactor(Node $node): null|FuncCall|MethodCall return null; } - if (in_array($name, ['setCsvControl', 'fgetcsv'], true) && isset($node->getArgs()[2])) { + if (in_array($name, ['setCsvControl', 'fgetcsv'], true) && $node->getArg('escape', 2) instanceof Arg) { return null; } - if ($name === 'fputcsv' && isset($node->getArgs()[3])) { + if ($name === 'fputcsv' && $node->getArg('escape', 3) instanceof Arg) { return null; } diff --git a/rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php b/rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php index c2127cd7bb4..56fabc08faa 100644 --- a/rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php +++ b/rules/Php85/Rector/ArrayDimFetch/ArrayFirstLastRector.php @@ -5,6 +5,7 @@ namespace Rector\Php85\Rector\ArrayDimFetch; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\BinaryOp\Minus; use PhpParser\Node\Expr\FuncCall; @@ -98,7 +99,12 @@ private function refactorArrayKeyPattern(ArrayDimFetch $arrayDimFetch): ?FuncCal return null; } - if (! $this->nodeComparator->areNodesEqual($arrayDimFetch->var, $arrayDimFetch->dim->getArgs()[0]->value)) { + $firstArg = $arrayDimFetch->dim->getArg('array', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + if (! $this->nodeComparator->areNodesEqual($arrayDimFetch->var, $firstArg->value)) { return null; } @@ -135,8 +141,12 @@ private function refactorArrayValuesPattern(ArrayDimFetch $arrayDimFetch): ?Func return null; } - $arrayArg = $arrayDimFetch->var->getArgs()[0] - ->value; + $firstArg = $arrayDimFetch->var->getArg('array', 0); + if (! $firstArg instanceof Arg) { + return null; + } + + $arrayArg = $firstArg->value; if ($arrayDimFetch->dim instanceof Int_ && $arrayDimFetch->dim->value === 0) { return $this->nodeFactory->createFuncCall('array_first', [$arrayArg]); @@ -163,7 +173,12 @@ private function refactorArrayValuesPattern(ArrayDimFetch $arrayDimFetch): ?Func return null; } - if (! $this->nodeComparator->areNodesEqual($arrayArg, $arrayDimFetch->dim->left->getArgs()[0]->value)) { + $countArg = $arrayDimFetch->dim->left->getArg('value', 0); + if (! $countArg instanceof Arg) { + return null; + } + + if (! $this->nodeComparator->areNodesEqual($arrayArg, $countArg->value)) { return null; } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php index df6d9921355..6720cce203f 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php @@ -289,8 +289,8 @@ private function shouldStop(Node $node, string $paramName): bool if ($node instanceof FuncCall && ! $node->isFirstClassCallable() && $this->isNames($node, ['is_array', 'is_string', 'is_int', 'is_bool', 'is_float'])) { - $firstArg = $node->getArgs()[0]; - $nodeToCheck = $firstArg->value; + $firstArg = $node->getArg('value', 0); + $nodeToCheck = $firstArg instanceof Arg ? $firstArg->value : null; } if ($node instanceof Expression) { diff --git a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php index 9defe2a2399..e6173bdab56 100644 --- a/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php +++ b/rules/TypeDeclaration/Rector/FuncCall/AddArrowFunctionParamArrayWhereDimFetchRector.php @@ -5,6 +5,7 @@ namespace Rector\TypeDeclaration\Rector\FuncCall; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Closure; @@ -83,13 +84,23 @@ public function refactor(Node $node): ?Node $callbackPosition = $positions['callback']; - $closureExpr = $node->getArgs()[$callbackPosition]->value; + $callbackArg = $node->getArg('callback', $callbackPosition); + if (! $callbackArg instanceof Arg) { + continue; + } + + $closureExpr = $callbackArg->value; if (! $closureExpr instanceof ArrowFunction && ! $closureExpr instanceof Closure) { continue; } $arrayPosition = $positions['array']; - $closureItems = $node->getArgs()[$arrayPosition]->value; + $arrayArg = $node->getArg('array', $arrayPosition); + if (! $arrayArg instanceof Arg) { + continue; + } + + $closureItems = $arrayArg->value; $isArrayVariableNames = $this->resolveIsArrayVariables($closureExpr); $instanceofVariableNames = $this->resolveInstanceofVariables($closureExpr); @@ -179,8 +190,12 @@ private function resolveIsArrayVariables(Closure|ArrowFunction $closureExpr): ar continue; } - $firstArgExpr = $funcCall->getArgs()[0] - ->value; + $firstArg = $funcCall->getArg('value', 0); + if (! $firstArg instanceof Arg) { + continue; + } + + $firstArgExpr = $firstArg->value; if (! $firstArgExpr instanceof Variable) { continue; }