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 |
||
4 | View Code Duplication | class Project extends Base |
|
|
|||
5 | { |
||
6 | /** |
||
7 | * Private constructor so only the client can create this |
||
8 | * @param string $apikey |
||
9 | */ |
||
10 | public function __construct($apikey) |
||
14 | |||
15 | /** |
||
16 | * Get one or multiple projects |
||
17 | * @param string project id, leave null for list of boxes |
||
18 | * @param object Containing query arguments |
||
19 | * @return object Result of the request |
||
20 | */ |
||
21 | public function Get($projectId = null, $args = array("limit" => 50)) |
||
25 | |||
26 | /** |
||
27 | * Create new project |
||
28 | * @param object Containing all the information of a project |
||
29 | * @return object Result of the request |
||
30 | */ |
||
31 | public function Create($project) |
||
35 | |||
36 | /** |
||
37 | * Create new project |
||
38 | * @param id of the project |
||
39 | * @param object Containing all the information of a project |
||
40 | * @return object Result of the request |
||
41 | */ |
||
42 | public function Update($projectId, $project) |
||
46 | |||
47 | /** |
||
48 | * Delete a project object by project id |
||
49 | * @param string Id of the project |
||
50 | * @return object Result of the request |
||
51 | */ |
||
52 | public function Delete($projectId) |
||
56 | |||
57 | /** |
||
58 | * Retrieve fields of a project object by project id |
||
59 | * @param string Id of the project |
||
60 | * @return object Result of the request |
||
61 | */ |
||
62 | public function Fields($projectId) |
||
66 | |||
67 | /** |
||
68 | * Retrieve logs of a project object by project id |
||
69 | * @param string Id of the project |
||
70 | * @return object Result of the request |
||
71 | */ |
||
72 | public function Logs($projectId) |
||
76 | } |
||
77 |
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.