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:
1 | <?php |
||
20 | class SelectQuery extends ProjectionQuery implements MapperProvider |
||
21 | { |
||
22 | use Mappers; |
||
23 | |||
24 | /** |
||
25 | * @var ReturnVars |
||
26 | */ |
||
27 | private $return_vars; |
||
28 | |||
29 | /** |
||
30 | * @param Table $root |
||
31 | * @param Driver $driver |
||
32 | * @param TypeProvider $types |
||
33 | */ |
||
34 | 1 | public function __construct(Table $root, Driver $driver, TypeProvider $types) |
|
40 | |||
41 | /** |
||
42 | * Add all the Columns of a full Table to be selected and returned |
||
43 | * |
||
44 | * @param Table $table Table to select and return |
||
45 | * |
||
46 | * @return $this |
||
47 | */ |
||
48 | 1 | public function table(Table $table) |
|
54 | |||
55 | /** |
||
56 | * Add one or more Columns to select and return |
||
57 | * |
||
58 | * @param Column|Column[] one or more Columns to select and return |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | 1 | public function columns($cols) |
|
68 | |||
69 | /** |
||
70 | * Add an SQL expression to select and return |
||
71 | * |
||
72 | * @param string $expr return expression |
||
73 | * @param string|null $name return variable name (optional, but usually required) |
||
74 | * @param Type|string|null $type optional Type (or Type class-name) |
||
75 | * |
||
76 | * @return $this |
||
77 | */ |
||
78 | 1 | public function value($expr, $name = null, $type = null) |
|
84 | |||
85 | /** |
||
86 | * @inheritdoc |
||
87 | */ |
||
88 | 1 | public function getMappers() |
|
92 | |||
93 | /** |
||
94 | * @inheritdoc |
||
95 | */ |
||
96 | 1 | View Code Duplication | public function getSQL() |
117 | |||
118 | /** |
||
119 | * @ignore string magic (enables creation of nested SELECT queries) |
||
120 | */ |
||
121 | 1 | public function __toString() |
|
125 | |||
126 | /** |
||
127 | * @return string comma-separated return expressions (for use in the SELECT or RETURNING clause of an SQL query) |
||
128 | */ |
||
129 | 1 | protected function buildReturnVars() |
|
133 | } |
||
134 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.