-
-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement UnwrapArrayMap and UnwrapArrayFilter mutators #513
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -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; | ||||||
*/ | ||||||
public function mutate(Node $node) | ||||||
{ | ||||||
return $node->args[$this->getParameterIndex()]; | ||||||
} | ||||||
|
||||||
protected function mutatesNode(Node $node): bool | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be final, what do you think?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definietly :) |
||||||
{ | ||||||
if (!$node instanceof Node\Expr\FuncCall) { | ||||||
return false; | ||||||
} | ||||||
|
||||||
return $node->name->toString() === $this->getFunctionName(); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -159,6 +160,11 @@ final class MutatorProfile | |
Mutator\Cast\CastString::class, | ||
]; | ||
|
||
public const UNWRAP = [ | ||
Mutator\Unwrap\UnwrapArrayMap::class, | ||
Mutator\Unwrap\UnwrapArrayFilter::class, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same order as in the manual should work best, IMO. |
||
]; | ||
|
||
public const DEFAULT = [ | ||
'@arithmetic', | ||
'@boolean', | ||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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 [ | ||
<<<'PHP' | ||
<?php | ||
|
||
$a = array_filter(['A', 1, 'C'], 'is_int'); | ||
PHP | ||
, | ||
<<<'PHP' | ||
<?php | ||
|
||
$a = ['A', 1, 'C']; | ||
PHP | ||
]; | ||
|
||
yield [ | ||
<<<'PHP' | ||
<?php | ||
|
||
$a = array_filter(['A', 1, 'C'], function($var): bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please add more tests (like parameter as a variable, function with lower/upper cased characters and so on.) See the list of cases in https://github.com/infection/infection/blob/68f70ad62ffd14af62781d989e4230c45c235010/tests/Mutator/Regex/PregQuoteTest.php same for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch, wrongly capitalized was the problem :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's |
||
return is_int($var); | ||
}); | ||
PHP | ||
, | ||
<<<'PHP' | ||
<?php | ||
|
||
$a = ['A', 1, 'C']; | ||
PHP | ||
]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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 UnwrapArrayMapTest extends AbstractMutatorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideMutationCases | ||
*/ | ||
public function test_mutator($input, $expected = null): void | ||
{ | ||
$this->doTest($input, $expected); | ||
} | ||
|
||
public function provideMutationCases(): \Generator | ||
{ | ||
yield [ | ||
<<<'PHP' | ||
<?php | ||
|
||
$a = array_map('strtolower', ['A', 'B', 'C']); | ||
PHP | ||
, | ||
<<<'PHP' | ||
<?php | ||
|
||
$a = ['A', 'B', 'C']; | ||
PHP | ||
]; | ||
|
||
yield [ | ||
<<<'PHP' | ||
<?php | ||
|
||
$a = array_map(function(string $letter): string { | ||
return strtolower($letter); | ||
}, ['A', 'B', 'C']); | ||
PHP | ||
, | ||
<<<'PHP' | ||
<?php | ||
|
||
$a = ['A', 'B', 'C']; | ||
PHP | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.