| Total Complexity | 40 |
| Total Lines | 166 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like QueryHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | final class QueryHelper |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Removes {@see isEmpty()|empty operands} from the given query condition. |
||
| 28 | * |
||
| 29 | * @param array|string $condition the original condition |
||
| 30 | * |
||
| 31 | * @return array|string the condition with {@see isEmpty()|empty operands} removed. |
||
| 32 | */ |
||
| 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 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns a value indicating whether the give value is "empty". |
||
| 98 | * |
||
| 99 | * The value is considered "empty", if one of the following conditions is satisfied: |
||
| 100 | * |
||
| 101 | * - it is `null`, |
||
| 102 | * - an empty string (`''`), |
||
| 103 | * - a string containing only whitespace characters, |
||
| 104 | * - or an empty array. |
||
| 105 | * |
||
| 106 | * @param mixed $value |
||
| 107 | * |
||
| 108 | * @return bool if the value is empty |
||
| 109 | */ |
||
| 110 | public function isEmpty(mixed $value): bool |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Normalizes format of ORDER BY data. |
||
| 117 | * |
||
| 118 | * @param array|ExpressionInterface|string $columns the columns value to normalize. |
||
| 119 | * |
||
| 120 | * See {@see orderBy} and {@see addOrderBy}. |
||
| 121 | */ |
||
| 122 | public function normalizeOrderBy(array|string|ExpressionInterface $columns): array |
||
| 123 | { |
||
| 124 | if ($columns instanceof ExpressionInterface) { |
||
| 125 | return [$columns]; |
||
| 126 | } |
||
| 127 | |||
| 128 | if (is_array($columns)) { |
||
| 129 | return $columns; |
||
| 130 | } |
||
| 131 | |||
| 132 | $columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
||
| 133 | $result = []; |
||
| 134 | |||
| 135 | foreach ($columns as $column) { |
||
| 136 | if (preg_match('/^(.*?)\s+(asc|desc)$/i', $column, $matches)) { |
||
| 137 | $result[$matches[1]] = strcasecmp($matches[2], 'desc') ? SORT_ASC : SORT_DESC; |
||
| 138 | } else { |
||
| 139 | $result[$column] = SORT_ASC; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | return $result; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Normalizes the SELECT columns passed to {@see select()} or {@see addSelect()}. |
||
| 148 | */ |
||
| 149 | public function normalizeSelect(array|ExpressionInterface|string $columns): array |
||
| 190 | } |
||
| 191 | } |
||
| 192 |