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 |
||
16 | class Campaigns |
||
17 | { |
||
18 | /** |
||
19 | * Optimizely API Client. |
||
20 | * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
||
21 | */ |
||
22 | private $client; |
||
23 | |||
24 | /** |
||
25 | * Constructor. |
||
26 | */ |
||
27 | 8 | public function __construct($client) |
|
31 | |||
32 | /** |
||
33 | * Returns the list of campaigns. |
||
34 | * @param integer $projectId |
||
35 | * @param integer $page |
||
36 | * @param integer $perPage |
||
37 | * @return Result |
||
38 | * @throws Exception |
||
39 | */ |
||
40 | 3 | public function listAll($projectId, $page=1, $perPage=25) |
|
74 | |||
75 | /** |
||
76 | * Reads a campaign. |
||
77 | * @param integer $campaignId |
||
78 | * @return Result |
||
79 | * @throws Exception |
||
80 | */ |
||
81 | 1 | View Code Duplication | public function get($campaignId) |
94 | |||
95 | /** |
||
96 | * Get campaign results |
||
97 | * @param integer $campaignId The id for the campaign you want results for |
||
98 | * @param string $starTime The earliest time to count events in results. Defaults to the time that the campaign was first activated. |
||
99 | * @param string $endTime The latest time to count events in results. Defaults to the time the campaign was last active or the current time if the campaign is still running. |
||
100 | * @return Result |
||
101 | * @throws Exception |
||
102 | */ |
||
103 | 1 | public function getResults($campaignId, $startTime = null, $endTime = null) |
|
127 | |||
128 | /** |
||
129 | * Create a new Campaign in your account |
||
130 | * @param Campaign $campaign |
||
131 | * @return Result |
||
132 | * @throws Exception |
||
133 | */ |
||
134 | 1 | public function create($campaign) |
|
151 | |||
152 | /** |
||
153 | * Update a Campaign |
||
154 | * @param integer $campaignId |
||
155 | * @param Campaign $campaign |
||
156 | * @return Result |
||
157 | * @throws Exception |
||
158 | */ |
||
159 | 1 | public function update($campaignId, $campaign) |
|
176 | |||
177 | /** |
||
178 | * Delete Campaign by ID |
||
179 | * @param integer $campaignId |
||
180 | * @return Result |
||
181 | * @throws Exception |
||
182 | */ |
||
183 | 1 | public function delete($campaignId) |
|
190 | } |
||
191 | |||
194 |
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.