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 |
||
19 | class Group |
||
20 | { |
||
21 | /** |
||
22 | * Getopt. |
||
23 | * |
||
24 | * @var GetOpt |
||
25 | */ |
||
26 | protected $getopt; |
||
27 | |||
28 | /** |
||
29 | * Logger. |
||
30 | * |
||
31 | * @var LoggerInterface |
||
32 | */ |
||
33 | protected $logger; |
||
34 | |||
35 | /** |
||
36 | * Server. |
||
37 | * |
||
38 | * @var Server |
||
39 | */ |
||
40 | protected $server; |
||
41 | |||
42 | /** |
||
43 | * Constructor. |
||
44 | * |
||
45 | * @param Server $server |
||
46 | * @param LoggerInterface $logger |
||
47 | * @param GetOpt $getopt |
||
48 | */ |
||
49 | public function __construct(Server $server, LoggerInterface $logger, GetOpt $getopt) |
||
55 | |||
56 | /** |
||
57 | * Get description. |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | public function getDescription(): string |
||
65 | |||
66 | /** |
||
67 | * Start. |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | View Code Duplication | public function add(): bool |
|
82 | |||
83 | /** |
||
84 | * Start. |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | View Code Duplication | public function update(): bool |
|
109 | |||
110 | /** |
||
111 | * Parse member. |
||
112 | * |
||
113 | * @return arrray |
||
114 | */ |
||
115 | protected function parseMemberUpdate(): array |
||
143 | |||
144 | /** |
||
145 | * Parse params. |
||
146 | * |
||
147 | * @return arrray |
||
148 | */ |
||
149 | protected function parseParams(): array |
||
162 | |||
163 | /** |
||
164 | * Parse member. |
||
165 | * |
||
166 | * @return arrray |
||
167 | */ |
||
168 | protected function parseMember(): array |
||
182 | } |
||
183 |
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.