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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
15 | class Grammar extends FluentAqlGrammar |
||
16 | { |
||
17 | use Macroable; |
||
18 | |||
19 | public $name; |
||
20 | |||
21 | /** |
||
22 | * The grammar table prefix. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $tablePrefix = ''; |
||
27 | |||
28 | /** |
||
29 | * The grammar table prefix. |
||
30 | * |
||
31 | * @var null|int |
||
32 | */ |
||
33 | protected $offset = null; |
||
34 | |||
35 | /** |
||
36 | * The components that make up a select clause. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $selectComponents = [ |
||
41 | 'from', |
||
42 | 'joins', |
||
43 | 'wheres', |
||
44 | 'groups', |
||
45 | 'aggregate', |
||
46 | 'havings', |
||
47 | 'orders', |
||
48 | 'offset', |
||
49 | 'limit', |
||
50 | 'columns', |
||
51 | ]; |
||
52 | |||
53 | protected $operatorTranslations = [ |
||
54 | '=' => '==', |
||
55 | '<>' => '!=', |
||
56 | '<=>' => '==', |
||
57 | 'rlike' => '=~', |
||
58 | 'not rlike' => '!~', |
||
59 | 'regexp' => '=~', |
||
60 | 'not regexp' => '!~', |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * Get the format for database stored dates. |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | public function getDateFormat() |
||
72 | |||
73 | /** |
||
74 | * Get the grammar specific operators. |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public function getOperators() |
||
82 | |||
83 | /** |
||
84 | * @param Builder $builder |
||
85 | * @param $table |
||
86 | * @param string $postfix |
||
87 | * @return mixed |
||
88 | */ |
||
89 | protected function generateTableAlias($builder, $table, $postfix = 'Doc') |
||
95 | |||
96 | protected function prefixTable($table) |
||
100 | |||
101 | /** |
||
102 | * Compile an insert statement into AQL. |
||
103 | * |
||
104 | * @param Builder $builder |
||
105 | * @param array $values |
||
106 | * @return Builder |
||
107 | * @throws BindException |
||
108 | */ |
||
109 | public function compileInsert(Builder $builder, array $values) |
||
131 | |||
132 | /** |
||
133 | * Compile an insert and get ID statement into SQL. |
||
134 | * |
||
135 | * @param Builder $builder |
||
136 | * @param array $values |
||
137 | * @return Builder |
||
138 | * @throws BindException |
||
139 | */ |
||
140 | public function compileInsertGetId(Builder $builder, $values) |
||
144 | |||
145 | /** |
||
146 | * Compile a select query into AQL. |
||
147 | * |
||
148 | * @param Builder $builder |
||
149 | * @return Builder |
||
150 | */ |
||
151 | public function compileSelect(Builder $builder) |
||
171 | |||
172 | /** |
||
173 | * Compile the components necessary for a select clause. |
||
174 | * |
||
175 | * @param Builder $builder |
||
176 | * @return Builder |
||
177 | */ |
||
178 | protected function compileComponents(Builder $builder) |
||
194 | |||
195 | /** |
||
196 | * Compile the "from" portion of the query -> FOR in AQL. |
||
197 | * |
||
198 | * @param Builder $builder |
||
199 | * @param string $table |
||
200 | * @return Builder |
||
201 | */ |
||
202 | protected function compileFrom(Builder $builder, $table) |
||
212 | |||
213 | /** |
||
214 | * Compile the "where" portions of the query. |
||
215 | * |
||
216 | * @param Builder $builder |
||
217 | * @return Builder |
||
218 | */ |
||
219 | protected function compileWheres(Builder $builder) |
||
236 | |||
237 | /** |
||
238 | * Get an array of all the where clauses for the query. |
||
239 | * |
||
240 | * @param \Illuminate\Database\Query\Builder $builder |
||
241 | * @return array |
||
242 | */ |
||
243 | protected function compileWheresToArray($builder) |
||
265 | |||
266 | /** |
||
267 | * Compile an aggregated select clause. |
||
268 | * |
||
269 | * @param Builder $builder |
||
270 | * @param array $aggregate |
||
271 | * @return Builder |
||
272 | */ |
||
273 | protected function compileAggregate(Builder $builder, $aggregate) |
||
279 | |||
280 | /** |
||
281 | * Compile AQL for count aggregate. |
||
282 | * @param Builder $builder |
||
283 | * @param $aggregate |
||
284 | * @return Builder |
||
285 | */ |
||
286 | protected function compileCount(Builder $builder, $aggregate) |
||
292 | |||
293 | /** |
||
294 | * Compile AQL for max aggregate. |
||
295 | * |
||
296 | * @param Builder $builder |
||
297 | * @param $aggregate |
||
298 | * @return Builder |
||
299 | */ |
||
300 | protected function compileMax(Builder $builder, $aggregate) |
||
308 | |||
309 | /** |
||
310 | * Compile AQL for min aggregate. |
||
311 | * |
||
312 | * @param Builder $builder |
||
313 | * @param $aggregate |
||
314 | * @return Builder |
||
315 | */ |
||
316 | protected function compileMin(Builder $builder, $aggregate) |
||
324 | |||
325 | /** |
||
326 | * Compile AQL for average aggregate. |
||
327 | * |
||
328 | * @param Builder $builder |
||
329 | * @param $aggregate |
||
330 | * @return Builder |
||
331 | */ |
||
332 | protected function compileAvg(Builder $builder, $aggregate) |
||
340 | |||
341 | /** |
||
342 | * Compile AQL for sum aggregate. |
||
343 | * |
||
344 | * @param Builder $builder |
||
345 | * @param $aggregate |
||
346 | * @return Builder |
||
347 | */ |
||
348 | protected function compileSum(Builder $builder, $aggregate) |
||
356 | |||
357 | /** |
||
358 | * Compile the "order by" portions of the query. |
||
359 | * |
||
360 | * @param Builder $builder |
||
361 | * @param array $orders |
||
362 | * @return Builder |
||
363 | */ |
||
364 | protected function compileOrders(Builder $builder, $orders) |
||
374 | |||
375 | /** |
||
376 | * Compile the query orders to an array. |
||
377 | * |
||
378 | * @param Builder $builder |
||
379 | * @param array $orders |
||
380 | * @return array |
||
381 | */ |
||
382 | protected function compileOrdersToArray(Builder $builder, $orders) |
||
393 | |||
394 | /** |
||
395 | * Compile the "offset" portions of the query. |
||
396 | * We are handling this first by saving the offset which will be used by the FluentAQL's limit function. |
||
397 | * |
||
398 | * @param Builder $builder |
||
399 | * @param int $offset |
||
400 | * @return Builder |
||
401 | */ |
||
402 | protected function compileOffset(Builder $builder, $offset) |
||
408 | |||
409 | /** |
||
410 | * Compile the "limit" portions of the query. |
||
411 | * |
||
412 | * @param Builder $builder |
||
413 | * @param int $limit |
||
414 | * @return Builder |
||
415 | */ |
||
416 | protected function compileLimit(Builder $builder, $limit) |
||
427 | |||
428 | /** |
||
429 | * Compile the "RETURN" portion of the query. |
||
430 | * |
||
431 | * @param Builder $builder |
||
432 | * @param array $columns |
||
433 | * @return Builder |
||
434 | */ |
||
435 | protected function compileColumns(Builder $builder, array $columns) : Builder |
||
463 | |||
464 | /** |
||
465 | * Compile an update statement into SQL. |
||
466 | * |
||
467 | * @param Builder $builder |
||
468 | * @param array $values |
||
469 | * @return Builder |
||
470 | */ |
||
471 | public function compileUpdate(Builder $builder, array $values) |
||
485 | |||
486 | /** |
||
487 | * Compile a delete statement into SQL. |
||
488 | * |
||
489 | * @param Builder $builder |
||
490 | * @param null $_key |
||
491 | * @return Builder |
||
492 | */ |
||
493 | public function compileDelete(Builder $builder, $_key = null) |
||
514 | |||
515 | /** |
||
516 | * Compile the random statement into SQL. |
||
517 | * |
||
518 | * @param Builder $builder |
||
519 | * @return FunctionExpression; |
||
520 | */ |
||
521 | public function compileRandom(Builder $builder) |
||
525 | |||
526 | /** |
||
527 | * Translate sql operators to their AQL equivalent where possible. |
||
528 | * |
||
529 | * @param string $operator |
||
530 | * @return mixed|string |
||
531 | */ |
||
532 | private function translateOperator(string $operator) |
||
540 | |||
541 | /** |
||
542 | * @param Builder $builder |
||
543 | * @param string $target |
||
544 | * @param string $value |
||
545 | * @return Builder |
||
546 | */ |
||
547 | protected function prefixAlias(Builder $builder, string $target, string $value) : string |
||
551 | } |
||
552 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: