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 |
||
7 | final class Report implements Common |
||
8 | { |
||
9 | private $client; |
||
10 | |||
11 | /** |
||
12 | * Security constructor. |
||
13 | * |
||
14 | * @param Client $client |
||
15 | * @throws Exception |
||
16 | */ |
||
17 | public function __construct(Client $client) |
||
25 | |||
26 | /** |
||
27 | * @param null $groupBy |
||
28 | * @param null $periodFrom |
||
29 | * @param null $periodTo |
||
30 | * @return mixed |
||
31 | * @throws Exception |
||
32 | */ |
||
33 | View Code Duplication | public function getBooksViewsStatistics($groupBy = null, $periodFrom = null, $periodTo = null) |
|
44 | |||
45 | /** |
||
46 | * @param $method |
||
47 | * @param array $params |
||
48 | * @return array |
||
49 | * @throws Exception |
||
50 | */ |
||
51 | public function getUrl($method, array $params = []) |
||
100 | |||
101 | /** |
||
102 | * @param null $groupBy |
||
103 | * @param null $periodFrom |
||
104 | * @param null $periodTo |
||
105 | * @return mixed |
||
106 | * @throws Exception |
||
107 | */ |
||
108 | View Code Duplication | public function getJournalsViewsStatistics($groupBy = null, $periodFrom = null, $periodTo = null) |
|
119 | |||
120 | /** |
||
121 | * @param null $groupBy |
||
122 | * @param null $periodFrom |
||
123 | * @param null $periodTo |
||
124 | * @return mixed |
||
125 | * @throws Exception |
||
126 | */ |
||
127 | View Code Duplication | public function getUsersVisitsSatistics($groupBy = null, $periodFrom = null, $periodTo = null) |
|
138 | |||
139 | /** |
||
140 | * @return mixed |
||
141 | * @throws Exception |
||
142 | */ |
||
143 | public function getAvailablePackets() |
||
147 | |||
148 | /** |
||
149 | * @param $pdKey |
||
150 | * @return mixed |
||
151 | * @throws Exception |
||
152 | */ |
||
153 | public function getAvailableBooks($pdKey) |
||
157 | |||
158 | /** |
||
159 | * @return mixed |
||
160 | * @throws Exception |
||
161 | */ |
||
162 | public function getAvailableJournals() |
||
166 | |||
167 | /** |
||
168 | * @return mixed |
||
169 | * @throws Exception |
||
170 | */ |
||
171 | public function getFormReportEBooks() |
||
175 | } |
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.