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 |
||
13 | class DelayHandlerClient implements SQLInterface |
||
14 | { |
||
15 | use SQLTrait; |
||
16 | |||
17 | /** |
||
18 | * Database connection |
||
19 | * @var PDO |
||
20 | */ |
||
21 | private $pdo; |
||
22 | |||
23 | /** |
||
24 | * PDO driver |
||
25 | * @var string |
||
26 | */ |
||
27 | private $driver; |
||
28 | |||
29 | /** |
||
30 | * Base UriClient |
||
31 | * @var string |
||
32 | */ |
||
33 | private $base; |
||
34 | |||
35 | /** |
||
36 | * User-agent |
||
37 | * @var string |
||
38 | */ |
||
39 | private $userAgent; |
||
40 | |||
41 | /** |
||
42 | * Delay |
||
43 | * @var float|int |
||
44 | */ |
||
45 | private $delay; |
||
46 | |||
47 | /** |
||
48 | * DelayClient constructor. |
||
49 | * |
||
50 | * @param PDO $pdo |
||
51 | * @param string $baseUri |
||
52 | * @param string $userAgent |
||
53 | * @param float|int $delay |
||
54 | */ |
||
55 | public function __construct(PDO $pdo, $baseUri, $userAgent, $delay) |
||
66 | |||
67 | /** |
||
68 | * Queue |
||
69 | * |
||
70 | * @return float|int |
||
71 | */ |
||
72 | View Code Duplication | public function getQueue() |
|
92 | |||
93 | /** |
||
94 | * Sleep |
||
95 | * |
||
96 | * @return float|int |
||
97 | */ |
||
98 | public function sleep() |
||
112 | |||
113 | /** |
||
114 | * Timestamp with milliseconds |
||
115 | * |
||
116 | * @return float|int |
||
117 | */ |
||
118 | View Code Duplication | public function getTimeSleepUntil() |
|
139 | |||
140 | /** |
||
141 | * Set new delayUntil timestamp |
||
142 | * |
||
143 | * @return bool |
||
144 | */ |
||
145 | private function increment() |
||
160 | } |
||
161 |
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.