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 aaa1f75
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 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: 6 additions & 19 deletions src/Mutator/Unwrap/UnwrapArrayReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,22 @@

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;
foreach ($node->args as $index => $arg) {
yield $index;

This comment has been minimized.

Copy link
@sanmai

sanmai Nov 1, 2018

Member
 yield from array_keys(...)

This comment has been minimized.

Copy link
@localheinz

localheinz Nov 1, 2018

Author Member

Ha, nice!

}

return $node->name->toLowerString() === 'array_replace';
}
}
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 aaa1f75

Please sign in to comment.