From 108020da1ac22b33e6eb6656cc7bc027e050cdd6 Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Fri, 19 Oct 2018 08:56:04 +0200 Subject: [PATCH] Implement UnwrapArrayMap and UnwrapArrayFilter mutators (#513) * Implement UnwrapArrayMap and UnwrapArrayFilter mutators * Add more tests examples and fix wrongly capitalized funcation name * Fix typos in tests provider for UnwrapArrayFilter mutator --- src/Mutator/Unwrap/AbstractUnwrapMutator.php | 42 +++++ src/Mutator/Unwrap/UnwrapArrayFilter.php | 26 +++ src/Mutator/Unwrap/UnwrapArrayMap.php | 26 +++ src/Mutator/Util/MutatorProfile.php | 10 ++ .../Mutator/Unwrap/UnwrapArrayFilterTest.php | 153 ++++++++++++++++++ tests/Mutator/Unwrap/UnwrapArrayMapTest.php | 153 ++++++++++++++++++ 6 files changed, 410 insertions(+) create mode 100644 src/Mutator/Unwrap/AbstractUnwrapMutator.php create mode 100644 src/Mutator/Unwrap/UnwrapArrayFilter.php create mode 100644 src/Mutator/Unwrap/UnwrapArrayMap.php create mode 100644 tests/Mutator/Unwrap/UnwrapArrayFilterTest.php create mode 100644 tests/Mutator/Unwrap/UnwrapArrayMapTest.php diff --git a/src/Mutator/Unwrap/AbstractUnwrapMutator.php b/src/Mutator/Unwrap/AbstractUnwrapMutator.php new file mode 100644 index 000000000..192354575 --- /dev/null +++ b/src/Mutator/Unwrap/AbstractUnwrapMutator.php @@ -0,0 +1,42 @@ +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()); + } +} diff --git a/src/Mutator/Unwrap/UnwrapArrayFilter.php b/src/Mutator/Unwrap/UnwrapArrayFilter.php new file mode 100644 index 000000000..99ce60fa3 --- /dev/null +++ b/src/Mutator/Unwrap/UnwrapArrayFilter.php @@ -0,0 +1,26 @@ + 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, + ]; + 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, ]; } diff --git a/tests/Mutator/Unwrap/UnwrapArrayFilterTest.php b/tests/Mutator/Unwrap/UnwrapArrayFilterTest.php new file mode 100644 index 000000000..a96e5332f --- /dev/null +++ b/tests/Mutator/Unwrap/UnwrapArrayFilterTest.php @@ -0,0 +1,153 @@ +doTest($input, $expected); + } + + public function provideMutationCases(): \Generator + { + yield 'It mutates correctly when provided with a array' => [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' +bar(), 'is_int'); +PHP + , + <<<'PHP' +bar(); +PHP + ]; + + yield 'It mutates correctly when provided with a more complex situation' => [ + <<<'PHP' +doTest($input, $expected); + } + + public function provideMutationCases(): \Generator + { + yield 'It mutates correctly when provided with a array' => [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' +bar()); +PHP + , + <<<'PHP' +bar(); +PHP + ]; + + yield 'It mutates correctly when provided with a more complex situation' => [ + <<<'PHP' +