diff --git a/resources/schema.json b/resources/schema.json index 2ddaf041c..60da6fff7 100644 --- a/resources/schema.json +++ b/resources/schema.json @@ -350,6 +350,7 @@ "UnwrapArrayValues": { "$ref": "#/definitions/default-mutator-config" }, "UnwrapLcFirst": { "$ref": "#/definitions/default-mutator-config" }, "UnwrapLtrim": { "$ref": "#/definitions/default-mutator-config" }, + "UnwrapStrIreplace": { "$ref": "#/definitions/default-mutator-config" }, "UnwrapStrRepeat": { "$ref": "#/definitions/default-mutator-config" }, "UnwrapStrReplace": { "$ref": "#/definitions/default-mutator-config" }, "UnwrapStrToLower": { "$ref": "#/definitions/default-mutator-config" }, diff --git a/src/Mutator/ProfileList.php b/src/Mutator/ProfileList.php index 8b2cde8f1..61b9ad22a 100644 --- a/src/Mutator/ProfileList.php +++ b/src/Mutator/ProfileList.php @@ -240,6 +240,7 @@ final class ProfileList Mutator\Unwrap\UnwrapArrayValues::class, Mutator\Unwrap\UnwrapLcFirst::class, Mutator\Unwrap\UnwrapLtrim::class, + Mutator\Unwrap\UnwrapStrIreplace::class, Mutator\Unwrap\UnwrapStrRepeat::class, Mutator\Unwrap\UnwrapStrReplace::class, Mutator\Unwrap\UnwrapStrToLower::class, @@ -423,6 +424,7 @@ final class ProfileList 'UnwrapArrayValues' => Mutator\Unwrap\UnwrapArrayValues::class, 'UnwrapLcFirst' => Mutator\Unwrap\UnwrapLcFirst::class, 'UnwrapLtrim' => Mutator\Unwrap\UnwrapLtrim::class, + 'UnwrapStrIreplace' => Mutator\Unwrap\UnwrapStrIreplace::class, 'UnwrapStrRepeat' => Mutator\Unwrap\UnwrapStrRepeat::class, 'UnwrapStrReplace' => Mutator\Unwrap\UnwrapStrReplace::class, 'UnwrapStrToLower' => Mutator\Unwrap\UnwrapStrToLower::class, diff --git a/src/Mutator/Unwrap/UnwrapStrIreplace.php b/src/Mutator/Unwrap/UnwrapStrIreplace.php new file mode 100644 index 000000000..fc04dfaf7 --- /dev/null +++ b/src/Mutator/Unwrap/UnwrapStrIreplace.php @@ -0,0 +1,81 @@ +'); +``` + +Will be mutated to: + +```php +$x = ''; +``` +TXT + , + MutatorCategory::SEMANTIC_REDUCTION, + null + ); + } + + protected function getFunctionName(): string + { + return 'str_ireplace'; + } + + /** + * @psalm-pure + */ + protected function getParameterIndexes(Node\Expr\FuncCall $node): iterable + { + yield 2; + } +} diff --git a/src/Mutator/Unwrap/UnwrapStrReplace.php b/src/Mutator/Unwrap/UnwrapStrReplace.php index 5e18718bc..bb96016e7 100644 --- a/src/Mutator/Unwrap/UnwrapStrReplace.php +++ b/src/Mutator/Unwrap/UnwrapStrReplace.php @@ -48,16 +48,16 @@ public static function getDefinition(): ?Definition { return new Definition( <<<'TXT' -Replaces a `str_replace` function call with its first operand. For example: +Replaces a `str_replace` function call with its third operand. For example: ```php -$x = str_replace('Hello!'); +$x = str_replace('%body%', 'black', ''); ``` Will be mutated to: ```php -$x = 'Hello!'; +$x = ''; ``` TXT , diff --git a/tests/phpunit/Mutator/Unwrap/UnwrapStrIreplaceTest.php b/tests/phpunit/Mutator/Unwrap/UnwrapStrIreplaceTest.php new file mode 100644 index 000000000..2d8a8755a --- /dev/null +++ b/tests/phpunit/Mutator/Unwrap/UnwrapStrIreplaceTest.php @@ -0,0 +1,191 @@ +doTest($input, $expected); + } + + public function mutationsProvider(): iterable + { + yield 'It mutates correctly when provided with a string' => [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' +bar()); +PHP + , + <<<'PHP' +bar(); +PHP + ]; + + yield 'It mutates correctly when provided with a more complex situation' => [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' + [ + <<<'PHP' +