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 Pages |
||
16 | { |
||
17 | /** |
||
18 | * Optimizely API Client. |
||
19 | * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
||
20 | */ |
||
21 | private $client; |
||
22 | |||
23 | /** |
||
24 | * Constructor. |
||
25 | */ |
||
26 | 5 | public function __construct($client) |
|
30 | |||
31 | /** |
||
32 | * List all Pages for a project. |
||
33 | * @param integer $projectId |
||
34 | * @param integer $page |
||
35 | * @param integer $perPage |
||
36 | * @return Result |
||
37 | * @throws Exception |
||
38 | */ |
||
39 | 1 | View Code Duplication | public function listAll($projectId, $page=1, $perPage=25) |
65 | |||
66 | /** |
||
67 | * Get metadata for a single Page |
||
68 | * @param integer $pageId |
||
69 | * @return Result |
||
70 | * @throws Exception |
||
71 | */ |
||
72 | 1 | public function get($pageId) |
|
85 | |||
86 | /** |
||
87 | * Create a new Page in a provided Project |
||
88 | * @param Page $page |
||
89 | */ |
||
90 | 1 | View Code Duplication | public function create($page) |
106 | |||
107 | /** |
||
108 | * Update a Page in a provided Project |
||
109 | * @param integer $pageId |
||
110 | * @param Audience $page |
||
111 | * @return Result |
||
112 | * @throws Exception |
||
113 | */ |
||
114 | 1 | View Code Duplication | public function update($pageId, $page) |
130 | |||
131 | /** |
||
132 | * Delete a Page within a Project by ID. |
||
133 | * @param integer $pageId |
||
134 | * @return Result |
||
135 | * @throws Exception |
||
136 | */ |
||
137 | 1 | public function delete($pageId) |
|
144 | } |
||
145 | |||
151 |
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.