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) |
||
| 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!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: