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 OracleGrammar 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 OracleGrammar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class OracleGrammar extends Grammar |
||
12 | { |
||
13 | use OracleReservedWords; |
||
14 | |||
15 | /** |
||
16 | * The keyword identifier wrapper format. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $wrapper = '%s'; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $schema_prefix = ''; |
||
26 | |||
27 | /** |
||
28 | * Compile an exists statement into SQL. |
||
29 | * |
||
30 | * @param \Illuminate\Database\Query\Builder $query |
||
31 | * @return string |
||
32 | */ |
||
33 | 3 | public function compileExists(Builder $query) |
|
42 | |||
43 | /** |
||
44 | * Compile a select query into SQL. |
||
45 | * |
||
46 | * @param \Illuminate\Database\Query\Builder |
||
47 | * @return string |
||
48 | */ |
||
49 | 159 | public function compileSelect(Builder $query) |
|
86 | 12 | ||
87 | /** |
||
88 | 12 | * @param Builder $query |
|
89 | * @param array $components |
||
90 | * @return bool |
||
91 | */ |
||
92 | protected function isPaginationable(Builder $query, array $components) |
||
96 | |||
97 | /** |
||
98 | * Create a full ANSI offset clause for the query. |
||
99 | * |
||
100 | * @param \Illuminate\Database\Query\Builder $query |
||
101 | * @param array $components |
||
102 | * @return string |
||
103 | */ |
||
104 | 12 | protected function compileAnsiOffset(Builder $query, $components) |
|
127 | |||
128 | /** |
||
129 | 12 | * Compile the limit / offset row constraint for a query. |
|
130 | * |
||
131 | 12 | * @param \Illuminate\Database\Query\Builder $query |
|
132 | 6 | * @return string |
|
133 | */ |
||
134 | 9 | protected function compileRowConstraint($query) |
|
149 | |||
150 | /** |
||
151 | * Compile a common table expression for a query. |
||
152 | * |
||
153 | * @param string $sql |
||
154 | * @param string $constraint |
||
155 | 201 | * @param Builder $query |
|
156 | * @return string |
||
157 | 201 | */ |
|
158 | protected function compileTableExpression($sql, $constraint, $query) |
||
166 | 201 | ||
167 | 201 | /** |
|
168 | * Compile a truncate table statement into SQL. |
||
169 | * |
||
170 | * @param \Illuminate\Database\Query\Builder $query |
||
171 | 201 | * @return array |
|
172 | */ |
||
173 | public function compileTruncate(Builder $query) |
||
177 | |||
178 | /** |
||
179 | 201 | * Wrap a value in keyword identifiers. |
|
180 | * |
||
181 | 201 | * Override due to laravel's stringify integers. |
|
182 | * |
||
183 | * @param \Illuminate\Database\Query\Expression|string $value |
||
184 | * @param bool $prefixAlias |
||
185 | * @return string |
||
186 | */ |
||
187 | public function wrap($value, $prefixAlias = false) |
||
195 | |||
196 | /** |
||
197 | * Wrap a table in keyword identifiers. |
||
198 | * |
||
199 | * @param \Illuminate\Database\Query\Expression|string $table |
||
200 | 201 | * @return string |
|
201 | */ |
||
202 | 201 | public function wrapTable($table) |
|
220 | |||
221 | 6 | /** |
|
222 | * Return the schema prefix. |
||
223 | * |
||
224 | * @return string |
||
225 | 6 | */ |
|
226 | public function getSchemaPrefix() |
||
230 | |||
231 | /** |
||
232 | * Set the schema prefix. |
||
233 | * |
||
234 | 6 | * @param string $prefix |
|
235 | */ |
||
236 | public function setSchemaPrefix($prefix) |
||
240 | |||
241 | /** |
||
242 | * Wrap a single string in keyword identifiers. |
||
243 | * |
||
244 | 15 | * @param string $value |
|
245 | * @return string |
||
246 | */ |
||
247 | protected function wrapValue($value) |
||
257 | |||
258 | /** |
||
259 | * Compile an insert and get ID statement into SQL. |
||
260 | 15 | * |
|
261 | * @param \Illuminate\Database\Query\Builder $query |
||
262 | 15 | * @param array $values |
|
263 | * @param string $sequence |
||
264 | 15 | * @return string |
|
265 | 3 | */ |
|
266 | 3 | public function compileInsertGetId(Builder $query, $values, $sequence = 'id') |
|
283 | |||
284 | /** |
||
285 | * Compile an insert statement into SQL. |
||
286 | * |
||
287 | * @param \Illuminate\Database\Query\Builder $query |
||
288 | 6 | * @param array $values |
|
289 | * @return string |
||
290 | 6 | */ |
|
291 | public function compileInsert(Builder $query, array $values) |
||
325 | |||
326 | /** |
||
327 | * Compile an insert with blob field statement into SQL. |
||
328 | * |
||
329 | 6 | * @param \Illuminate\Database\Query\Builder $query |
|
330 | * @param array $values |
||
331 | 6 | * @param array $binaries |
|
332 | * @param string $sequence |
||
333 | * @return string |
||
334 | */ |
||
335 | public function compileInsertLob(Builder $query, $values, $binaries, $sequence = 'id') |
||
366 | |||
367 | /** |
||
368 | 6 | * Compile an update statement into SQL. |
|
369 | * |
||
370 | * @param \Illuminate\Database\Query\Builder $query |
||
371 | * @param array $values |
||
372 | * @param array $binaries |
||
373 | * @param string $sequence |
||
374 | 6 | * @return string |
|
375 | */ |
||
376 | 6 | public function compileUpdateLob(Builder $query, array $values, array $binaries, $sequence = 'id') |
|
424 | |||
425 | /** |
||
426 | * Compile the lock into SQL. |
||
427 | * |
||
428 | * @param \Illuminate\Database\Query\Builder $query |
||
429 | * @param bool|string $value |
||
430 | 3 | * @return string |
|
431 | */ |
||
432 | 3 | protected function compileLock(Builder $query, $value) |
|
444 | |||
445 | 9 | /** |
|
446 | * Compile the "limit" portions of the query. |
||
447 | 9 | * |
|
448 | * @param \Illuminate\Database\Query\Builder $query |
||
449 | 9 | * @param int $limit |
|
450 | * @return string |
||
451 | */ |
||
452 | protected function compileLimit(Builder $query, $limit) |
||
456 | |||
457 | /** |
||
458 | * Compile the "offset" portions of the query. |
||
459 | * |
||
460 | * @param \Illuminate\Database\Query\Builder $query |
||
461 | * @param int $offset |
||
462 | * @return string |
||
463 | */ |
||
464 | protected function compileOffset(Builder $query, $offset) |
||
468 | |||
469 | /** |
||
470 | * Compile a "where date" clause. |
||
471 | * |
||
472 | * @param \Illuminate\Database\Query\Builder $query |
||
473 | * @param array $where |
||
474 | * @return string |
||
475 | */ |
||
476 | protected function whereDate(Builder $query, $where) |
||
482 | |||
483 | /** |
||
484 | * Compile a date based where clause. |
||
485 | * |
||
486 | * @param string $type |
||
487 | * @param \Illuminate\Database\Query\Builder $query |
||
488 | * @param array $where |
||
489 | * @return string |
||
490 | */ |
||
491 | protected function dateBasedWhere($type, Builder $query, $where) |
||
497 | |||
498 | /** |
||
499 | * Compile a "where not in raw" clause. |
||
500 | * |
||
501 | * For safety, whereIntegerInRaw ensures this method is only used with integer values. |
||
502 | * |
||
503 | * @param \Illuminate\Database\Query\Builder $query |
||
504 | * @param array $where |
||
505 | * @return string |
||
506 | */ |
||
507 | View Code Duplication | protected function whereNotInRaw(Builder $query, $where) |
|
519 | |||
520 | /** |
||
521 | * Compile a "where in raw" clause. |
||
522 | * |
||
523 | * For safety, whereIntegerInRaw ensures this method is only used with integer values. |
||
524 | * |
||
525 | * @param \Illuminate\Database\Query\Builder $query |
||
526 | * @param array $where |
||
527 | * @return string |
||
528 | */ |
||
529 | View Code Duplication | protected function whereInRaw(Builder $query, $where) |
|
541 | |||
542 | private function resolveClause($column, $values, $type) |
||
558 | } |
||
559 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.