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 Corundum 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 Corundum, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Corundum |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $tokens = []; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $urlToken; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $basic; |
||
32 | |||
33 | /** |
||
34 | * @var Slice |
||
35 | */ |
||
36 | protected $slice; |
||
37 | |||
38 | /** |
||
39 | * @var Slice |
||
40 | */ |
||
41 | protected $fake; |
||
42 | |||
43 | /** |
||
44 | * @var ResponseInterface |
||
45 | */ |
||
46 | protected $response; |
||
47 | |||
48 | /** |
||
49 | * @var Slice |
||
50 | */ |
||
51 | protected $results; |
||
52 | |||
53 | /** |
||
54 | * Corundum constructor. |
||
55 | * |
||
56 | * @param Slice $slice |
||
57 | */ |
||
58 | public function __construct(Slice $slice) |
||
67 | |||
68 | /** |
||
69 | * @return array |
||
70 | */ |
||
71 | public function getTokens(): array |
||
75 | |||
76 | /** |
||
77 | * @param array $tokens |
||
78 | */ |
||
79 | public function setTokens(array $tokens) |
||
83 | |||
84 | /** |
||
85 | * @return Slice |
||
86 | */ |
||
87 | protected function fake(): Slice |
||
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | * |
||
100 | * @throws \RuntimeException |
||
101 | */ |
||
102 | public function getBody(): string |
||
107 | |||
108 | /** |
||
109 | * @return Slice|null |
||
110 | */ |
||
111 | public function getResults() |
||
115 | |||
116 | /** |
||
117 | * @return int|null |
||
118 | */ |
||
119 | public function getCode() |
||
128 | |||
129 | /** |
||
130 | * @param mixed $value |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | protected function value($value) |
||
143 | |||
144 | /** |
||
145 | * @param array $params |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | protected function multipart(array $params): array |
||
170 | |||
171 | /** |
||
172 | * @param Slice $options |
||
173 | * |
||
174 | * @return Slice|null |
||
175 | * |
||
176 | * @throws Invalid |
||
177 | */ |
||
178 | protected function apiRequest(Slice $options) |
||
235 | |||
236 | /** |
||
237 | * @param string $user |
||
238 | * @param array $params |
||
239 | * |
||
240 | * @return Slice |
||
241 | * |
||
242 | * @throws Invalid |
||
243 | */ |
||
244 | protected function authorize(string $user, array $params = []): Slice |
||
266 | |||
267 | /** |
||
268 | * @param string $user |
||
269 | * @param string $scope |
||
270 | * |
||
271 | * @return Slice |
||
272 | * |
||
273 | * @throws Invalid |
||
274 | */ |
||
275 | protected function getToken(string $user, string $scope = ''): Slice |
||
286 | |||
287 | /** |
||
288 | * @param string $user |
||
289 | * |
||
290 | * @return Slice |
||
291 | * |
||
292 | * @throws Invalid |
||
293 | */ |
||
294 | protected function refreshToken(string $user): Slice |
||
306 | |||
307 | /** |
||
308 | * @param string $user |
||
309 | * @param string $file |
||
310 | * @param Slice $options |
||
311 | * |
||
312 | * @return Slice |
||
313 | * |
||
314 | * @throws Invalid |
||
315 | */ |
||
316 | public function upload(string $user, string $file, Slice $options = null): Slice |
||
344 | |||
345 | /** |
||
346 | * @param string $user |
||
347 | * @param string $name |
||
348 | * @param Slice|null $options |
||
349 | * |
||
350 | * @return Slice |
||
351 | */ |
||
352 | View Code Duplication | public function update(string $user, string $name, Slice $options = null): Slice |
|
379 | |||
380 | /** |
||
381 | * @param string $user |
||
382 | * @param string $name |
||
383 | * @param Slice|null $options |
||
384 | * |
||
385 | * @return Slice|null |
||
386 | */ |
||
387 | View Code Duplication | public function delete(string $user, string $name, Slice $options = null) |
|
416 | |||
417 | /** |
||
418 | * @param Slice $slice |
||
419 | * @param callable $callback |
||
420 | * |
||
421 | * @return Slice|null |
||
422 | */ |
||
423 | protected function apiSend(Slice $slice, callable $callback) |
||
434 | |||
435 | /** |
||
436 | * @param Slice $token |
||
437 | * @param Slice $options |
||
438 | * @param string $file |
||
439 | * |
||
440 | * @return Slice |
||
441 | */ |
||
442 | protected function uploadSlice(Slice $token, Slice $options, string $file = null): Slice |
||
471 | |||
472 | /** |
||
473 | * @param Slice $token |
||
474 | * |
||
475 | * @return Slice |
||
476 | * |
||
477 | * @throws Invalid |
||
478 | */ |
||
479 | protected function verify(Slice $token): Slice |
||
490 | |||
491 | /** |
||
492 | * @param string $user |
||
493 | * @param Slice $slice |
||
494 | * |
||
495 | * @return Slice |
||
496 | * |
||
497 | * @throws Invalid |
||
498 | */ |
||
499 | protected function tokenUpdate(string $user, Slice $slice): Slice |
||
522 | |||
523 | } |
||
524 |
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.