| Total Complexity | 45 |
| Total Lines | 458 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Grammar 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 Grammar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Grammar extends IlluminateQueryGrammar |
||
| 23 | { |
||
| 24 | use CompilesAggregates; |
||
| 25 | use CompilesColumns; |
||
|
|
|||
| 26 | use CompilesFilters; |
||
| 27 | use CompilesDataManipulations; |
||
| 28 | use CompilesJoins; |
||
| 29 | use CompilesGroups; |
||
| 30 | use CompilesUnions; |
||
| 31 | use ConvertsIdToKey; |
||
| 32 | use HandlesAqlGrammar; |
||
| 33 | use Macroable; |
||
| 34 | |||
| 35 | public string $name; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The grammar table prefix. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $tablePrefix = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The grammar table prefix. |
||
| 46 | * |
||
| 47 | * @var null|int |
||
| 48 | */ |
||
| 49 | protected $offset = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The grammar specific operators. |
||
| 53 | * |
||
| 54 | * @var array<string> |
||
| 55 | */ |
||
| 56 | protected $operators = [ |
||
| 57 | '==', '!=', '<', '>', '<=', '>=', |
||
| 58 | 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', '=~', '!~', |
||
| 59 | 'ALL ==', 'ALL !=', 'ALL <', 'ALL >', 'ALL <=', 'ALL >=', 'ALL IN', 'ALL NOT IN', |
||
| 60 | 'ANY ==', 'ANY !=', 'ANY <', 'ANY >', 'ANY <=', 'ANY >=', 'ANY IN', 'ANY NOT IN', |
||
| 61 | 'NONE ==', 'NONE !=', 'NONE <', 'NONE >', 'NONE <=', 'NONE >=', 'NONE IN', 'NONE NOT IN', |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The components that make up a select clause. |
||
| 66 | * |
||
| 67 | * @var array<string> |
||
| 68 | */ |
||
| 69 | protected $selectComponents = [ |
||
| 70 | 'preIterationVariables', |
||
| 71 | 'from', |
||
| 72 | 'search', |
||
| 73 | 'joins', |
||
| 74 | 'postIterationVariables', |
||
| 75 | 'wheres', |
||
| 76 | 'groups', |
||
| 77 | 'aggregate', |
||
| 78 | 'havings', |
||
| 79 | 'orders', |
||
| 80 | 'offset', |
||
| 81 | 'limit', |
||
| 82 | 'columns', |
||
| 83 | ]; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array<string, string> |
||
| 87 | */ |
||
| 88 | protected array $operatorTranslations = [ |
||
| 89 | '=' => '==', |
||
| 90 | '<>' => '!=', |
||
| 91 | '<=>' => '==', |
||
| 92 | 'rlike' => '=~', |
||
| 93 | 'not rlike' => '!~', |
||
| 94 | 'regexp' => '=~', |
||
| 95 | 'not regexp' => '!~', |
||
| 96 | ]; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array<string, string> |
||
| 100 | */ |
||
| 101 | protected array $whereTypeOperators = [ |
||
| 102 | 'In' => 'IN', |
||
| 103 | 'NotIn' => 'NOT IN', |
||
| 104 | ]; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The grammar specific bitwise operators. |
||
| 108 | * |
||
| 109 | * @var array<string> |
||
| 110 | */ |
||
| 111 | public $bitwiseOperators = [ |
||
| 112 | '&', '|', '^', '<<', '>>', '~', |
||
| 113 | ]; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get the format for database stored dates. |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getDateFormat() |
||
| 121 | { |
||
| 122 | return config('arangodb.datetime_format'); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the grammar specific operators. |
||
| 127 | * |
||
| 128 | * @return array<string> |
||
| 129 | */ |
||
| 130 | public function getOperators() |
||
| 131 | { |
||
| 132 | return $this->operators; |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | public function translateOperator(string $operator): string |
||
| 137 | { |
||
| 138 | if (array_key_exists($operator, $this->operatorTranslations)) { |
||
| 139 | return $this->operatorTranslations[$operator]; |
||
| 140 | } |
||
| 141 | |||
| 142 | return $operator; |
||
| 143 | } |
||
| 144 | |||
| 145 | protected function prefixTable(Expression|bool|float|int|string|null $table): string |
||
| 146 | { |
||
| 147 | return $this->tablePrefix . $this->getValue($table); |
||
| 148 | } |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the appropriate query parameter place-holder for a value. |
||
| 153 | * |
||
| 154 | * @param mixed $value |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function parameter($value) |
||
| 158 | { |
||
| 159 | |||
| 160 | return $this->isExpression($value) ? $this->getValue($value) : $value; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Compile the components necessary for a select clause. |
||
| 165 | * |
||
| 166 | * @param IlluminateQueryBuilder $query |
||
| 167 | * @return array<string, string> |
||
| 168 | */ |
||
| 169 | protected function compileComponents(IlluminateQueryBuilder $query) |
||
| 170 | { |
||
| 171 | $aql = []; |
||
| 172 | |||
| 173 | foreach ($this->selectComponents as $component) { |
||
| 174 | if ($component === 'unions') { |
||
| 175 | continue; |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($component === 'aggregate' && $query->unions !== null) { |
||
| 179 | continue; |
||
| 180 | } |
||
| 181 | |||
| 182 | if (isset($query->$component)) { |
||
| 183 | $method = 'compile' . ucfirst($component); |
||
| 184 | |||
| 185 | $aql[$component] = $this->$method($query, $query->$component); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | return $aql; |
||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Compile a select query into SQL. |
||
| 196 | * |
||
| 197 | * @param IlluminateQueryBuilder $query |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function compileSelect(IlluminateQueryBuilder $query) |
||
| 201 | { |
||
| 202 | assert($query instanceof Builder); |
||
| 203 | |||
| 204 | // If the query does not have any columns set, we'll set the columns to the |
||
| 205 | // * character to just get all of the columns from the database. Then we |
||
| 206 | // can build the query and concatenate all the pieces together as one. |
||
| 207 | $original = $query->columns; |
||
| 208 | |||
| 209 | if (empty($query->columns) || $query->unions !== null) { |
||
| 210 | $query->columns = ['*']; |
||
| 211 | } |
||
| 212 | |||
| 213 | // To compile the query, we'll spin through each component of the query and |
||
| 214 | // see if that component exists. If it does we'll just call the compiler |
||
| 215 | // function for the component which is responsible for making the SQL. |
||
| 216 | |||
| 217 | $aql = trim( |
||
| 218 | $this->concatenate( |
||
| 219 | $this->compileComponents($query), |
||
| 220 | ), |
||
| 221 | ); |
||
| 222 | |||
| 223 | $query->columns = $original; |
||
| 224 | |||
| 225 | if ($query->unions) { |
||
| 226 | return $this->compileUnions($query, $aql); |
||
| 227 | } |
||
| 228 | |||
| 229 | if ($query->groupVariables !== null) { |
||
| 230 | $query->cleanGroupVariables(); |
||
| 231 | } |
||
| 232 | |||
| 233 | return $aql; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Compile the "from" portion of the query -> FOR in AQL. |
||
| 238 | * |
||
| 239 | * @param IlluminateQueryBuilder $query |
||
| 240 | * @param Expression|string $table |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | protected function compileFrom(IlluminateQueryBuilder $query, $table) |
||
| 245 | { |
||
| 246 | assert($query instanceof Builder); |
||
| 247 | |||
| 248 | // FIXME: wrapping/quoting |
||
| 249 | $table = $this->prefixTable($this->getValue($table)); |
||
| 250 | |||
| 251 | $alias = $query->registerTableAlias($table); |
||
| 252 | |||
| 253 | $aql = "FOR $alias IN $table"; |
||
| 254 | |||
| 255 | if (!empty($query->fromOptions)) { |
||
| 256 | $aql .= $this->compileFromOptions($query->fromOptions); |
||
| 257 | } |
||
| 258 | |||
| 259 | return $aql; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Compile AQL options for the "from" portion of the query. |
||
| 264 | * |
||
| 265 | * @param array<mixed> $options |
||
| 266 | * |
||
| 267 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||
| 268 | */ |
||
| 269 | protected function compileFromOptions($options): string |
||
| 270 | { |
||
| 271 | return ' OPTIONS ' . $this->generateAqlObject($options); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param IlluminateQueryBuilder $query |
||
| 276 | * @param array<string, mixed> $variables |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | protected function compilePreIterationVariables(IlluminateQueryBuilder $query, array $variables): string |
||
| 280 | { |
||
| 281 | return $this->compileVariables($query, $variables); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param IlluminateQueryBuilder $query |
||
| 286 | * @param array<string, mixed> $variables |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | protected function compilePostIterationVariables(IlluminateQueryBuilder $query, array $variables): string |
||
| 290 | { |
||
| 291 | return $this->compileVariables($query, $variables); |
||
| 292 | } |
||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * @param IlluminateQueryBuilder $query |
||
| 297 | * @param array<string, mixed> $variables |
||
| 298 | * @return string |
||
| 299 | * |
||
| 300 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||
| 301 | */ |
||
| 302 | protected function compileVariables(IlluminateQueryBuilder $query, array $variables): string |
||
| 303 | { |
||
| 304 | $aql = ''; |
||
| 305 | |||
| 306 | foreach ($variables as $variable => $value) { |
||
| 307 | if ($value instanceof Expression) { |
||
| 308 | $value = $value->getValue($this); |
||
| 309 | } |
||
| 310 | |||
| 311 | $aql .= ' LET ' . $variable . ' = ' . $value; |
||
| 312 | } |
||
| 313 | |||
| 314 | return trim($aql); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Compile the "order by" portions of the query. |
||
| 319 | * |
||
| 320 | * @param Builder $query |
||
| 321 | * @param array<mixed> $orders |
||
| 322 | * @param null|string $table |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | protected function compileOrders(IlluminateQueryBuilder $query, $orders, $table = null) |
||
| 326 | { |
||
| 327 | if (!empty($orders)) { |
||
| 328 | return 'SORT ' . implode(', ', $this->compileOrdersToArray($query, $orders, $table)); |
||
| 329 | } |
||
| 330 | |||
| 331 | return ''; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Compile the query orders to an array. |
||
| 336 | * |
||
| 337 | * @param Builder $query |
||
| 338 | * @param array<mixed> $orders |
||
| 339 | * @param null|string $table |
||
| 340 | * @return array<string> |
||
| 341 | * @throws \Exception |
||
| 342 | */ |
||
| 343 | protected function compileOrdersToArray(IlluminateQueryBuilder $query, $orders, $table = null) |
||
| 344 | { |
||
| 345 | return array_map(function ($order) use ($query, $table) { |
||
| 346 | $key = 'column'; |
||
| 347 | if (array_key_exists('sql', $order)) { |
||
| 348 | $key = 'sql'; |
||
| 349 | } |
||
| 350 | |||
| 351 | if (!$order[$key] instanceof Expression) { |
||
| 352 | $order[$key] = $this->normalizeColumn($query, $order[$key], $table); |
||
| 353 | } |
||
| 354 | |||
| 355 | if ($order[$key] instanceof Expression) { |
||
| 356 | $order[$key] = $order[$key]->getValue($this); |
||
| 357 | } |
||
| 358 | |||
| 359 | return array_key_exists('direction', $order) ? $order[$key] . ' ' . $order['direction'] : $order[$key]; |
||
| 360 | }, $orders); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Compile the "offset" portions of the query. |
||
| 365 | * |
||
| 366 | * @param \Illuminate\Database\Query\Builder $query |
||
| 367 | * @param int $offset |
||
| 368 | * @return string |
||
| 369 | * |
||
| 370 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||
| 371 | */ |
||
| 372 | protected function compileOffset(IlluminateQueryBuilder $query, $offset) |
||
| 373 | { |
||
| 374 | $this->offset = (int) $offset; |
||
| 375 | |||
| 376 | return ""; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Compile the "limit" portions of the query. |
||
| 381 | * |
||
| 382 | * @param \Illuminate\Database\Query\Builder $query |
||
| 383 | * @param int $limit |
||
| 384 | * @return string |
||
| 385 | * |
||
| 386 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||
| 387 | */ |
||
| 388 | protected function compileLimit(IlluminateQueryBuilder $query, $limit) |
||
| 389 | { |
||
| 390 | if ($this->offset !== null) { |
||
| 391 | return "LIMIT " . (int) $this->offset . ", " . (int) $limit; |
||
| 392 | } |
||
| 393 | |||
| 394 | return "LIMIT " . (int) $limit; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Compile the random statement into SQL. |
||
| 399 | * |
||
| 400 | * @param string|int|null $seed |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function compileRandom($seed = null) |
||
| 404 | { |
||
| 405 | unset($seed); |
||
| 406 | |||
| 407 | return "RAND()"; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param IlluminateQueryBuilder $query |
||
| 412 | * @param array<mixed> $search |
||
| 413 | * @return string |
||
| 414 | * @throws \Exception |
||
| 415 | */ |
||
| 416 | public function compileSearch(IlluminateQueryBuilder $query, array $search) |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get the value of a raw expression. |
||
| 435 | * |
||
| 436 | * @param bool|float|Expression|int|string|null $expression |
||
| 437 | * @return bool|float|int|string|null |
||
| 438 | */ |
||
| 439 | public function getValue($expression) |
||
| 440 | { |
||
| 441 | if ($expression instanceof Expression) { |
||
| 442 | return $expression->getValue($this); |
||
| 443 | } |
||
| 444 | |||
| 445 | return $expression; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get the grammar specific bit operators. |
||
| 450 | * |
||
| 451 | * @return array<string> |
||
| 452 | */ |
||
| 453 | public function getBitwiseOperators() |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Prepare the bindings for a delete statement. |
||
| 460 | * |
||
| 461 | * @param array<mixed> $bindings |
||
| 462 | * @return array<mixed> |
||
| 463 | */ |
||
| 464 | public function prepareBindingsForDelete(array $bindings) |
||
| 465 | { |
||
| 466 | return Arr::collapse( |
||
| 467 | Arr::except($bindings, 'select'), |
||
| 468 | ); |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Determine if the given value is a raw expression. |
||
| 473 | * |
||
| 474 | * @param mixed $value |
||
| 475 | * @return bool |
||
| 476 | */ |
||
| 477 | public function isExpression($value) |
||
| 480 | } |
||
| 481 | } |
||
| 482 |