Skip to content

Commit

Permalink
Enhancement: Use generic approach
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz authored and maks-rafalko committed Nov 3, 2018
1 parent c24ebd3 commit 0ba9520
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 36 deletions.
12 changes: 8 additions & 4 deletions src/Mutator/Unwrap/AbstractUnwrapMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,25 @@ 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
{
if (!$node instanceof Node\Expr\FuncCall) {
return false;
}

if (!array_key_exists($this->getParameterIndex(), $node->args)) {
return false;
foreach ($this->getParameterIndex($node) as $index) {
if (!array_key_exists($index, $node->args)) {
return false;
}
}

return $node->name->toLowerString() === strtolower($this->getFunctionName());
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;
}
}
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapArrayReduce.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_reduce';
}

protected function getParameterIndex(): int
protected function getParameterIndex(Node $node): \Generator
{
return 2;
yield 2;
}
}
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;
}
}
6 changes: 4 additions & 2 deletions src/Mutator/Unwrap/UnwrapStrRepeat.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 'str_repeat';
}

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

0 comments on commit 0ba9520

Please sign in to comment.