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 |
||
18 | class Query implements QueryInterface |
||
19 | { |
||
20 | /* @var \PDO The PDO. */ |
||
21 | private $pdo; |
||
22 | |||
23 | /* @var array The bindings. */ |
||
24 | private $bindings; |
||
25 | |||
26 | /* @var QueryBuilder The query builder. */ |
||
27 | private $queryBuilder; |
||
28 | |||
29 | /** |
||
30 | * Construct a query object with the given pdo and table. |
||
31 | * |
||
32 | * @param \PDO $pdo |
||
33 | * @param string $table |
||
34 | */ |
||
35 | 15 | public function __construct(\PDO $pdo, $table) |
|
41 | |||
42 | /** |
||
43 | * Returns a string representation of the query object. |
||
44 | * |
||
45 | * @return string a string representation of the query object. |
||
46 | */ |
||
47 | 15 | public function __toString() |
|
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 11 | public function select($columns = ['*']) |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 1 | public function insert(array $values) |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 1 | public function update(array $values) |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 1 | public function delete() |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 5 | public function where($column, $operator, $value) |
|
108 | |||
109 | /** |
||
110 | * Set an additional where in condition. |
||
111 | * |
||
112 | * @param string $column |
||
113 | * @param mixed $values |
||
114 | * @return $this |
||
115 | */ |
||
116 | 1 | View Code Duplication | private function whereIn($column, $values) |
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 1 | public function groupBy($column) |
|
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | 1 | public function orderBy($column, $order = null) |
|
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | 2 | public function limit($limit) |
|
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | 1 | public function offset($offset) |
|
172 | |||
173 | /** |
||
174 | * Returns the result of the executed prepared query. |
||
175 | * |
||
176 | * @return QueryResult the result of the executed prepared query. |
||
177 | */ |
||
178 | 4 | public function execute() |
|
200 | |||
201 | /** |
||
202 | * Returns the values array with bindings instead of values. |
||
203 | * |
||
204 | * @param array $values |
||
205 | * @return array the values array with bindings instead of values. |
||
206 | */ |
||
207 | 2 | View Code Duplication | private function replaceValuesWithBindings(array $values) |
220 | } |
||
221 |
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.