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 Events |
||
18 | { |
||
19 | /** |
||
20 | * Optimizely API Client. |
||
21 | * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
||
22 | */ |
||
23 | private $client; |
||
24 | |||
25 | /** |
||
26 | * Constructor. |
||
27 | */ |
||
28 | 6 | public function __construct($client) |
|
32 | |||
33 | /** |
||
34 | * Get all events for a Project |
||
35 | * @param integer $projectId |
||
36 | * @param integer $includeClassic |
||
37 | * @param integer $page |
||
38 | * @param integer $perPage |
||
39 | * @return Result |
||
40 | * @throws Exception |
||
41 | */ |
||
42 | 1 | public function listAll($projectId, $includeClassic, $page=1, $perPage=25) |
|
71 | |||
72 | /** |
||
73 | * Get Event by ID |
||
74 | * @param type $eventId |
||
75 | * @return Result |
||
76 | * @throws Exception |
||
77 | */ |
||
78 | 1 | public function get($eventId) |
|
92 | |||
93 | /** |
||
94 | * Creates a new click event. |
||
95 | * @param integer $pageId |
||
96 | * @param ClickEvent $event |
||
97 | * @return Result |
||
98 | * @throws Exception |
||
99 | */ |
||
100 | 1 | View Code Duplication | public function createClickEvent($pageId, $event) |
117 | |||
118 | /** |
||
119 | * Creates a new custom event. |
||
120 | * @param integer $projectId |
||
121 | * @param CustomEvent $event |
||
122 | * @return Result |
||
123 | * @throws Exception |
||
124 | */ |
||
125 | 1 | View Code Duplication | public function createCustomEvent($projectId, $event) |
142 | |||
143 | /** |
||
144 | * Updates the given click event. |
||
145 | * @param integer $pageId |
||
146 | * @param integer $eventId |
||
147 | * @param ClickEvent $event |
||
148 | * @return Result |
||
149 | * @throws Exception |
||
150 | */ |
||
151 | 1 | View Code Duplication | public function updateClickEvent($pageId, $eventId, $event) |
168 | |||
169 | /** |
||
170 | * Updates the given custom event. |
||
171 | * @param integer $projectId |
||
172 | * @param integer $eventId |
||
173 | * @param CustomEvent $event |
||
174 | * @return Result |
||
175 | * @throws Exception |
||
176 | */ |
||
177 | 1 | View Code Duplication | public function updateCustomEvent($projectId, $eventId, $event) |
194 | } |
||
195 | |||
200 |
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.