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 f3f658796..aadb367a5 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\UnwrapArrayUintersectUassoc::class, Mutator\Unwrap\UnwrapArrayUnique::class, @@ -376,6 +377,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, 'UnwrapArrayUintersectUassoc' => Mutator\Unwrap\UnwrapArrayUintersectUassoc::class, 'UnwrapArrayUnique' => Mutator\Unwrap\UnwrapArrayUnique::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 + ]; + } +}