1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This code is licensed under the BSD 3-Clause License. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017, Maks Rafalko |
6
|
|
|
* All rights reserved. |
7
|
|
|
* |
8
|
|
|
* Redistribution and use in source and binary forms, with or without |
9
|
|
|
* modification, are permitted provided that the following conditions are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright notice, this |
12
|
|
|
* list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, |
15
|
|
|
* this list of conditions and the following disclaimer in the documentation |
16
|
|
|
* and/or other materials provided with the distribution. |
17
|
|
|
* |
18
|
|
|
* * Neither the name of the copyright holder nor the names of its |
19
|
|
|
* contributors may be used to endorse or promote products derived from |
20
|
|
|
* this software without specific prior written permission. |
21
|
|
|
* |
22
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
23
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
25
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
26
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
28
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
29
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
30
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
declare(strict_types=1); |
35
|
|
|
|
36
|
|
|
namespace Infection\Mutator\Removal; |
37
|
|
|
|
38
|
|
|
use Infection\Mutator\Definition; |
39
|
|
|
use Infection\Mutator\GetMutatorName; |
40
|
|
|
use Infection\Mutator\Mutator; |
41
|
|
|
use Infection\Mutator\MutatorCategory; |
42
|
|
|
use PhpParser\Node; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @internal |
46
|
|
|
*/ |
47
|
|
|
final class SharedCaseRemoval implements Mutator |
48
|
|
|
{ |
49
|
|
|
use GetMutatorName; |
50
|
|
|
|
51
|
|
|
public static function getDefinition(): ?Definition |
52
|
|
|
{ |
53
|
|
|
return new Definition( |
54
|
|
|
'Removes `case`s from `switch`.', |
55
|
|
|
MutatorCategory::SEMANTIC_REDUCTION, |
56
|
|
|
null |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function canMutate(Node $node): bool |
61
|
|
|
{ |
62
|
|
|
if (!$node instanceof Node\Stmt\Switch_) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($node->cases as $case) { |
67
|
|
|
if ($case->stmts === []) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Node\Stmt\Switch_ $node |
77
|
|
|
* |
78
|
|
|
* @return iterable<Node\Stmt\Switch_> |
79
|
|
|
*/ |
80
|
|
|
public function mutate(Node $node): iterable |
81
|
|
|
{ |
82
|
|
|
$previousWasEmpty = false; |
83
|
|
|
|
84
|
|
|
foreach ($node->cases as $i => $case) { |
85
|
|
|
if ($case->stmts === []) { |
86
|
|
|
$previousWasEmpty = true; |
87
|
|
|
$cases = $node->cases; |
|
|
|
|
88
|
|
|
unset($cases[$i]); |
89
|
|
|
|
90
|
|
|
yield new Node\Stmt\Switch_( |
91
|
|
|
$node->cond, |
|
|
|
|
92
|
|
|
$cases, |
93
|
|
|
$node->getAttributes() |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($previousWasEmpty) { |
100
|
|
|
$previousWasEmpty = false; |
101
|
|
|
$cases = $node->cases; |
102
|
|
|
unset($cases[$i]); |
103
|
|
|
$lastCase = $cases[$i - 1]; |
104
|
|
|
$cases[$i - 1] = new Node\Stmt\Case_( |
105
|
|
|
$lastCase->cond, |
106
|
|
|
$case->stmts, |
107
|
|
|
$lastCase->getAttributes() |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
yield new Node\Stmt\Switch_( |
111
|
|
|
$node->cond, |
112
|
|
|
$cases, |
113
|
|
|
$node->getAttributes() |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|