| Conditions | 19 |
| Paths | 18 |
| Total Lines | 61 |
| Code Lines | 34 |
| 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 |
||
| 33 | public function filterCondition(array|string $condition): array|string |
||
| 34 | { |
||
| 35 | if (!is_array($condition)) { |
||
|
|
|||
| 36 | return $condition; |
||
| 37 | } |
||
| 38 | |||
| 39 | if (!isset($condition[0])) { |
||
| 40 | /** hash format: 'column1' => 'value1', 'column2' => 'value2', ... */ |
||
| 41 | /** @var mixed $value */ |
||
| 42 | foreach ($condition as $name => $value) { |
||
| 43 | if ($this->isEmpty($value)) { |
||
| 44 | unset($condition[$name]); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | return $condition; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** operator format: operator, operand 1, operand 2, ... */ |
||
| 52 | /** @var string */ |
||
| 53 | $operator = array_shift($condition); |
||
| 54 | |||
| 55 | switch (strtoupper($operator)) { |
||
| 56 | case 'NOT': |
||
| 57 | case 'AND': |
||
| 58 | case 'OR': |
||
| 59 | /** @psalm-var array<array-key, array|string> $condition */ |
||
| 60 | foreach ($condition as $i => $operand) { |
||
| 61 | $subCondition = $this->filterCondition($operand); |
||
| 62 | if ($this->isEmpty($subCondition)) { |
||
| 63 | unset($condition[$i]); |
||
| 64 | } else { |
||
| 65 | $condition[$i] = $subCondition; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | if (empty($condition)) { |
||
| 70 | return []; |
||
| 71 | } |
||
| 72 | |||
| 73 | break; |
||
| 74 | case 'BETWEEN': |
||
| 75 | case 'NOT BETWEEN': |
||
| 76 | if (array_key_exists(1, $condition) && array_key_exists(2, $condition)) { |
||
| 77 | if ($this->isEmpty($condition[1]) || $this->isEmpty($condition[2])) { |
||
| 78 | return []; |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | return []; |
||
| 82 | } |
||
| 83 | |||
| 84 | break; |
||
| 85 | default: |
||
| 86 | if (array_key_exists(1, $condition) && $this->isEmpty($condition[1])) { |
||
| 87 | return []; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | array_unshift($condition, $operator); |
||
| 92 | |||
| 93 | return $condition; |
||
| 94 | } |
||
| 192 |