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:
Complex classes like CertainRessourceAbstract often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CertainRessourceAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class CertainRessourceAbstract implements CertainRessourceInterface, CertainResponseInterface |
||
15 | { |
||
16 | const NOT_FOUND = 404; |
||
17 | |||
18 | /** |
||
19 | * CertainApiService |
||
20 | * @var CertainApiService |
||
21 | */ |
||
22 | protected $certainApiService; |
||
23 | |||
24 | /** |
||
25 | * array of results with information about the request |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $results; |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $ressourceCalled; |
||
35 | |||
36 | /** |
||
37 | * @param CertainApiService $certainApiService |
||
38 | */ |
||
39 | public function __construct(CertainApiService $certainApiService) |
||
43 | |||
44 | /** |
||
45 | * Get information about ressource |
||
46 | * @param string $ressourceId |
||
47 | * @param array $params |
||
48 | * @param boolean $assoc |
||
49 | * @param string $contentType |
||
50 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
51 | * @throws Exceptions\RessourceException |
||
52 | */ |
||
53 | View Code Duplication | public function get($ressourceId = null, $params = array(), $assoc = false, |
|
|
|||
54 | $contentType = 'json') |
||
55 | { |
||
56 | $ressourceName = $this->getRessourceName(); |
||
57 | ; |
||
58 | if ($ressourceName == '' || is_null($ressourceName)) { |
||
59 | throw new Exceptions\RessourceException('No ressource name provided.'); |
||
60 | } |
||
61 | $this->results = $this->certainApiService->get($ressourceName, |
||
62 | $this->ressourceCalled, $ressourceId, $params, $assoc, $contentType); |
||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Add/Update information to certain |
||
68 | * @param array $bodyData |
||
69 | * @param array $query |
||
70 | * @param string $ressourceId |
||
71 | * @param boolean $assoc |
||
72 | * @param string $contentType |
||
73 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
74 | * @throws Exceptions\RessourceException |
||
75 | * @throws Exceptions\RessourceMandatoryFieldException |
||
76 | */ |
||
77 | public function post($bodyData, $query = array(), $ressourceId = null, |
||
78 | $assoc = false, $contentType = 'json') |
||
79 | { |
||
80 | $ressourceName = $this->getRessourceName(); |
||
81 | ; |
||
82 | if ($ressourceName == '' || is_null($ressourceName)) { |
||
83 | throw new Exceptions\RessourceException('No ressource name provided.'); |
||
84 | } |
||
85 | if ($ressourceId === null && count($this->getMandatoryFields()) > 0) { |
||
86 | View Code Duplication | foreach ($this->getMandatoryFields() as $field) { |
|
87 | if (!in_array($field, array_keys($bodyData))) { |
||
88 | throw new Exceptions\RessourceMandatoryFieldException(sprintf('The field %s is required', |
||
89 | $field)); |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | $this->results = $this->certainApiService->post($ressourceName, |
||
94 | $this->ressourceCalled, $ressourceId, $bodyData, $query, $assoc, |
||
95 | $contentType); |
||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Update information to certain |
||
101 | * @param array $bodyData |
||
102 | * @param array $query |
||
103 | * @param string $ressourceId |
||
104 | * @param boolean $assoc |
||
105 | * @param string $contentType |
||
106 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
107 | * @throws Exceptions\RessourceException |
||
108 | * @throws Exceptions\RessourceMandatoryFieldException |
||
109 | */ |
||
110 | public function put($bodyData, $query = array(), $ressourceId = null, |
||
111 | $assoc = false, $contentType = 'json') |
||
112 | { |
||
113 | $ressourceName = $this->getRessourceName(); |
||
114 | ; |
||
115 | if ($ressourceName == '' || is_null($ressourceName)) { |
||
116 | throw new Exceptions\RessourceException('No ressource name provided.'); |
||
117 | } |
||
118 | if (!is_null($ressourceId) && count($this->getMandatoryFields()) > 0) { |
||
119 | View Code Duplication | foreach ($this->getMandatoryFields() as $field) { |
|
120 | if (!in_array($field, array_keys($bodyData))) { |
||
121 | throw new Exceptions\RessourceMandatoryFieldException(sprintf('The field %s is required', |
||
122 | $field)); |
||
123 | } |
||
124 | } |
||
125 | } else { |
||
126 | throw new Exceptions\RessourceMandatoryFieldException(sprintf('The id field is required')); |
||
127 | } |
||
128 | $this->results = $this->certainApiService->put($ressourceName, |
||
129 | $this->ressourceCalled, $ressourceId, $bodyData, $query, $assoc, |
||
130 | $contentType); |
||
131 | return $this; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Delete information from certain |
||
136 | * @param string $ressourceId |
||
137 | * @param boolean $assoc |
||
138 | * @param string $contentType |
||
139 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
140 | * @throws Exceptions\RessourceException |
||
141 | */ |
||
142 | View Code Duplication | public function delete($ressourceId, $assoc = false, $contentType = 'json') |
|
143 | { |
||
144 | $ressourceName = $this->getRessourceName(); |
||
145 | if ($ressourceName == '' || is_null($ressourceName)) { |
||
146 | throw new Exceptions\RessourceException('No ressource name provided.'); |
||
147 | } |
||
148 | $this->results = $this->certainApiService->delete($ressourceName, |
||
149 | $this->ressourceCalled, $ressourceId, $assoc, $contentType); |
||
150 | return $this; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Check is a successful request |
||
155 | * @return boolean |
||
156 | */ |
||
157 | public function isSuccessFul() |
||
161 | |||
162 | /** |
||
163 | * Check is not found. |
||
164 | * @return boolean |
||
165 | */ |
||
166 | public function isNotFound() |
||
167 | { |
||
168 | if (isset($this->results['statusCode']) && $this->results['statusCode'] == self::NOT_FOUND) { |
||
169 | return true; |
||
170 | } elseif (isset($this->results['statusCode']) && $this->results['statusCode'] |
||
171 | != self::NOT_FOUND) { |
||
172 | return false; |
||
173 | } |
||
174 | return null; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get the results |
||
179 | * @return \stdClass|\stdClass[]|array |
||
180 | */ |
||
181 | public function getResults() |
||
182 | { |
||
183 | return $this->results['results']; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Get the succes value, results, success or fail reason |
||
188 | * @return array |
||
189 | */ |
||
190 | public function getCompleteResults() |
||
191 | { |
||
192 | return $this->results; |
||
193 | } |
||
194 | |||
195 | public function getRessourceCalled() |
||
199 | |||
200 | public function createRessourceCalled($ressourceCalledParameters = null) |
||
201 | { |
||
202 | $this->ressourceCalled = ''; |
||
203 | if (is_array($ressourceCalledParameters) && !empty($ressourceCalledParameters)) { |
||
204 | foreach ($ressourceCalledParameters as $segmentKey => $segmentValue) { |
||
205 | if ($segmentValue != '') { |
||
206 | $this->ressourceCalled .= '/'.$segmentKey.'/'.$segmentValue; |
||
207 | } else { |
||
208 | $this->ressourceCalled .= '/'.$segmentKey; |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * |
||
218 | * @param string $eventCode |
||
219 | * @param string $ressourceId |
||
220 | * @param array $params |
||
221 | * @param boolean $assoc |
||
222 | * @param string $contentType |
||
223 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
224 | */ |
||
225 | public function getWithEventCode($eventCode, $ressourceId, $params = array(), |
||
226 | $assoc = false, $contentType = 'json') |
||
227 | { |
||
228 | $ressourceId = $eventCode.'/'.$ressourceId; |
||
229 | return $this->get($ressourceId, $params, $assoc, $contentType); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * |
||
234 | * @param string $eventCode |
||
235 | * @param string $ressourceId |
||
236 | * @param array $bodyData |
||
237 | * @param array $query |
||
238 | * @param boolean $assoc |
||
239 | * @param string $contentType |
||
240 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
241 | */ |
||
242 | View Code Duplication | public function postWithEventCode($eventCode, $ressourceId, $bodyData, |
|
243 | $query = array(), $assoc = false, |
||
244 | $contentType = 'json') |
||
245 | { |
||
246 | $ressourceId = $eventCode.'/'.$ressourceId; |
||
247 | return $this->post($bodyData, $query, $ressourceId, $assoc, $contentType); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * |
||
252 | * @param string $eventCode |
||
253 | * @param string $ressourceId |
||
254 | * @param array $bodyData |
||
255 | * @param array $query |
||
256 | * @param boolean $assoc |
||
257 | * @param string $contentType |
||
258 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
259 | */ |
||
260 | View Code Duplication | public function putWithEventCode($eventCode, $ressourceId, $bodyData, |
|
261 | $query = array(), $assoc = false, |
||
262 | $contentType = 'json') |
||
263 | { |
||
264 | $ressourceId = $eventCode.'/'.$ressourceId; |
||
265 | return $this->put($bodyData, $query, $ressourceId, $assoc, $contentType); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * |
||
270 | * @param type $eventCode |
||
271 | * @param string $ressourceId |
||
272 | * @param boolean $assoc |
||
273 | * @param string $contentType |
||
274 | * @return \Wabel\CertainAPI\CertainRessourceAbstract |
||
275 | */ |
||
276 | public function deleteWithEventCode($eventCode, $ressourceId, |
||
277 | $assoc = false, $contentType = 'json') |
||
278 | { |
||
279 | $ressourceId = $eventCode.'/'.$ressourceId; |
||
280 | return $this->delete($ressourceId, $assoc, $contentType); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Return the size. |
||
285 | * @return int |
||
286 | */ |
||
287 | public function getSize() |
||
288 | { |
||
289 | $result = $this->getResults(); |
||
290 | if (!isset($result->size)) { |
||
291 | return null; |
||
292 | } |
||
293 | return $result->size; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Return the completeCollectionSizet. |
||
298 | * @return int |
||
299 | */ |
||
300 | public function getCompleteCollectionSize() |
||
301 | { |
||
302 | $result = $this->getResults(); |
||
303 | if (!isset($result->size)) { |
||
304 | return null; |
||
305 | } |
||
306 | return $result->completeCollectionSize; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Return the maxResults. |
||
311 | * @return int |
||
312 | */ |
||
313 | public function getMaxResults() |
||
321 | |||
322 | /** |
||
323 | * |
||
324 | * @return CertainApiService |
||
325 | */ |
||
326 | public function getCertainApiService() |
||
327 | { |
||
328 | return $this->certainApiService; |
||
329 | } |
||
330 | |||
331 | |||
332 | } |
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.