Skip to content

Commit

Permalink
Implement UnwrapArrayMap and UnwrapArrayFilter mutators (#513)
Browse files Browse the repository at this point in the history
* Implement UnwrapArrayMap and UnwrapArrayFilter mutators

* Add more tests examples and fix wrongly capitalized funcation name

* Fix typos in tests provider for UnwrapArrayFilter mutator
  • Loading branch information
akondas authored and maks-rafalko committed Oct 19, 2018
1 parent 68f70ad commit 108020d
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Mutator/Unwrap/AbstractUnwrapMutator.php
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

declare(strict_types=1);

namespace Infection\Mutator\Unwrap;

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

/**
* @internal
*/
abstract class AbstractUnwrapMutator extends Mutator
{
abstract protected function getFunctionName(): string;

abstract protected function getParameterIndex(): int;

/**
* Replaces "$a = function(arg1, arg2);" with "$a = arg1;"
*
* @return Node\Param;
*/
final public function mutate(Node $node)
{
return $node->args[$this->getParameterIndex()];
}

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

return $node->name->toLowerString() === strtolower($this->getFunctionName());
}
}
26 changes: 26 additions & 0 deletions src/Mutator/Unwrap/UnwrapArrayFilter.php
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

declare(strict_types=1);

namespace Infection\Mutator\Unwrap;

/**
* @internal
*/
final class UnwrapArrayFilter extends AbstractUnwrapMutator
{
protected function getFunctionName(): string
{
return 'array_filter';
}

protected function getParameterIndex(): int
{
return 0;
}
}
26 changes: 26 additions & 0 deletions src/Mutator/Unwrap/UnwrapArrayMap.php
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

declare(strict_types=1);

namespace Infection\Mutator\Unwrap;

/**
* @internal
*/
final class UnwrapArrayMap extends AbstractUnwrapMutator
{
protected function getFunctionName(): string
{
return 'array_map';
}

protected function getParameterIndex(): int
{
return 1;
}
}
10 changes: 10 additions & 0 deletions src/Mutator/Util/MutatorProfile.php
Expand Up @@ -33,6 +33,7 @@ final class MutatorProfile
'@sort' => self::SORT,
'@zero_iteration' => self::ZERO_ITERATION,
'@cast' => self::CAST,
'@unwrap' => self::UNWRAP,

//Special Profiles
'@default' => self::DEFAULT,
Expand Down Expand Up @@ -159,6 +160,11 @@ final class MutatorProfile
Mutator\Cast\CastString::class,
];

public const UNWRAP = [
Mutator\Unwrap\UnwrapArrayMap::class,
Mutator\Unwrap\UnwrapArrayFilter::class,
];

public const DEFAULT = [
'@arithmetic',
'@boolean',
Expand Down Expand Up @@ -278,5 +284,9 @@ final class MutatorProfile
'CastInt' => Mutator\Cast\CastInt::class,
'CastObject' => Mutator\Cast\CastObject::class,
'CastString' => Mutator\Cast\CastString::class,

// Unwrap
'UnwrapArrayMap' => Mutator\Unwrap\UnwrapArrayMap::class,
'UnwrapArrayFilter' => Mutator\Unwrap\UnwrapArrayFilter::class,
];
}
153 changes: 153 additions & 0 deletions tests/Mutator/Unwrap/UnwrapArrayFilterTest.php
@@ -0,0 +1,153 @@
<?php
/**
* Copyright © 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/

declare(strict_types=1);

namespace Infection\Tests\Mutator\Unwrap;

use Infection\Tests\Mutator\AbstractMutatorTestCase;

/**
* @internal
*/
final class UnwrapArrayFilterTest extends AbstractMutatorTestCase
{
/**
* @dataProvider provideMutationCases
*/
public function test_mutator($input, $expected = null): void
{
$this->doTest($input, $expected);
}

public function provideMutationCases(): \Generator
{
yield 'It mutates correctly when provided with a array' => [
<<<'PHP'
<?php
$a = array_filter(['A', 1, 'C'], 'is_int');
PHP
,
<<<'PHP'
<?php
$a = ['A', 1, 'C'];
PHP
];

yield 'It mutates correctly when provided with a constant' => [
<<<'PHP'
<?php
$a = array_filter(\Class_With_Const::Const, 'is_int');
PHP
,
<<<'PHP'
<?php
$a = \Class_With_Const::Const;
PHP
];

yield 'It mutates correctly when a backslash is in front of array_filter' => [
<<<'PHP'
<?php
$a = \array_filter(['A', 1, 'C'], 'is_int');
PHP
,
<<<'PHP'
<?php
$a = ['A', 1, 'C'];
PHP
];

yield 'It does not mutate other array_ calls' => [
<<<'PHP'
<?php
$a = array_map('strtolower', ['A', 'B', 'C']);
PHP
];

yield 'It does not mutate functions named array_filter' => [
<<<'PHP'
<?php
function array_filter($text, $other)
{
}
PHP
];

yield 'It mutates correctly within if statements' => [
<<<'PHP'
<?php
$a = ['A', 1, 'C'];
if (array_filter($a, 'is_int') === $a) {
return true;
}
PHP
,
<<<'PHP'
<?php
$a = ['A', 1, 'C'];
if ($a === $a) {
return true;
}
PHP
];

yield 'It mutates correctly when array_filter is wrongly capitalized' => [
<<<'PHP'
<?php
$a = aRrAy_FiLtEr(['A', 1, 'C'], 'is_int');
PHP
,
<<<'PHP'
<?php
$a = ['A', 1, 'C'];
PHP
];

yield 'It mutates correctly when array_filter uses another function as input' => [
<<<'PHP'
<?php
$a = array_filter($foo->bar(), 'is_int');
PHP
,
<<<'PHP'
<?php
$a = $foo->bar();
PHP
];

yield 'It mutates correctly when provided with a more complex situation' => [
<<<'PHP'
<?php
$a = array_map('strtolower', array_filter(['A', 1, 'C'], function($char): bool {
return !is_int($char);
}));
PHP
,
<<<'PHP'
<?php
$a = array_map('strtolower', ['A', 1, 'C']);
PHP
];
}
}

0 comments on commit 108020d

Please sign in to comment.