From 347b567029e141b9cfa2f60d5ffa351486b690e8 Mon Sep 17 00:00:00 2001 From: Oleg Zhulnev Date: Tue, 12 Oct 2021 11:00:18 +0300 Subject: [PATCH] use `getArgs` function instead of conditions with VariardicPlacehoder --- .../Finder/Iterator/RealPathFilterIterator.php | 3 ++- src/Mutator/Extensions/BCMath.php | 14 +++++--------- src/Mutator/Extensions/MBString.php | 16 +++++++--------- src/Mutator/Regex/AbstractPregMatch.php | 16 +++++++--------- src/Mutator/Regex/PregMatchMatches.php | 6 +----- src/Mutator/Regex/PregQuote.php | 6 +----- src/Mutator/Unwrap/AbstractUnwrapMutator.php | 8 +++----- 7 files changed, 26 insertions(+), 43 deletions(-) diff --git a/src/FileSystem/Finder/Iterator/RealPathFilterIterator.php b/src/FileSystem/Finder/Iterator/RealPathFilterIterator.php index ce6e550e1f..c1e40ab417 100644 --- a/src/FileSystem/Finder/Iterator/RealPathFilterIterator.php +++ b/src/FileSystem/Finder/Iterator/RealPathFilterIterator.php @@ -37,6 +37,7 @@ use const DIRECTORY_SEPARATOR; use function preg_quote; +use ReturnTypeWillChange; use function str_replace; use Symfony\Component\Finder\Iterator\MultiplePcreFilterIterator; @@ -50,7 +51,7 @@ final class RealPathFilterIterator extends MultiplePcreFilterIterator * * @return bool true if the value should be kept, false otherwise */ - #[\ReturnTypeWillChange] + #[ReturnTypeWillChange] public function accept() { $filename = $this->current()->getRealPath(); diff --git a/src/Mutator/Extensions/BCMath.php b/src/Mutator/Extensions/BCMath.php index 921d32f4bf..a9fa20bedd 100644 --- a/src/Mutator/Extensions/BCMath.php +++ b/src/Mutator/Extensions/BCMath.php @@ -217,11 +217,9 @@ private static function makeCastToStringMapper(Closure $converter): Closure private static function makeBinaryOperatorMapper(string $operator): Closure { return static function (Node\Expr\FuncCall $node) use ($operator): iterable { - if ($node->args[0] instanceof Node\VariadicPlaceholder || $node->args[1] instanceof Node\VariadicPlaceholder) { - return []; - } + $args = $node->getArgs(); - yield new $operator($node->args[0]->value, $node->args[1]->value); + yield new $operator($args[0]->value, $args[1]->value); }; } @@ -241,16 +239,14 @@ private static function makeSquareRootsMapper(): Closure private static function makePowerModuloMapper(): Closure { return static function (Node\Expr\FuncCall $node): iterable { - if ($node->args[2] instanceof Node\VariadicPlaceholder) { - return []; - } + $args = $node->getArgs(); yield new Node\Expr\BinaryOp\Mod( new Node\Expr\FuncCall( new Node\Name('\pow'), - [$node->args[0], $node->args[1]] + [$args[0], $args[1]] ), - $node->args[2]->value + $args[2]->value ); }; } diff --git a/src/Mutator/Extensions/MBString.php b/src/Mutator/Extensions/MBString.php index 10742cac30..fdeec00887 100644 --- a/src/Mutator/Extensions/MBString.php +++ b/src/Mutator/Extensions/MBString.php @@ -160,7 +160,7 @@ private static function createConverters(array $allowedFunctions): array private static function makeFunctionMapper(string $newFunctionName): Closure { return static function (Node\Expr\FuncCall $node) use ($newFunctionName): iterable { - yield self::mapFunctionCall($node, $newFunctionName, $node->args); + yield self::mapFunctionCall($node, $newFunctionName, $node->getArgs()); }; } @@ -170,7 +170,7 @@ private static function makeFunctionMapper(string $newFunctionName): Closure private static function makeFunctionAndRemoveExtraArgsMapper(string $newFunctionName, int $argsAtMost): Closure { return static function (Node\Expr\FuncCall $node) use ($newFunctionName, $argsAtMost): iterable { - yield self::mapFunctionCall($node, $newFunctionName, array_slice($node->args, 0, $argsAtMost)); + yield self::mapFunctionCall($node, $newFunctionName, array_slice($node->getArgs(), 0, $argsAtMost)); }; } @@ -192,21 +192,19 @@ private static function makeConvertCaseMapper(): Closure return; } - yield self::mapFunctionCall($node, $functionName, [$node->args[0]]); + yield self::mapFunctionCall($node, $functionName, [$node->getArgs()[0]]); }; } private static function getConvertCaseModeValue(Node\Expr\FuncCall $node): ?int { - if (count($node->args) < 2) { - return null; - } + $args = $node->getArgs(); - if ($node->args[1] instanceof Node\VariadicPlaceholder) { + if (count($args) < 2) { return null; } - $mode = $node->args[1]->value; + $mode = $args[1]->value; if ($mode instanceof Node\Scalar\LNumber) { return $mode->value; @@ -248,7 +246,7 @@ private static function isInMbCaseMode(int $mode, string ...$cases): bool } /** - * @param array $args + * @param Node\Arg[] $args */ private static function mapFunctionCall(Node\Expr\FuncCall $node, string $newFuncName, array $args): Node\Expr\FuncCall { diff --git a/src/Mutator/Regex/AbstractPregMatch.php b/src/Mutator/Regex/AbstractPregMatch.php index cf5824386d..34f9763219 100644 --- a/src/Mutator/Regex/AbstractPregMatch.php +++ b/src/Mutator/Regex/AbstractPregMatch.php @@ -60,16 +60,14 @@ abstract class AbstractPregMatch implements Mutator */ public function mutate(Node $node): iterable { - if ($node->args[0] instanceof Node\VariadicPlaceholder) { - return []; - } - - $originalRegex = $this->pullOutRegex($node->args[0]); + $arguments = $node->getArgs(); + $firstArgument = $arguments[0]; + $originalRegex = $this->pullOutRegex($firstArgument); foreach ($this->mutateRegex($originalRegex) as $mutatedRegex) { - $newArgument = $this->getNewRegexArgument($mutatedRegex, $node->args[0]); + $newArgument = $this->getNewRegexArgument($mutatedRegex, $firstArgument); - yield new FuncCall($node->name, [$newArgument] + $node->args, $node->getAttributes()); + yield new FuncCall($node->name, [$newArgument] + $arguments, $node->getAttributes()); } } @@ -78,8 +76,8 @@ public function canMutate(Node $node): bool return $node instanceof FuncCall && $node->name instanceof Node\Name && strtolower((string) $node->name) === 'preg_match' - && $node->args[0] instanceof Node\Arg - && $node->args[0]->value instanceof Node\Scalar\String_ + && $node->getArgs()[0] instanceof Node\Arg + && $node->getArgs()[0]->value instanceof Node\Scalar\String_ && $this->isProperRegexToMutate($this->pullOutRegex($node->args[0])); } diff --git a/src/Mutator/Regex/PregMatchMatches.php b/src/Mutator/Regex/PregMatchMatches.php index 310af0e3b6..749123e07a 100644 --- a/src/Mutator/Regex/PregMatchMatches.php +++ b/src/Mutator/Regex/PregMatchMatches.php @@ -89,11 +89,7 @@ public static function getDefinition(): ?Definition */ public function mutate(Node $node): iterable { - if ($node->args[2] instanceof Node\VariadicPlaceholder) { - return []; - } - - yield new Node\Expr\Cast\Int_(new Node\Expr\Assign($node->args[2]->value, new Node\Expr\Array_())); + yield new Node\Expr\Cast\Int_(new Node\Expr\Assign($node->getArgs()[2]->value, new Node\Expr\Array_())); } public function canMutate(Node $node): bool diff --git a/src/Mutator/Regex/PregQuote.php b/src/Mutator/Regex/PregQuote.php index 072cf1d8d8..315d8a9ed6 100644 --- a/src/Mutator/Regex/PregQuote.php +++ b/src/Mutator/Regex/PregQuote.php @@ -84,11 +84,7 @@ public static function getDefinition(): ?Definition */ public function mutate(Node $node): iterable { - if ($node->args[0] instanceof Node\VariadicPlaceholder) { - return []; - } - - yield $node->args[0]; + yield $node->getArgs()[0]; } public function canMutate(Node $node): bool diff --git a/src/Mutator/Unwrap/AbstractUnwrapMutator.php b/src/Mutator/Unwrap/AbstractUnwrapMutator.php index f9c48ae314..c4c6e01e74 100644 --- a/src/Mutator/Unwrap/AbstractUnwrapMutator.php +++ b/src/Mutator/Unwrap/AbstractUnwrapMutator.php @@ -58,15 +58,13 @@ abstract class AbstractUnwrapMutator implements Mutator final public function mutate(Node $node): iterable { foreach ($this->getParameterIndexes($node) as $index) { - if ($node->args[$index] instanceof Node\VariadicPlaceholder) { - continue; - } + $args = $node->getArgs(); - if ($node->args[$index]->unpack) { + if ($args[$index]->unpack) { continue; } - yield $node->args[$index]; + yield $args[$index]; } }