Skip to content

Commit

Permalink
Add new Catch_ mutator (#1741)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidz committed Oct 18, 2022
1 parent c080552 commit 6bc6cfe
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions resources/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
"OneZeroFloat": { "$ref": "#/definitions/default-mutator-config" },
"AssignCoalesce": { "$ref": "#/definitions/default-mutator-config" },
"Break_": { "$ref": "#/definitions/default-mutator-config" },
"Catch_": { "$ref": "#/definitions/default-mutator-config" },
"Continue_": { "$ref": "#/definitions/default-mutator-config" },
"Ternary": { "$ref": "#/definitions/default-mutator-config" },
"Throw_": { "$ref": "#/definitions/default-mutator-config" },
Expand Down
92 changes: 92 additions & 0 deletions src/Mutator/Operator/Catch_.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Mutator\Operator;

use function count;
use Infection\Mutator\Definition;
use Infection\Mutator\GetMutatorName;
use Infection\Mutator\Mutator;
use Infection\Mutator\MutatorCategory;
use PhpParser\Node;

/**
* @internal
*
* @implements Mutator<Node\Stmt\Catch_>
*/
final class Catch_ implements Mutator
{
use GetMutatorName;

public static function getDefinition(): ?Definition
{
return new Definition(
'Removes exception types in `catch` block.',
MutatorCategory::SEMANTIC_REDUCTION,
null,
<<<'DIFF'
try {
$fn();
- } catch (\Exception | \DomainException $e) {
+ } catch (\Exception $e) {
throw $e;
}
DIFF
);
}

public function canMutate(Node $node): bool
{
return $node instanceof Node\Stmt\Catch_
&& count($node->types) > 1;
}

/**
* @psalm-mutation-free
*
* @return iterable<Node\Stmt\Catch_>
*/
public function mutate(Node $node): iterable
{
foreach ($node->types as $i => $type) {
$types = $node->types;

unset($types[$i]);

yield new Node\Stmt\Catch_($types, $node->var, $node->stmts, $node->getAttributes());
}
}
}
2 changes: 2 additions & 0 deletions src/Mutator/ProfileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ final class ProfileList
public const OPERATOR_PROFILE = [
Mutator\Operator\AssignCoalesce::class,
Mutator\Operator\Break_::class,
Mutator\Operator\Catch_::class,
Mutator\Operator\Coalesce::class,
Mutator\Operator\Concat::class,
Mutator\Operator\Continue_::class,
Expand Down Expand Up @@ -369,6 +370,7 @@ final class ProfileList
'SpreadRemoval' => Mutator\Operator\SpreadRemoval::class,
'Ternary' => Mutator\Operator\Ternary::class,
'Throw_' => Mutator\Operator\Throw_::class,
'Catch_' => Mutator\Operator\Catch_::class,

// Regex
'PregMatchMatches' => Mutator\Regex\PregMatchMatches::class,
Expand Down
97 changes: 97 additions & 0 deletions tests/phpunit/Mutator/Operator/Catch_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Tests\Mutator\Operator;

use Infection\Tests\Mutator\BaseMutatorTestCase;

final class Catch_Test extends BaseMutatorTestCase
{
/**
* @dataProvider mutationsProvider
*
* @param string|string[] $expected
*/
public function test_it_can_mutate(string $input, array|string $expected = []): void
{
$this->doTest($input, $expected);
}

public function mutationsProvider(): iterable
{
yield 'It removes multiple exceptions using pipe (|) character used' => [
'<?php
try {
$fn();
} catch (\Throwable | \Exception | \DomainException $e) {
throw $e;
}',
[
'<?php
try {
$fn();
} catch (\Exception|\DomainException $e) {
throw $e;
}',
'<?php
try {
$fn();
} catch (\Throwable|\DomainException $e) {
throw $e;
}',
'<?php
try {
$fn();
} catch (\Throwable|\Exception $e) {
throw $e;
}',
],
];

yield 'It does not mutate when pipe (|) character is not used' => [
'<?php
try {
$fn();
} catch (\Throwable $e) {
throw $e;
}',
];
}
}

0 comments on commit 6bc6cfe

Please sign in to comment.