From 81f06ae7b3dcda60ee9255a8b676fa242ccdb0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sat, 16 Feb 2019 09:07:42 +0100 Subject: [PATCH] Enhancement: Implement UnwrapArrayUintersect mutator --- src/Mutator/Unwrap/UnwrapArrayUintersect.php | 58 ++++ src/Mutator/Util/MutatorProfile.php | 2 + .../Unwrap/UnwrapArrayUintersectTest.php | 282 ++++++++++++++++++ 3 files changed, 342 insertions(+) create mode 100644 src/Mutator/Unwrap/UnwrapArrayUintersect.php create mode 100644 tests/Mutator/Unwrap/UnwrapArrayUintersectTest.php diff --git a/src/Mutator/Unwrap/UnwrapArrayUintersect.php b/src/Mutator/Unwrap/UnwrapArrayUintersect.php new file mode 100644 index 000000000..b3574ca6a --- /dev/null +++ b/src/Mutator/Unwrap/UnwrapArrayUintersect.php @@ -0,0 +1,58 @@ +args), + 0, + \count($node->args) - 1 + ); + } +} diff --git a/src/Mutator/Util/MutatorProfile.php b/src/Mutator/Util/MutatorProfile.php index 0224d3255..1e44fa6f7 100644 --- a/src/Mutator/Util/MutatorProfile.php +++ b/src/Mutator/Util/MutatorProfile.php @@ -216,6 +216,7 @@ final class MutatorProfile Mutator\Unwrap\UnwrapArrayUdiff::class, Mutator\Unwrap\UnwrapArrayUdiffAssoc::class, Mutator\Unwrap\UnwrapArrayUdiffUassoc::class, + Mutator\Unwrap\UnwrapArrayUintersect::class, Mutator\Unwrap\UnwrapArrayUintersectAssoc::class, Mutator\Unwrap\UnwrapArrayUnique::class, Mutator\Unwrap\UnwrapArrayValues::class, @@ -373,6 +374,7 @@ final class MutatorProfile 'UnwrapArrayUdiff' => Mutator\Unwrap\UnwrapArrayUdiff::class, 'UnwrapArrayUdiffAssoc' => Mutator\Unwrap\UnwrapArrayUdiffAssoc::class, 'UnwrapArrayUdiffUassoc' => Mutator\Unwrap\UnwrapArrayUdiffUassoc::class, + 'UnwrapArrayUintersect' => Mutator\Unwrap\UnwrapArrayUintersect::class, 'UnwrapArrayUintersectAssoc' => Mutator\Unwrap\UnwrapArrayUintersectAssoc::class, 'UnwrapArrayUnique' => Mutator\Unwrap\UnwrapArrayUnique::class, 'UnwrapArrayValues' => Mutator\Unwrap\UnwrapArrayValues::class, diff --git a/tests/Mutator/Unwrap/UnwrapArrayUintersectTest.php b/tests/Mutator/Unwrap/UnwrapArrayUintersectTest.php new file mode 100644 index 000000000..a5cde7a65 --- /dev/null +++ b/tests/Mutator/Unwrap/UnwrapArrayUintersectTest.php @@ -0,0 +1,282 @@ +doTest($input, $expected); + } + + public function provideMutationCases(): \Generator + { + yield 'It mutates correctly when provided with an array' => [ + <<<'PHP' + 'bar'], ['baz' => 'bar'], $valueCompareFunc); +PHP + , + [ + <<<'PHP' + 'bar']; +PHP + , + <<<'PHP' + 'bar']; +PHP + , + ], + ]; + + yield 'It mutates correctly when provided with a constant' => [ + <<<'PHP' + 'bar'], $valueCompareFunc); +PHP + , + [ + <<<'PHP' + 'bar']; +PHP + , + ], + ]; + + yield 'It mutates correctly when a backslash is in front of array_uintersect' => [ + <<<'PHP' + 'bar'], ['baz' => 'bar'], $valueCompareFunc); +PHP + , + [ + <<<'PHP' + 'bar']; +PHP + , + <<<'PHP' + 'bar']; +PHP + , + ], + ]; + + yield 'It mutates correctly within if statements' => [ + <<<'PHP' + 'bar']; +if (array_uintersect($a, ['baz' => 'bar'], $valueCompareFunc) === $a) { + return true; +} +PHP + , + [ + <<<'PHP' + 'bar']; +if ($a === $a) { + return true; +} +PHP + , + <<<'PHP' + 'bar']; +if (['baz' => 'bar'] === $a) { + return true; +} +PHP + , + ], + ]; + + yield 'It mutates correctly when array_uintersect is wrongly capitalized' => [ + <<<'PHP' + 'bar'], ['baz' => 'bar'], $valueCompareFunc); +PHP + , + [ + <<<'PHP' + 'bar']; +PHP + , + <<<'PHP' + 'bar']; +PHP + , + ], + ]; + + yield 'It mutates correctly when array_uintersect uses functions as input' => [ + <<<'PHP' +bar(), $foo->baz(), $valueCompareFunc); +PHP + , + [ + <<<'PHP' +bar(); +PHP + , + <<<'PHP' +baz(); +PHP + , + ], + ]; + + yield 'It mutates correctly when provided with a more complex situation' => [ + <<<'PHP' + 'bar'], ['baz' => 'bar'], $valueCompareFunc)); +PHP + , + [ + <<<'PHP' + 'bar']); +PHP + , + <<<'PHP' + 'bar']); +PHP + , + ], + ]; + + yield 'It mutates correctly when more than two parameters are present' => [ + <<<'PHP' + 'bar'], ['baz' => 'bar'], ['qux' => 'bar'], $valueCompareFunc); +PHP + , + [ + <<<'PHP' + 'bar']; +PHP + , + <<<'PHP' + 'bar']; +PHP + , + <<<'PHP' + 'bar']; +PHP + , + ], + ]; + + yield 'It does not mutate other array_ calls' => [ + <<<'PHP' + 'bar']); +PHP + ]; + + yield 'It does not mutate functions named array_uintersect' => [ + <<<'PHP' + [ + <<<'PHP' + 'bar'], ['baz' => 'bar'], $valueCompareFunc); +PHP + ]; + } +}