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 |
||
14 | class UpdateQuery extends Query |
||
15 | { |
||
16 | use Conditions; |
||
17 | |||
18 | /** |
||
19 | * @var Table |
||
20 | */ |
||
21 | protected $table; |
||
22 | |||
23 | /** |
||
24 | * @var Driver |
||
25 | */ |
||
26 | protected $driver; |
||
27 | |||
28 | /** |
||
29 | * @var mixed[] map where Column name => literal SQL expression to assign |
||
30 | */ |
||
31 | private $assignments = []; |
||
32 | |||
33 | /** |
||
34 | * @param Table $table |
||
35 | * @param TypeProvider $types |
||
36 | */ |
||
37 | 1 | public function __construct(Table $table, Driver $driver, TypeProvider $types) |
|
44 | |||
45 | /** |
||
46 | * @param Column|string $column Column to update (or Column name) |
||
47 | * @param mixed $value value to apply |
||
48 | * |
||
49 | * @return $this |
||
50 | */ |
||
51 | 1 | public function setValue($column, $value) |
|
71 | |||
72 | /** |
||
73 | * @param Column|string $column Column to update (or Column name) |
||
74 | * @param string $expr literal SQL expression |
||
75 | * |
||
76 | * @return $this |
||
77 | */ |
||
78 | 1 | public function setExpr($column, $expr) |
|
96 | |||
97 | /** |
||
98 | * @param array $values map where Column name => scalar values to assign |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | 1 | public function assign(array $values) |
|
116 | |||
117 | /** |
||
118 | * @inheritdoc |
||
119 | */ |
||
120 | 1 | public function getSQL() |
|
132 | |||
133 | /** |
||
134 | * @param Column $column |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | 1 | private function getPlaceholder(Column $column) |
|
150 | } |
||
151 |
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.