| 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 |
||
| 160 | private function resolveOptionalKeys(Dot $dot, array $sqlParts, array &$params = []): string |
||
| 161 | { |
||
| 162 | $paramKeys = array_keys($params); |
||
| 163 | |||
| 164 | // Se recorren los sqlParts y por cada uno se van agregando los filtros opcionales. |
||
| 165 | // El query principal se evalúa al final |
||
| 166 | foreach ($sqlParts as $sqlPartKey => $sql) { |
||
| 167 | $snippetIds = []; |
||
| 168 | |||
| 169 | $whereConnector = (false === strpos(strtoupper($sql), 'WHERE')) ? 'WHERE ' : 'AND '; |
||
| 170 | |||
| 171 | preg_match_all(self::OPTIONAL_ID_PATTERN, $sql, $snippetIds); |
||
| 172 | |||
| 173 | foreach ($snippetIds[0] as $snippetId) { |
||
| 174 | $requestedSnippets = []; |
||
| 175 | |||
| 176 | preg_match(self::KEY_PATTERN, $snippetId, $matches); |
||
| 177 | |||
| 178 | $snippetKey = $matches[0]; |
||
| 179 | |||
| 180 | if (empty($snippetKey)) { |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | if (!$dot->has($snippetKey)) { |
||
| 185 | throw new NonExistentQueryKeyException($snippetKey); |
||
| 186 | } |
||
| 187 | |||
| 188 | $snippets = $dot->get($snippetKey); |
||
| 189 | |||
| 190 | foreach ($snippets as $filter) { |
||
| 191 | $filterType = null; |
||
| 192 | |||
| 193 | if (is_array($filter)) { |
||
| 194 | $filterType = key($filter); |
||
| 195 | $filter = $filter[$filterType]; |
||
| 196 | } |
||
| 197 | |||
| 198 | foreach ($paramKeys as $paramKey) { |
||
| 199 | if (false !== strpos($filter, ':'.$paramKey)) { |
||
| 200 | $event = new ProcessQueryParamsEvent($snippetKey, $filterType, $paramKey, $params, $filter); |
||
| 201 | |||
| 202 | if ($this->eventDispatcher) { |
||
| 203 | $this->eventDispatcher->dispatch($event); |
||
| 204 | } |
||
| 205 | |||
| 206 | $params = $event->getProcessedParams(); |
||
| 207 | $filter = $event->getProcessedFilter(); |
||
| 208 | |||
| 209 | // Si no existe en la lista de filtros a usar, se agrega |
||
| 210 | if (!in_array($filter, array_keys($requestedSnippets))) { |
||
| 211 | $requestedSnippets[$filter] = $whereConnector.'('.$filter.')'; |
||
| 212 | } |
||
| 213 | |||
| 214 | $whereConnector = 'AND '; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | if (!empty($requestedSnippets)) { |
||
| 220 | $snippetSql = implode(' ', $requestedSnippets); |
||
| 221 | |||
| 222 | $sqlParts[$sqlPartKey] = str_replace($snippetId, $snippetSql, $sql); |
||
| 223 | } else { |
||
| 224 | $sqlParts[$sqlPartKey] = str_replace($snippetId, '', $sql); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | // Obtiene la última posición del array que corresponde al SQL base |
||
| 230 | $sql = array_pop($sqlParts); |
||
| 231 | |||
| 232 | // Recorre las partes y las va reemplazando en el SQL base |
||
| 233 | foreach ($sqlParts as $sqlPartKey => $sqlPart) { |
||
| 234 | $sql = str_replace($sqlPartKey, $sqlPart, $sql); |
||
| 235 | } |
||
| 236 | |||
| 237 | return $sql; |
||
| 238 | } |
||
| 240 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.