Skip to content

Commit

Permalink
use getArgs function instead of conditions with VariardicPlacehoder
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz committed Oct 12, 2021
1 parent 25b8bae commit 347b567
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 43 deletions.
3 changes: 2 additions & 1 deletion src/FileSystem/Finder/Iterator/RealPathFilterIterator.php
Expand Up @@ -37,6 +37,7 @@

use const DIRECTORY_SEPARATOR;
use function preg_quote;
use ReturnTypeWillChange;
use function str_replace;
use Symfony\Component\Finder\Iterator\MultiplePcreFilterIterator;

Expand All @@ -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();
Expand Down
14 changes: 5 additions & 9 deletions src/Mutator/Extensions/BCMath.php
Expand Up @@ -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);
};
}

Expand All @@ -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
);
};
}
Expand Down
16 changes: 7 additions & 9 deletions src/Mutator/Extensions/MBString.php
Expand Up @@ -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());
};
}

Expand All @@ -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));
};
}

Expand All @@ -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;
Expand Down Expand Up @@ -248,7 +246,7 @@ private static function isInMbCaseMode(int $mode, string ...$cases): bool
}

/**
* @param array<Node\Arg|Node\VariadicPlaceholder> $args
* @param Node\Arg[] $args
*/
private static function mapFunctionCall(Node\Expr\FuncCall $node, string $newFuncName, array $args): Node\Expr\FuncCall
{
Expand Down
16 changes: 7 additions & 9 deletions src/Mutator/Regex/AbstractPregMatch.php
Expand Up @@ -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());
}
}

Expand All @@ -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]));
}

Expand Down
6 changes: 1 addition & 5 deletions src/Mutator/Regex/PregMatchMatches.php
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions src/Mutator/Regex/PregQuote.php
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/Mutator/Unwrap/AbstractUnwrapMutator.php
Expand Up @@ -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];
}
}

Expand Down

0 comments on commit 347b567

Please sign in to comment.