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 |
||
14 | class Projects |
||
15 | { |
||
16 | /** |
||
17 | * Optimizely API Client. |
||
18 | * @var WebMarketingROI\OptimizelyPHP\OptimizelyApiClient |
||
19 | */ |
||
20 | private $client; |
||
21 | |||
22 | /** |
||
23 | * Constructor. |
||
24 | */ |
||
25 | 4 | public function __construct($client) |
|
29 | |||
30 | /** |
||
31 | * Get a list of all the Projects in your account, with associated metadata. |
||
32 | * @param integer $page Page number (zero-based). |
||
33 | * @param integer $perPage Count of projects per page (10 by default). |
||
34 | * @return array |
||
35 | * @throws \Exception |
||
36 | */ |
||
37 | 1 | View Code Duplication | public function listAll($page=0, $perPage=10) |
61 | |||
62 | /** |
||
63 | * Get metadata for a single Project. |
||
64 | * @param integer $projectId |
||
65 | * @return Project |
||
66 | * @throws \Exception |
||
67 | */ |
||
68 | 1 | public function get($projectId) |
|
84 | |||
85 | /** |
||
86 | * Create a new Project in your account. |
||
87 | * @param Project $project Project meta information. |
||
88 | * @return Project Created project. |
||
89 | */ |
||
90 | 1 | View Code Duplication | public function create($project) |
105 | |||
106 | /** |
||
107 | * Update a Project. |
||
108 | * @param integer $projectId |
||
109 | * @param Project $project |
||
110 | * @return Project Updated project. |
||
111 | * @throws \Exception |
||
112 | */ |
||
113 | 1 | View Code Duplication | public function update($projectId, $project) |
136 | } |
||
137 | |||
138 |
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.