Skip to content

Commit

Permalink
Enhancement: Use generic approach
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Nov 1, 2018
1 parent fc786aa commit 1a90079
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/AbstractUnwrapMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ abstract class AbstractUnwrapMutator extends Mutator
*/
final public function mutate(Node $node)
{
return $node->args[$this->getParameterIndex()];
foreach ($this->getParameterIndex($node) as $index) {
yield $node->args[$index];
}
}

abstract protected function getFunctionName(): string;

abstract protected function getParameterIndex(): int;
abstract protected function getParameterIndex(Node $node): \Generator;

final protected function mutatesNode(Node $node): bool
{
Expand Down
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapArrayFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
Expand All @@ -45,8 +47,8 @@ protected function getFunctionName(): string
return 'array_filter';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 0;
yield 0;
}
}
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapArrayFlip.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
Expand All @@ -45,8 +47,8 @@ protected function getFunctionName(): string
return 'array_flip';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 0;
yield 0;
}
}
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapArrayMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
Expand All @@ -45,8 +47,8 @@ protected function getFunctionName(): string
return 'array_map';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 1;
yield 1;
}
}
25 changes: 5 additions & 20 deletions src/Mutator/Unwrap/UnwrapArrayReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,20 @@

namespace Infection\Mutator\Unwrap;

use Infection\Mutator\Util\Mutator;
use PhpParser\Node;

/**
* @internal
*/
final class UnwrapArrayReplace extends Mutator
final class UnwrapArrayReplace extends AbstractUnwrapMutator
{
/**
* Replaces "$a = array_replace($array1, $array1);" with
*
* - "$a = $array1;
* - "$a = $array2;
*
* @return Node\Param;
*/
public function mutate(Node $node)
protected function getFunctionName(): string
{
foreach ($node->args as $arg) {
yield $arg;
}
return 'array_replace';
}

protected function mutatesNode(Node $node): bool
protected function getParameterIndex(Node $node): \Generator
{
if (!$node instanceof Node\Expr\FuncCall) {
return false;
}

return $node->name->toLowerString() === 'array_replace';
yield from array_keys($node->args);
}
}
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapArrayReverse.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

namespace Infection\Mutator\Unwrap;

use PhpParser\Node;

/**
* @internal
*/
Expand All @@ -45,8 +47,8 @@ protected function getFunctionName(): string
return 'array_reverse';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 0;
yield 0;
}
}

0 comments on commit 1a90079

Please sign in to comment.