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 |
||
17 | class DelayHandler implements SQLInterface |
||
18 | { |
||
19 | use SQLTrait; |
||
20 | use UrlParser; |
||
21 | |||
22 | /** |
||
23 | * Database connection |
||
24 | * @var PDO |
||
25 | */ |
||
26 | private $pdo; |
||
27 | |||
28 | /** |
||
29 | * DelayHandler constructor. |
||
30 | * |
||
31 | * @param PDO $pdo |
||
32 | */ |
||
33 | public function __construct(PDO $pdo) |
||
37 | |||
38 | /** |
||
39 | * Client |
||
40 | * |
||
41 | * @param DelayInterface $client |
||
42 | * @return DelayHandlerClient |
||
43 | */ |
||
44 | public function client(DelayInterface $client) |
||
48 | |||
49 | /** |
||
50 | * Invalidate delay |
||
51 | * |
||
52 | * @param string $baseUri |
||
53 | * @param string $userAgent |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function invalidate($baseUri, $userAgent) |
||
68 | |||
69 | /** |
||
70 | * List all with queue |
||
71 | * |
||
72 | * @param int $minSec |
||
73 | * @param int $limit |
||
74 | * @return array |
||
75 | */ |
||
76 | public function listQueue($minSec = 0, $limit = 100) |
||
98 | |||
99 | /** |
||
100 | * Clean the delay table |
||
101 | * |
||
102 | * @param int $delay - in seconds |
||
103 | * @return bool |
||
104 | */ |
||
105 | View Code Duplication | public function clean($delay = 60) |
|
115 | |||
116 | /** |
||
117 | * Create SQL table |
||
118 | * |
||
119 | * @return bool |
||
120 | * @throws SQLException |
||
121 | */ |
||
122 | public function setup() |
||
129 | } |
||
130 |
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.