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 | abstract class CertainRessourceAbstract implements CertainRessourceInterface, CertainResponseInterface |
||
16 | { |
||
17 | const NOT_FOUND = 404; |
||
18 | /** |
||
19 | * CertainApiService |
||
20 | * @var CertainApiService |
||
21 | */ |
||
22 | protected $certainApiService; |
||
23 | /** |
||
24 | * array of results with information about the request |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $results; |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $ressourceCalled; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * @param CertainApiService $certainApiService |
||
38 | */ |
||
39 | public function __construct(CertainApiService $certainApiService) |
||
43 | |||
44 | |||
45 | /** |
||
46 | * Get information about ressource |
||
47 | * @param string $ressourceId |
||
48 | * @param array $params |
||
49 | * @param boolean $assoc |
||
50 | * @param string $contentType |
||
51 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
52 | * @throws Exceptions\RessourceException |
||
53 | */ |
||
54 | View Code Duplication | public function get($ressourceId= null,$params= array(), $assoc = false, $contentType='json'){ |
|
62 | |||
63 | /** |
||
64 | * Add/Update information to certain |
||
65 | * @param array $bodyData |
||
66 | * @param array $query |
||
67 | * @param string $ressourceId |
||
68 | * @param boolean $assoc |
||
69 | * @param string $contentType |
||
70 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
71 | * @throws Exceptions\RessourceException |
||
72 | * @throws Exceptions\RessourceMandatoryFieldException |
||
73 | */ |
||
74 | public function post($bodyData, $query=array(), $ressourceId= null, $assoc = false, $contentType='json'){ |
||
89 | |||
90 | /** |
||
91 | * Update information to certain |
||
92 | * @param array $bodyData |
||
93 | * @param array $query |
||
94 | * @param string $ressourceId |
||
95 | * @param boolean $assoc |
||
96 | * @param string $contentType |
||
97 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
98 | * @throws Exceptions\RessourceException |
||
99 | * @throws Exceptions\RessourceMandatoryFieldException |
||
100 | */ |
||
101 | public function put($bodyData, $query=array(), $ressourceId= null, $assoc = false, $contentType='json'){ |
||
118 | |||
119 | /** |
||
120 | * Delete information from certain |
||
121 | * @param string $ressourceId |
||
122 | * @param boolean $assoc |
||
123 | * @param string $contentType |
||
124 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
125 | * @throws Exceptions\RessourceException |
||
126 | */ |
||
127 | View Code Duplication | public function delete($ressourceId, $assoc = false, $contentType='json'){ |
|
135 | |||
136 | /** |
||
137 | * Check is a successful request |
||
138 | * @return boolean |
||
139 | */ |
||
140 | public function isSuccessFul() |
||
144 | |||
145 | /** |
||
146 | * Check is not found. |
||
147 | * @return boolean |
||
148 | */ |
||
149 | public function isNotFound(){ |
||
157 | |||
158 | /** |
||
159 | * Get the results |
||
160 | * @return \stdClass|\stdClass[]|array |
||
161 | */ |
||
162 | public function getResults() |
||
166 | |||
167 | /** |
||
168 | * Get the succes value, results, success or fail reason |
||
169 | * @return array |
||
170 | */ |
||
171 | public function getCompleteResults(){ |
||
174 | |||
175 | public function getRessourceCalled() |
||
179 | |||
180 | public function createRessourceCalled($ressourceCalledParameters = null) |
||
195 | |||
196 | |||
197 | |||
198 | } |
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.