| Conditions | 10 |
| Paths | 18 |
| Total Lines | 41 |
| Code Lines | 21 |
| 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 |
||
| 149 | public function normalizeSelect(array|ExpressionInterface|string $columns): array |
||
| 150 | { |
||
| 151 | if ($columns instanceof ExpressionInterface) { |
||
| 152 | $columns = [$columns]; |
||
| 153 | } elseif (!is_array($columns)) { |
||
| 154 | $columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
||
| 155 | } |
||
| 156 | |||
| 157 | $select = []; |
||
| 158 | |||
| 159 | /** @psalm-var array<array-key, ExpressionInterface|string> $columns */ |
||
| 160 | foreach ($columns as $columnAlias => $columnDefinition) { |
||
| 161 | if (is_string($columnAlias)) { |
||
| 162 | // Already in the normalized format, good for them. |
||
| 163 | $select[$columnAlias] = $columnDefinition; |
||
| 164 | continue; |
||
| 165 | } |
||
| 166 | |||
| 167 | if (is_string($columnDefinition)) { |
||
| 168 | if ( |
||
| 169 | preg_match('/^(.*?)(?i:\s+as\s+|\s+)([\w\-_.]+)$/', $columnDefinition, $matches) && |
||
| 170 | !preg_match('/^\d+$/', $matches[2]) && |
||
| 171 | !str_contains($matches[2], '.') |
||
| 172 | ) { |
||
| 173 | /** Using "columnName as alias" or "columnName alias" syntax */ |
||
| 174 | $select[$matches[2]] = $matches[1]; |
||
| 175 | continue; |
||
| 176 | } |
||
| 177 | if (!str_contains($columnDefinition, '(')) { |
||
| 178 | /** Normal column name, just alias it to itself to ensure it's not selected twice */ |
||
| 179 | $select[$columnDefinition] = $columnDefinition; |
||
| 180 | continue; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | // Either a string calling a function, DB expression, or sub-query |
||
| 185 | /** @var string */ |
||
| 186 | $select[] = $columnDefinition; |
||
| 187 | } |
||
| 188 | |||
| 189 | return $select; |
||
| 190 | } |
||
| 192 |