Conditions | 22 |
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 |
||
137 | protected function evaluateCondition( |
||
138 | $left, |
||
139 | $right, |
||
140 | ?string $operator, |
||
141 | Context $context |
||
142 | ): bool { |
||
143 | if ($operator === null) { |
||
144 | $value = $this->stringValue($context->get($left)); |
||
145 | |||
146 | return (bool) $value; |
||
147 | } |
||
148 | |||
149 | // values of 'empty' have a special meaning in array comparisons |
||
150 | if ($right == 'empty' && is_array($context->get($left))) { |
||
151 | $left = $context->get($left); |
||
152 | $right = 0; |
||
153 | } elseif ($left == 'empty' && is_array($context->get($right))) { |
||
154 | $right = $context->get($right); |
||
155 | $left = 0; |
||
156 | } else { |
||
157 | $leftValue = $context->get($left); |
||
158 | $rightValue = $context->get($right); |
||
159 | |||
160 | $left = $this->stringValue($leftValue); |
||
161 | $right = $this->stringValue($rightValue); |
||
162 | } |
||
163 | |||
164 | // special rules for null values |
||
165 | if (is_null($left) || is_null($right)) { |
||
166 | //null == null => true |
||
167 | if ($operator === '==' && is_null($left) && is_null($right)) { |
||
168 | return true; |
||
169 | } |
||
170 | |||
171 | //null != anything other than null => true |
||
172 | if ($operator === '!=' && (is_null($left) || is_null($right))) { |
||
173 | return true; |
||
174 | } |
||
175 | |||
176 | return false; |
||
177 | } |
||
178 | |||
179 | //regular rules |
||
180 | switch ($operator) { |
||
181 | case '==': |
||
182 | return ($left == $right); |
||
183 | case '!=': |
||
184 | return ($left != $right); |
||
185 | case '>': |
||
186 | return ($left > $right); |
||
187 | case '<': |
||
188 | return ($left < $right); |
||
189 | case '>=': |
||
190 | return ($left >= $right); |
||
191 | case '<=': |
||
192 | return ($left <= $right); |
||
193 | case 'contains': |
||
194 | return (is_array($left) ? in_array($right, $left) : (strpos($left, $right) !== false)); |
||
195 | default: |
||
196 | throw new RenderException(sprintf( |
||
197 | 'Error in tag [%s] - Unknown operator [%s]', |
||
198 | $this->getTagName(), |
||
199 | $operator |
||
200 | )); |
||
204 |