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 | class Box extends Base |
||
5 | { |
||
6 | /** |
||
7 | * Private constructor so only the client can create this |
||
8 | * @param string $apikey |
||
9 | * @param string $projectid |
||
10 | */ |
||
11 | public function __construct($apikey, $projectid) |
||
15 | |||
16 | /** |
||
17 | * Get one or multiple boxes |
||
18 | * @param string box id, leave null for list of boxes |
||
19 | * @param object Containing query arguments |
||
20 | * @return object Result of the request |
||
21 | */ |
||
22 | public function Get($boxId = null, $args = array("limit" => 50)) |
||
26 | |||
27 | /** |
||
28 | * Get all box codes |
||
29 | * @param string box id |
||
30 | * @param object Containing query arguments |
||
31 | * @return object Result of the request |
||
32 | */ |
||
33 | public function GetCode($boxId, $args = array("limit" => 50)) |
||
37 | |||
38 | /** |
||
39 | * Get one or multiple versions of a box |
||
40 | * @param string box id |
||
41 | * @param string version id, leave null for list of versions |
||
42 | * @param object Containing query arguments |
||
43 | * @return object Result of the request |
||
44 | */ |
||
45 | public function GetVersion($boxId, $versionId = null, $args = array("limit" => 50)) |
||
49 | |||
50 | /** |
||
51 | * Create new box |
||
52 | * @param object Containing all the information of a box |
||
53 | * @return object Result of the request |
||
54 | */ |
||
55 | public function Create($box) |
||
59 | |||
60 | /** |
||
61 | * Delete a box version by version id |
||
62 | * @param string Id of the box |
||
63 | * @param string Id of the version to be deleted |
||
64 | * @return object Result of the request |
||
65 | */ |
||
66 | public function Delete($boxId, $versionId) |
||
70 | |||
71 | /** |
||
72 | * Update a box |
||
73 | * @param object Box containing the boxid and fields that need to be updated |
||
74 | * @throws \Exception When boxid is not present |
||
75 | * @return object Result of the request |
||
76 | */ |
||
77 | View Code Duplication | public function Update($box) |
|
85 | |||
86 | /** |
||
87 | * Update a box |
||
88 | * @param string Id of the box |
||
89 | * @param object Version containing the version and fields that need to be updated |
||
90 | * @throws \Exception When versionid is not present |
||
91 | * @return object Result of the request |
||
92 | */ |
||
93 | View Code Duplication | public function UpdateVersion($boxid, $version) |
|
101 | } |
||
102 |
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.