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 |
||
15 | class DelayHandler implements SQLInterface |
||
16 | { |
||
17 | use UrlParser; |
||
18 | |||
19 | /** |
||
20 | * Database connection |
||
21 | * @var PDO |
||
22 | */ |
||
23 | private $pdo; |
||
24 | |||
25 | /** |
||
26 | * DelayHandler constructor. |
||
27 | * |
||
28 | * @param PDO $pdo |
||
29 | */ |
||
30 | public function __construct(PDO $pdo) |
||
39 | |||
40 | /** |
||
41 | * Client |
||
42 | * |
||
43 | * @param DelayInterface $client |
||
44 | * @return DelayHandlerClient |
||
45 | */ |
||
46 | public function client(DelayInterface $client) |
||
50 | |||
51 | /** |
||
52 | * Clean the delay table |
||
53 | * |
||
54 | * @param int $delay - in seconds |
||
55 | * @return bool |
||
56 | */ |
||
57 | View Code Duplication | public function clean($delay = 60) |
|
67 | |||
68 | /** |
||
69 | * Top X delays |
||
70 | * |
||
71 | * @param int $limit |
||
72 | * @param int $min |
||
73 | * @return array |
||
74 | */ |
||
75 | View Code Duplication | public function getTopDelays($limit = 100, $min = 0) |
|
97 | |||
98 | /** |
||
99 | * Top X wait time |
||
100 | * |
||
101 | * @param int $limit |
||
102 | * @param int $min |
||
103 | * @return array |
||
104 | */ |
||
105 | View Code Duplication | public function getTopWaitTimes($limit = 100, $min = 0) |
|
127 | } |
||
128 |
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.