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)  | 
            ||
| 229 | |||
| 230 | /**  | 
            ||
| 231 |      * {@inheritdoc} | 
            ||
| 232 | */  | 
            ||
| 233 | protected function compileJoins(Builder $query, $joins)  | 
            ||
| 241 | |||
| 242 | /**  | 
            ||
| 243 |      * {@inheritdoc} | 
            ||
| 244 | */  | 
            ||
| 245 | protected function compileComponents(Builder $query)  | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 |      * {@inheritdoc} | 
            ||
| 269 | */  | 
            ||
| 270 | protected function compileFrom(Builder $query, $collection)  | 
            ||
| 274 | |||
| 275 | /**  | 
            ||
| 276 |      * {@inheritdoc} | 
            ||
| 277 | */  | 
            ||
| 278 | protected function compileColumns(Builder $query, $columns)  | 
            ||
| 286 | |||
| 287 | /**  | 
            ||
| 288 | * Return string for aggregate some column from AQL request  | 
            ||
| 289 | *  | 
            ||
| 290 | * @param Builder $query  | 
            ||
| 291 | * @param $aggregate  | 
            ||
| 292 | * @param $aql  | 
            ||
| 293 | * @return string  | 
            ||
| 294 | */  | 
            ||
| 295 | protected function compileAggregateExtended(Builder $query, $aggregate, $aql)  | 
            ||
| 299 | |||
| 300 | /**  | 
            ||
| 301 |      * {@inheritdoc} | 
            ||
| 302 | */  | 
            ||
| 303 | protected function compileWheres(Builder $query)  | 
            ||
| 321 | |||
| 322 | /**  | 
            ||
| 323 |      * {@inheritdoc} | 
            ||
| 324 | */  | 
            ||
| 325 | protected function concatenateWhereClauses($query, $sql)  | 
            ||
| 329 | |||
| 330 | /**  | 
            ||
| 331 |      * {@inheritdoc} | 
            ||
| 332 | */  | 
            ||
| 333 | protected function compileWheresToArray($query)  | 
            ||
| 339 | |||
| 340 | /**  | 
            ||
| 341 |      * {@inheritdoc} | 
            ||
| 342 | */  | 
            ||
| 343 | protected function whereBasic(Builder $query, $where)  | 
            ||
| 347 | |||
| 348 | /**  | 
            ||
| 349 |      * {@inheritdoc} | 
            ||
| 350 | */  | 
            ||
| 351 | View Code Duplication | protected function whereIn(Builder $query, $where)  | 
            |
| 360 | |||
| 361 | /**  | 
            ||
| 362 |      * {@inheritdoc} | 
            ||
| 363 | */  | 
            ||
| 364 | View Code Duplication | protected function whereNotIn(Builder $query, $where)  | 
            |
| 373 | |||
| 374 | /**  | 
            ||
| 375 |      * {@inheritdoc} | 
            ||
| 376 | */  | 
            ||
| 377 | protected function whereNull(Builder $query, $where)  | 
            ||
| 381 | |||
| 382 | /**  | 
            ||
| 383 |      * {@inheritdoc} | 
            ||
| 384 | */  | 
            ||
| 385 | protected function whereNotNull(Builder $query, $where)  | 
            ||
| 389 | |||
| 390 | /**  | 
            ||
| 391 |      * {@inheritdoc} | 
            ||
| 392 | */  | 
            ||
| 393 | protected function whereColumn(Builder $query, $where)  | 
            ||
| 399 | |||
| 400 | /**  | 
            ||
| 401 |      * {@inheritdoc} | 
            ||
| 402 | */  | 
            ||
| 403 | protected function compileOrders(Builder $query, $orders)  | 
            ||
| 411 | |||
| 412 | /**  | 
            ||
| 413 |      * {@inheritdoc} | 
            ||
| 414 | */  | 
            ||
| 415 | protected function compileOrdersToArray(Builder $query, $orders)  | 
            ||
| 423 | |||
| 424 | /**  | 
            ||
| 425 |      * {@inheritdoc} | 
            ||
| 426 | */  | 
            ||
| 427 | protected function compileLimit(Builder $query, $limit)  | 
            ||
| 438 | |||
| 439 | /**  | 
            ||
| 440 |      * {@inheritdoc} | 
            ||
| 441 | */  | 
            ||
| 442 | protected function compileOffset(Builder $query, $offset)  | 
            ||
| 449 | |||
| 450 | /**  | 
            ||
| 451 | * Wrap collection name  | 
            ||
| 452 | * @param string $collection  | 
            ||
| 453 | * @return string  | 
            ||
| 454 | */  | 
            ||
| 455 | protected function wrapCollection($collection)  | 
            ||
| 459 | |||
| 460 | protected function getClearColumnName($column)  | 
            ||
| 470 | |||
| 471 | protected function getAliasNameFromColumn($column)  | 
            ||
| 480 | }  | 
            
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: