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 GlLinkCheckerError 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 GlLinkCheckerError, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class GlLinkCheckerError |
||
30 | { |
||
31 | /** |
||
32 | * @var \GuzzleHttp\Client $client |
||
33 | */ |
||
34 | private $client; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $link; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $url; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $files; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | private $statuscode = 0; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | private $isAbsolute = true; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $isExist = true; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $isLowerCase = true; |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $isNotEndSlash = true; |
||
76 | |||
77 | /** |
||
78 | * Client $client |
||
79 | * string $link |
||
80 | * array $files |
||
81 | */ |
||
82 | public function __construct(Client $client, $link, $files) |
||
94 | |||
95 | /** |
||
96 | * @return bool |
||
97 | */ |
||
98 | View Code Duplication | private function checkexisthead() |
|
116 | |||
117 | /** |
||
118 | * @return bool |
||
119 | */ |
||
120 | View Code Duplication | private function checkexistget() |
|
137 | |||
138 | /** |
||
139 | * |
||
140 | */ |
||
141 | private function checkexist() |
||
152 | |||
153 | /** |
||
154 | * @return bool |
||
155 | */ |
||
156 | private function checkendslash() |
||
177 | |||
178 | /** |
||
179 | * @return bool |
||
180 | */ |
||
181 | private function checkabsolute() |
||
197 | |||
198 | /** |
||
199 | * @return bool |
||
200 | */ |
||
201 | private function checklowercase() |
||
207 | |||
208 | |||
209 | /** |
||
210 | * @param array|null $internalurls |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function isInternal($internalurls) |
||
232 | |||
233 | /** |
||
234 | * @param array $list |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function check(array $list) |
||
248 | |||
249 | /** |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getLink() |
||
256 | |||
257 | /** |
||
258 | * @return array |
||
259 | */ |
||
260 | public function getFiles() |
||
264 | |||
265 | /** |
||
266 | * @return int |
||
267 | */ |
||
268 | public function getStatusCode() |
||
272 | |||
273 | /** |
||
274 | * @return array |
||
275 | */ |
||
276 | public function getErrorMessages() |
||
298 | |||
299 | /** |
||
300 | * @return array |
||
301 | */ |
||
302 | public function getErrorArray() { |
||
312 | } |
||
313 |
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.