Conditions | 20 |
Paths | 37 |
Total Lines | 63 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
142 | protected function evaluateCondition( |
||
143 | mixed $left, |
||
144 | mixed $right, |
||
145 | ?string $operator, |
||
146 | Context $context |
||
147 | ): bool { |
||
148 | if ($operator === null) { |
||
149 | $value = $this->stringValue($context->get($left)); |
||
150 | |||
151 | return (bool) $value; |
||
152 | } |
||
153 | |||
154 | // values of 'empty' have a special meaning in array comparisons |
||
155 | if ($right == 'empty' && is_array($context->get($left))) { |
||
156 | $left = $context->get($left); |
||
157 | $right = 0; |
||
158 | } elseif ($left == 'empty' && is_array($context->get($right))) { |
||
159 | $right = $context->get($right); |
||
160 | $left = 0; |
||
161 | } else { |
||
162 | $leftValue = $context->get($left); |
||
163 | $rightValue = $context->get($right); |
||
164 | |||
165 | $left = $this->stringValue($leftValue); |
||
166 | $right = $this->stringValue($rightValue); |
||
167 | } |
||
168 | |||
169 | // special rules for null values |
||
170 | if (is_null($left) || is_null($right)) { |
||
171 | //null == null => true |
||
172 | if ($operator === '==' && is_null($left) && is_null($right)) { |
||
173 | return true; |
||
174 | } |
||
175 | |||
176 | //null != anything other than null => true |
||
177 | if ($operator === '!=') { |
||
178 | return true; |
||
179 | } |
||
180 | |||
181 | return false; |
||
182 | } |
||
183 | |||
184 | //regular rules |
||
185 | switch ($operator) { |
||
186 | case '==': |
||
187 | return ($left == $right); |
||
188 | case '!=': |
||
189 | return ($left != $right); |
||
190 | case '>': |
||
191 | return ($left > $right); |
||
192 | case '<': |
||
193 | return ($left < $right); |
||
194 | case '>=': |
||
195 | return ($left >= $right); |
||
196 | case '<=': |
||
197 | return ($left <= $right); |
||
198 | case 'contains': |
||
199 | return (is_array($left) ? in_array($right, $left) : (strpos($left, $right) !== false)); |
||
200 | default: |
||
201 | throw new RenderException(sprintf( |
||
202 | 'Error in tag [%s] - Unknown operator [%s]', |
||
203 | $this->getTagName(), |
||
204 | $operator |
||
205 | )); |
||
209 |