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\Number; |
||
37 | |||
38 | use function in_array; |
||
39 | use Infection\Mutator\Definition; |
||
40 | use Infection\Mutator\GetMutatorName; |
||
41 | use Infection\Mutator\MutatorCategory; |
||
42 | use Infection\PhpParser\Visitor\ParentConnector; |
||
43 | use PhpParser\Node; |
||
44 | |||
45 | /** |
||
46 | * @internal |
||
47 | */ |
||
48 | final class DecrementInteger extends AbstractNumberMutator |
||
49 | { |
||
50 | use GetMutatorName; |
||
51 | |||
52 | private const COUNT_NAMES = [ |
||
53 | 'count', |
||
54 | 'grapheme_strlen', |
||
55 | 'iconv_strlen', |
||
56 | 'mb_strlen', |
||
57 | 'sizeof', |
||
58 | 'strlen', |
||
59 | ]; |
||
60 | |||
61 | public static function getDefinition(): ?Definition |
||
62 | { |
||
63 | return new Definition( |
||
64 | 'Decrements an integer value with 1.', |
||
65 | MutatorCategory::ORTHOGONAL_REPLACEMENT, |
||
66 | null |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param Node\Scalar\LNumber $node |
||
72 | * |
||
73 | * @return iterable<Node\Scalar\LNumber> |
||
74 | */ |
||
75 | public function mutate(Node $node): iterable |
||
76 | { |
||
77 | yield new Node\Scalar\LNumber($node->value - 1); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
78 | } |
||
79 | |||
80 | public function canMutate(Node $node): bool |
||
81 | { |
||
82 | if (!$node instanceof Node\Scalar\LNumber || $node->value === 1) { |
||
83 | return false; |
||
84 | } |
||
85 | |||
86 | if ($this->isArrayZeroIndexAccess($node)) { |
||
87 | return false; |
||
88 | } |
||
89 | |||
90 | if ($this->isPartOfSizeComparison($node)) { |
||
91 | return false; |
||
92 | } |
||
93 | |||
94 | return $this->isAllowedComparison($node); |
||
95 | } |
||
96 | |||
97 | private function isAllowedComparison(Node\Scalar\LNumber $node): bool |
||
98 | { |
||
99 | if ($node->value !== 0) { |
||
100 | return true; |
||
101 | } |
||
102 | |||
103 | $parentNode = ParentConnector::getParent($node); |
||
104 | |||
105 | if (!$this->isComparison($parentNode)) { |
||
106 | return true; |
||
107 | } |
||
108 | /** @var Node\Expr\BinaryOp $parentNode */ |
||
109 | if ($parentNode->left instanceof Node\Expr\FuncCall && $parentNode->left->name instanceof Node\Name |
||
110 | && in_array( |
||
111 | $parentNode->left->name->toLowerString(), |
||
112 | self::COUNT_NAMES, |
||
113 | true |
||
114 | ) |
||
115 | ) { |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | if ($parentNode->right instanceof Node\Expr\FuncCall && $parentNode->right->name instanceof Node\Name |
||
120 | && in_array( |
||
121 | $parentNode->right->name->toLowerString(), |
||
122 | self::COUNT_NAMES, |
||
123 | true |
||
124 | ) |
||
125 | ) { |
||
126 | return false; |
||
127 | } |
||
128 | |||
129 | return true; |
||
130 | } |
||
131 | |||
132 | private function isComparison(Node $parentNode): bool |
||
133 | { |
||
134 | return $parentNode instanceof Node\Expr\BinaryOp\Identical |
||
135 | || $parentNode instanceof Node\Expr\BinaryOp\NotIdentical |
||
136 | || $parentNode instanceof Node\Expr\BinaryOp\Equal |
||
137 | || $parentNode instanceof Node\Expr\BinaryOp\NotEqual |
||
138 | || $parentNode instanceof Node\Expr\BinaryOp\Greater |
||
139 | || $parentNode instanceof Node\Expr\BinaryOp\GreaterOrEqual |
||
140 | || $parentNode instanceof Node\Expr\BinaryOp\Smaller |
||
141 | || $parentNode instanceof Node\Expr\BinaryOp\SmallerOrEqual |
||
142 | ; |
||
143 | } |
||
144 | |||
145 | private function isArrayZeroIndexAccess(Node $node): bool |
||
146 | { |
||
147 | if (!$node instanceof Node\Scalar\LNumber) { |
||
148 | return false; |
||
149 | } |
||
150 | |||
151 | /** @var Node\Scalar\LNumber $node */ |
||
152 | if ($node->value !== 0) { |
||
153 | return false; |
||
154 | } |
||
155 | |||
156 | if (ParentConnector::getParent($node) instanceof Node\Expr\ArrayDimFetch) { |
||
157 | return true; |
||
158 | } |
||
159 | |||
160 | return false; |
||
161 | } |
||
162 | } |
||
163 |