| Conditions | 14 |
| Paths | 116 |
| Total Lines | 78 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 183 | private function resolveOptionalKeys(Dot $dot, array $sqlParts, array &$params = []): string |
||
| 184 | { |
||
| 185 | $paramKeys = array_keys($params); |
||
| 186 | |||
| 187 | // Se recorren los sqlParts y por cada uno se van agregando los filtros opcionales. |
||
| 188 | // El query principal se evalúa al final |
||
| 189 | foreach ($sqlParts as $sqlPartKey => $sql) { |
||
| 190 | $snippetIds = []; |
||
| 191 | |||
| 192 | $whereConnector = (false === strpos(strtoupper($sql), 'WHERE')) ? 'WHERE ' : 'AND '; |
||
| 193 | |||
| 194 | preg_match_all(self::OPTIONAL_ID_PATTERN, $sql, $snippetIds); |
||
| 195 | |||
| 196 | foreach ($snippetIds[0] as $snippetId) { |
||
| 197 | $requestedSnippets = []; |
||
| 198 | |||
| 199 | preg_match(self::KEY_PATTERN, $snippetId, $matches); |
||
| 200 | |||
| 201 | $snippetKey = $matches[0]; |
||
| 202 | |||
| 203 | if (empty($snippetKey)) { |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | |||
| 207 | if (!$dot->has($snippetKey)) { |
||
| 208 | throw new NonExistentQueryKeyException($snippetKey); |
||
| 209 | } |
||
| 210 | |||
| 211 | $snippets = $dot->get($snippetKey); |
||
| 212 | |||
| 213 | foreach ($snippets as $filter) { |
||
| 214 | $filterType = null; |
||
| 215 | |||
| 216 | if (is_array($filter)) { |
||
| 217 | $filterType = key($filter); |
||
| 218 | $filter = $filter[$filterType]; |
||
| 219 | } |
||
| 220 | |||
| 221 | foreach ($paramKeys as $paramKey) { |
||
| 222 | if (false !== strpos($filter, ':'.$paramKey)) { |
||
| 223 | $event = new ProcessQueryParamsEvent($snippetKey, $filterType, $paramKey, $params, $filter); |
||
| 224 | |||
| 225 | if ($this->eventDispatcher) { |
||
| 226 | $this->eventDispatcher->dispatch(NativeQueryFromFileBuilderEvents::PROCESS_QUERY_PARAMS, $event); |
||
| 227 | } |
||
| 228 | |||
| 229 | $params = $event->getProcessedParams(); |
||
| 230 | $filter = $event->getProcessedFilter(); |
||
| 231 | |||
| 232 | // Si no existe en la lista de filtros a usar, se agrega |
||
| 233 | if (!in_array($filter, array_keys($requestedSnippets))) { |
||
| 234 | $requestedSnippets[$filter] = $whereConnector.'('.$filter.')'; |
||
| 235 | } |
||
| 236 | |||
| 237 | $whereConnector = 'AND '; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | if (!empty($requestedSnippets)) { |
||
| 243 | $snippetSql = implode(' ', $requestedSnippets); |
||
| 244 | |||
| 245 | $sqlParts[$sqlPartKey] = str_replace($snippetId, $snippetSql, $sql); |
||
| 246 | } else { |
||
| 247 | $sqlParts[$sqlPartKey] = str_replace($snippetId, '', $sql); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | // Obtiene la última posición del array que corresponde al SQL base |
||
| 253 | $sql = array_pop($sqlParts); |
||
| 254 | |||
| 255 | // Recorre las partes y las va reemplazando en el SQL base |
||
| 256 | foreach ($sqlParts as $sqlPartKey => $sqlPart) { |
||
| 257 | $sql = str_replace($sqlPartKey, $sqlPart, $sql); |
||
| 258 | } |
||
| 259 | |||
| 260 | return $sql; |
||
| 261 | } |
||
| 263 |