| Conditions | 18 |
| Paths | 70 |
| Total Lines | 62 |
| Code Lines | 49 |
| 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 |
||
| 24 | public function evaluate($string) |
||
| 25 | { |
||
| 26 | $string = '<?php ' . $string; |
||
| 27 | $comparison1 = $comparison2 = ''; |
||
| 28 | $operator = null; |
||
| 29 | $tokens = token_get_all($string); |
||
| 30 | array_shift($tokens); |
||
| 31 | foreach ($tokens as $token) { |
||
| 32 | if (is_array($token)) { |
||
| 33 | if (in_array($token[0], $this->operators)) { |
||
| 34 | $operator = $token[1]; |
||
| 35 | } else if ($token[0] == T_STRING || $token[0] == T_WHITESPACE || $token[0] == T_LNUMBER) { |
||
| 36 | if ($operator === null) { |
||
| 37 | $comparison1 .= $token[1]; |
||
| 38 | } else { |
||
| 39 | $comparison2 .= $token[1]; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } else if (in_array($token, $this->stringOperators)) { |
||
| 43 | $operator = $token; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | $comparison1 = trim($comparison1); |
||
| 48 | $comparison2 = trim($comparison2); |
||
| 49 | |||
| 50 | if ($operator === null) { |
||
| 51 | $isTrue = in_array($comparison1, $this->reservedTrue); |
||
| 52 | if ($isTrue) return true; |
||
| 53 | |||
| 54 | $isFalse = in_array($comparison1, $this->reservedFalse); |
||
| 55 | if ($isFalse) return false; |
||
| 56 | |||
| 57 | return (boolean)$comparison1; |
||
| 58 | } else { |
||
| 59 | switch ($operator) { |
||
| 60 | case '==': |
||
| 61 | return $comparison1 == $comparison2; |
||
| 62 | break; |
||
|
|
|||
| 63 | case '!=': |
||
| 64 | return $comparison1 != $comparison2; |
||
| 65 | break; |
||
| 66 | case '>': |
||
| 67 | return $comparison1 > $comparison2; |
||
| 68 | break; |
||
| 69 | case '<': |
||
| 70 | return $comparison1 < $comparison2; |
||
| 71 | break; |
||
| 72 | case '>=': |
||
| 73 | return $comparison1 >= $comparison2; |
||
| 74 | break; |
||
| 75 | case '<=': |
||
| 76 | return $comparison1 <= $comparison2; |
||
| 77 | break; |
||
| 78 | default: |
||
| 79 | return false; |
||
| 80 | break; |
||
| 81 | |||
| 82 | } |
||
| 83 | } |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 88 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.