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 |
||
3 | class Xhgui_Searcher_Pdo implements Xhgui_Searcher_Interface |
||
4 | { |
||
5 | /** |
||
6 | * @var PDO |
||
7 | */ |
||
8 | private $pdo; |
||
9 | |||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private $table; |
||
14 | |||
15 | /** |
||
16 | * @param PDO $pdo An open database connection |
||
17 | * @param string $table Table name where Xhgui profiles are stored |
||
18 | */ |
||
19 | public function __construct(PDO $pdo, $table) |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | View Code Duplication | public function latest() |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function query($conditions, $limit, $fields = []) |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | View Code Duplication | public function get($id) |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function getForUrl($url, $options, $conditions = array()) |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function getPercentileForUrl($percentile, $url, $search = array()) |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | public function getAvgsForUrl($url, $search = array()) |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function getAll($options = array()) |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | public function delete($id) |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function truncate() |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function saveWatch(array $data) |
||
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | public function getAllWatches() |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | public function truncateWatches() |
||
253 | } |
||
254 |
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.