| Conditions | 23 |
| Paths | 21 |
| Total Lines | 68 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 19 | public static function evaluate($value, string $rule, array $args): bool |
||
| 20 | { |
||
| 21 | switch ($rule) { |
||
| 22 | case '=': |
||
| 23 | /** |
||
| 24 | * @var scalar $lhs |
||
| 25 | * @var scalar $rhs |
||
| 26 | */ |
||
| 27 | [$lhs, $rhs] = [$value, $args[0]]; |
||
| 28 | if (strval($lhs) === strval($rhs)) { |
||
| 29 | return true; |
||
| 30 | } |
||
| 31 | break; |
||
| 32 | case '!=': |
||
| 33 | /** |
||
| 34 | * @var scalar $lhs |
||
| 35 | * @var scalar $rhs |
||
| 36 | */ |
||
| 37 | [$lhs, $rhs] = [$value, $args[0]]; |
||
| 38 | if (strval($lhs) !== strval($rhs)) { |
||
| 39 | return true; |
||
| 40 | } |
||
| 41 | break; |
||
| 42 | case '>': |
||
| 43 | if ($value > $args[0]) { |
||
| 44 | return true; |
||
| 45 | } |
||
| 46 | break; |
||
| 47 | case '>=': |
||
| 48 | if ($value >= $args[0]) { |
||
| 49 | return true; |
||
| 50 | } |
||
| 51 | break; |
||
| 52 | case '<': |
||
| 53 | if ($value < $args[0]) { |
||
| 54 | return true; |
||
| 55 | } |
||
| 56 | break; |
||
| 57 | case '<=': |
||
| 58 | if ($value <= $args[0]) { |
||
| 59 | return true; |
||
| 60 | } |
||
| 61 | break; |
||
| 62 | case 'between': |
||
| 63 | if ($value >= $args[0] && $value <= $args[1]) { |
||
| 64 | return true; |
||
| 65 | } |
||
| 66 | break; |
||
| 67 | case 'between strict': |
||
| 68 | if ($value > $args[0] && $value < $args[1]) { |
||
| 69 | return true; |
||
| 70 | } |
||
| 71 | break; |
||
| 72 | case 'in': |
||
| 73 | /** @var array{array<mixed>} $args */ |
||
| 74 | if (in_array($value, $args[0])) { |
||
| 75 | return true; |
||
| 76 | } |
||
| 77 | break; |
||
| 78 | case 'not in': |
||
| 79 | /** @var array{array<mixed>} $args */ |
||
| 80 | if (!in_array($value, $args[0])) { |
||
| 81 | return true; |
||
| 82 | } |
||
| 83 | break; |
||
| 84 | } |
||
| 85 | |||
| 86 | return false; |
||
| 87 | } |
||
| 89 |