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 |
||
15 | class Attributes |
||
16 | { |
||
17 | /** |
||
18 | * Optimizely API Client. |
||
19 | * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
||
20 | */ |
||
21 | private $client; |
||
22 | |||
23 | /** |
||
24 | * Constructor. |
||
25 | */ |
||
26 | 6 | public function __construct($client) |
|
30 | |||
31 | /** |
||
32 | * List Attributes under a Project |
||
33 | * @param integer $projectId |
||
34 | * @param integer $page |
||
35 | * @param integer $perPage |
||
36 | * @return Result |
||
37 | * @throws Exception |
||
38 | */ |
||
39 | 3 | View Code Duplication | public function listAll($projectId, $page=1, $perPage=25) |
65 | |||
66 | /** |
||
67 | * Get Attribute by ID |
||
68 | * @param type $attributeId |
||
69 | * @return Result |
||
70 | * @throws Exception |
||
71 | */ |
||
72 | 1 | public function get($attributeId) |
|
86 | |||
87 | /** |
||
88 | * Create an Attribue |
||
89 | * @param Result |
||
90 | */ |
||
91 | 1 | public function create($attribute) |
|
108 | |||
109 | /** |
||
110 | * Update an Attribute by ID |
||
111 | * @param integer $attributeId * |
||
112 | * @param Attribute $attribute |
||
113 | * @return Result |
||
114 | * @throws Exception |
||
115 | */ |
||
116 | 1 | public function update($attributeId, $attribute) |
|
133 | |||
134 | /** |
||
135 | * Delete Attribute by ID |
||
136 | * @param integer $attributeId |
||
137 | * @return Result |
||
138 | * @throws Exception |
||
139 | */ |
||
140 | public function delete($attributeId) |
||
147 | } |
||
148 | |||
154 |
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.