Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
16 | class Grammar extends IlluminateGrammar |
||
17 | { |
||
18 | /** |
||
19 | * The components that make up a select clause. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $selectComponents = [ |
||
24 | 'aggregate', |
||
25 | 'from', |
||
26 | 'joins', |
||
27 | 'wheres', |
||
28 | 'groups', |
||
29 | 'havings', |
||
30 | 'orders', |
||
31 | 'limit', |
||
32 | 'offset', |
||
33 | 'columns', |
||
34 | 'unions', |
||
35 | 'lock', |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | */ |
||
41 | public function compileInsertGetId(Builder $query, $values, $sequence) |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function compileInsert(Builder $query, array $values) |
||
50 | { |
||
51 | // Essentially we will force every insert to be treated as a batch insert which |
||
52 | // simply makes creating the SQL easier for us since we can utilize the same |
||
53 | // basic routine regardless of an amount of records given to us to insert. |
||
54 | $collection = $this->wrapTable($query->from); |
||
55 | |||
56 | $entityName = getEntityName($query->from); |
||
57 | |||
58 | if (!is_array(reset($values))) { |
||
59 | $values = [$values]; |
||
60 | } |
||
61 | |||
62 | $columns = array_keys(reset($values)); |
||
63 | |||
64 | $parameters = []; |
||
65 | |||
66 | foreach ($values as $record) { |
||
67 | $bindValuesTmp = []; |
||
68 | foreach ($columns as $column) { |
||
69 | if (!isset($record[$column])){ |
||
70 | continue; |
||
71 | } |
||
72 | $bindValuesTmp[$column] = $record[$column]; |
||
73 | } |
||
74 | $parameters[] = $bindValuesTmp; |
||
75 | } |
||
76 | $parameters = json_encode($parameters); |
||
77 | $parameters = preg_replace('/"(\@B\w+)"/', '$1', $parameters); |
||
78 | |||
79 | $aql = "FOR {$entityName} IN {$parameters} INSERT {$entityName} INTO {$collection}"; |
||
80 | return $aql; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Prepare column before use in AQL request |
||
85 | * Add entityName and wrap it if needed. |
||
86 | * Add alias for string like (column as other_name) |
||
87 | * |
||
88 | * @param $collection |
||
89 | * @param $column |
||
90 | * @param bool $withCollection |
||
91 | * @return string |
||
92 | */ |
||
93 | public function wrapColumn($column, $collection = null, $withCollection = true) |
||
94 | { |
||
95 | $entityName = getEntityNameFromColumn($column); |
||
96 | |||
97 | $clearColumn = $this->getClearColumnName($column); |
||
98 | |||
99 | $alias = $this->getAliasNameFromColumn($column); |
||
100 | |||
101 | if (is_null($entityName) && !is_null($collection)) { |
||
102 | $entityName = getEntityName($collection); |
||
103 | } |
||
104 | |||
105 | if ($clearColumn !== '_key') { |
||
106 | $clearColumn = trim($clearColumn, '`'); |
||
107 | $clearColumn = '`'.$clearColumn.'`'; |
||
108 | } |
||
109 | if ($withCollection) { |
||
110 | $column = $entityName.'.'.$clearColumn; |
||
111 | } |
||
112 | if ($alias) { |
||
|
|||
113 | $column = $alias.':'.$column; |
||
114 | } |
||
115 | return $column; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Return collection name |
||
120 | * @return string |
||
121 | */ |
||
122 | public function wrapTable($table) |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function columnize(array $columns) |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function compileUpdate(Builder $query, $values) |
||
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | public function compileDelete(Builder $query) |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function compileSelect(Builder $query) |
||
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | protected function compileJoins(Builder $query, $joins) |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | protected function compileComponents(Builder $query) |
||
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | protected function compileFrom(Builder $query, $collection) |
||
278 | |||
279 | /** |
||
280 | * {@inheritdoc} |
||
281 | */ |
||
282 | protected function compileColumns(Builder $query, $columns) |
||
291 | |||
292 | /** |
||
293 | * Return string for aggregate some column from AQL request |
||
294 | * |
||
295 | * @param Builder $query |
||
296 | * @param $aggregate |
||
297 | * @param $aql |
||
298 | * @return string |
||
299 | */ |
||
300 | protected function compileAggregateExtended(Builder $query, $aggregate, $aql) |
||
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | protected function compileWheres(Builder $query) |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | protected function concatenateWhereClauses($query, $sql) |
||
334 | |||
335 | /** |
||
336 | * {@inheritdoc} |
||
337 | */ |
||
338 | protected function compileWheresToArray($query) |
||
344 | |||
345 | /** |
||
346 | * {@inheritdoc} |
||
347 | */ |
||
348 | protected function whereBasic(Builder $query, $where) |
||
352 | |||
353 | /** |
||
354 | * {@inheritdoc} |
||
355 | */ |
||
356 | View Code Duplication | protected function whereIn(Builder $query, $where) |
|
365 | |||
366 | /** |
||
367 | * {@inheritdoc} |
||
368 | */ |
||
369 | View Code Duplication | protected function whereNotIn(Builder $query, $where) |
|
378 | |||
379 | /** |
||
380 | * {@inheritdoc} |
||
381 | */ |
||
382 | protected function whereNull(Builder $query, $where) |
||
386 | |||
387 | /** |
||
388 | * {@inheritdoc} |
||
389 | */ |
||
390 | protected function whereNotNull(Builder $query, $where) |
||
394 | |||
395 | /** |
||
396 | * {@inheritdoc} |
||
397 | */ |
||
398 | protected function whereColumn(Builder $query, $where) |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | protected function compileOrders(Builder $query, $orders) |
||
416 | |||
417 | /** |
||
418 | * {@inheritdoc} |
||
419 | */ |
||
420 | protected function compileOrdersToArray(Builder $query, $orders) |
||
428 | |||
429 | /** |
||
430 | * {@inheritdoc} |
||
431 | */ |
||
432 | protected function compileLimit(Builder $query, $limit) |
||
443 | |||
444 | /** |
||
445 | * {@inheritdoc} |
||
446 | */ |
||
447 | protected function compileOffset(Builder $query, $offset) |
||
454 | |||
455 | /** |
||
456 | * Wrap collection name |
||
457 | * @param string $collection |
||
458 | * @return string |
||
459 | */ |
||
460 | protected function wrapCollection($collection) |
||
464 | |||
465 | protected function getClearColumnName($column) |
||
475 | |||
476 | protected function getAliasNameFromColumn($column) |
||
485 | } |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: