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) |
||
81 | |||
82 | /** |
||
83 | * Prepare column before use in AQL request |
||
84 | * Add entityName and wrap it if needed. |
||
85 | * Add alias for string like (column as other_name) |
||
86 | * |
||
87 | * @param $collection |
||
88 | * @param $column |
||
89 | * @param bool $withCollection |
||
90 | * @return string |
||
91 | */ |
||
92 | public function wrapColumn($column, $collection = null, $withCollection = true){ |
||
116 | |||
117 | /** |
||
118 | * Return collection name |
||
119 | * @return string |
||
120 | */ |
||
121 | public function wrapTable($table) |
||
125 | |||
126 | /** |
||
127 | * @inheritdoc |
||
128 | */ |
||
129 | function columnize(array $columns) |
||
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | public function compileUpdate(Builder $query, $values) |
||
181 | |||
182 | /** |
||
183 | * @inheritdoc |
||
184 | */ |
||
185 | protected function compileJoins(Builder $query, $joins) |
||
194 | |||
195 | /** |
||
196 | * @inheritdoc |
||
197 | */ |
||
198 | public function compileDelete(Builder $query) |
||
208 | |||
209 | /** |
||
210 | * @inheritdoc |
||
211 | */ |
||
212 | public function compileSelect(Builder $query) |
||
244 | |||
245 | /** |
||
246 | * @inheritdoc |
||
247 | */ |
||
248 | protected function compileComponents(Builder $query) |
||
269 | |||
270 | /** |
||
271 | * @inheritdoc |
||
272 | */ |
||
273 | protected function compileFrom(Builder $query, $collection) |
||
277 | |||
278 | /** |
||
279 | * @inheritdoc |
||
280 | */ |
||
281 | protected function compileColumns(Builder $query, $columns) |
||
290 | |||
291 | /** |
||
292 | * Return string for aggregate some column from AQL request |
||
293 | * |
||
294 | * @param Builder $query |
||
295 | * @param $aggregate |
||
296 | * @param $aql |
||
297 | * @return string |
||
298 | */ |
||
299 | protected function compileAggregateExtended(Builder $query, $aggregate, $aql) |
||
303 | |||
304 | /** |
||
305 | * @inheritdoc |
||
306 | */ |
||
307 | protected function compileWheres(Builder $query) |
||
325 | |||
326 | /** |
||
327 | * @inheritdoc |
||
328 | */ |
||
329 | protected function concatenateWhereClauses($query, $sql) |
||
333 | |||
334 | /** |
||
335 | * @inheritdoc |
||
336 | */ |
||
337 | protected function compileWheresToArray($query) |
||
343 | |||
344 | /** |
||
345 | * @inheritdoc |
||
346 | */ |
||
347 | protected function whereBasic(Builder $query, $where) |
||
351 | |||
352 | /** |
||
353 | * @inheritdoc |
||
354 | */ |
||
355 | View Code Duplication | protected function whereIn(Builder $query, $where) |
|
364 | |||
365 | /** |
||
366 | * @inheritdoc |
||
367 | */ |
||
368 | View Code Duplication | protected function whereNotIn(Builder $query, $where) |
|
377 | |||
378 | /** |
||
379 | * @inheritdoc |
||
380 | */ |
||
381 | protected function whereNull(Builder $query, $where) |
||
385 | |||
386 | /** |
||
387 | * @inheritdoc |
||
388 | */ |
||
389 | protected function whereNotNull(Builder $query, $where) |
||
393 | |||
394 | /** |
||
395 | * @inheritdoc |
||
396 | */ |
||
397 | protected function whereColumn(Builder $query, $where) |
||
403 | |||
404 | /** |
||
405 | * @inheritdoc |
||
406 | */ |
||
407 | protected function compileOrders(Builder $query, $orders) |
||
415 | |||
416 | /** |
||
417 | * @inheritdoc |
||
418 | */ |
||
419 | protected function compileOrdersToArray(Builder $query, $orders) |
||
427 | |||
428 | /** |
||
429 | * @inheritdoc |
||
430 | */ |
||
431 | protected function compileLimit(Builder $query, $limit) |
||
442 | |||
443 | /** |
||
444 | * @inheritdoc |
||
445 | */ |
||
446 | protected function compileOffset(Builder $query, $offset) |
||
453 | |||
454 | /** |
||
455 | * Wrap collection name |
||
456 | * @param string $collection |
||
457 | * @return string |
||
458 | */ |
||
459 | protected function wrapCollection($collection){ |
||
462 | |||
463 | protected function getClearColumnName($column){ |
||
472 | |||
473 | protected function getAliasNameFromColumn($column){ |
||
481 | } |
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: