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