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 Message 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 Message, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Message |
||
20 | { |
||
21 | /** |
||
22 | * ID. |
||
23 | * |
||
24 | * @var int |
||
25 | */ |
||
26 | private $id; |
||
27 | |||
28 | /** |
||
29 | * QR. |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $isResponse; |
||
34 | |||
35 | /** |
||
36 | * OPCODE. |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | private $opcode; |
||
41 | |||
42 | /** |
||
43 | * AA. |
||
44 | * |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $isAuthoritative; |
||
48 | |||
49 | /** |
||
50 | * TC. |
||
51 | * |
||
52 | * @var bool |
||
53 | */ |
||
54 | private $isTruncated; |
||
55 | |||
56 | /** |
||
57 | * RD. |
||
58 | * |
||
59 | * @var bool |
||
60 | */ |
||
61 | private $isRecursionDesired; |
||
62 | |||
63 | /** |
||
64 | * RA. |
||
65 | * |
||
66 | * @var bool |
||
67 | */ |
||
68 | private $isRecursionAvailable; |
||
69 | |||
70 | /** |
||
71 | * Bit 9 of the header flags. |
||
72 | * |
||
73 | * @var int |
||
74 | */ |
||
75 | private $bit9 = 0; |
||
76 | |||
77 | /** |
||
78 | * AD. |
||
79 | * |
||
80 | * {@link https://tools.ietf.org/html/rfc4035#section-3.2.3} |
||
81 | * |
||
82 | * @var bool |
||
83 | */ |
||
84 | private $isAuthenticData; |
||
85 | |||
86 | /** |
||
87 | * CD. |
||
88 | * |
||
89 | * {@link https://tools.ietf.org/html/rfc4035#section-3.2.2} |
||
90 | * |
||
91 | * @var bool |
||
92 | */ |
||
93 | private $isCheckingDisabled; |
||
94 | |||
95 | /** |
||
96 | * RCODE. |
||
97 | * |
||
98 | * @var int |
||
99 | */ |
||
100 | private $rcode; |
||
101 | |||
102 | /** |
||
103 | * @var Question[] |
||
104 | */ |
||
105 | private $questions = []; |
||
106 | |||
107 | /** |
||
108 | * @var ResourceRecord[] |
||
109 | */ |
||
110 | private $answers = []; |
||
111 | |||
112 | /** |
||
113 | * @var ResourceRecord[] |
||
114 | */ |
||
115 | private $authoritatives = []; |
||
116 | |||
117 | /** |
||
118 | * @var ResourceRecord[] |
||
119 | */ |
||
120 | private $additionals = []; |
||
121 | |||
122 | /** |
||
123 | * Encode a domain name as a sequence of labels. |
||
124 | 43 | */ |
|
125 | public static function encodeName(string $name): string |
||
140 | 30 | ||
141 | public static function decodeName(string $string, int &$offset = 0): string |
||
179 | 2 | ||
180 | public function getId(): int |
||
184 | 8 | ||
185 | public function setId(int $id): void |
||
189 | 2 | ||
190 | public function isResponse(): bool |
||
194 | 8 | ||
195 | public function setResponse(bool $isResponse): void |
||
199 | 1 | ||
200 | public function isQuery(): bool |
||
204 | 1 | ||
205 | public function setQuery(bool $query): void |
||
209 | 2 | ||
210 | public function getOpcode(): int |
||
214 | 7 | ||
215 | public function setOpcode(int $opcode): void |
||
219 | 2 | ||
220 | public function isAuthoritative(): bool |
||
224 | 7 | ||
225 | public function setAuthoritative(bool $isAuthoritative): void |
||
229 | 2 | ||
230 | public function isTruncated(): bool |
||
234 | 7 | ||
235 | public function setTruncated(bool $isTruncated): void |
||
239 | 2 | ||
240 | public function isRecursionDesired(): bool |
||
244 | 7 | ||
245 | public function setRecursionDesired(bool $isRecursionDesired): void |
||
249 | 2 | ||
250 | public function isRecursionAvailable(): bool |
||
254 | 7 | ||
255 | public function setRecursionAvailable(bool $isRecursionAvailable): void |
||
259 | 2 | ||
260 | public function getBit9(): int |
||
264 | 7 | ||
265 | public function setBit9(int $bit9): void |
||
269 | 2 | ||
270 | public function isAuthenticData(): bool |
||
274 | 7 | ||
275 | public function setAuthenticData(bool $isAuthenticData): void |
||
279 | 2 | ||
280 | public function isCheckingDisabled(): bool |
||
284 | 7 | ||
285 | public function setCheckingDisabled(bool $isCheckingDisabled): void |
||
289 | 2 | ||
290 | public function getRcode(): int |
||
294 | 7 | ||
295 | public function setRcode(int $rcode): void |
||
299 | |||
300 | /** |
||
301 | * @return Question[] |
||
302 | 3 | */ |
|
303 | public function getQuestions(): array |
||
307 | 8 | ||
308 | public function addQuestion(Question $question): void |
||
312 | |||
313 | /** |
||
314 | * @param Question[] $questions |
||
315 | 1 | */ |
|
316 | public function setQuestions(array $questions): void |
||
323 | |||
324 | /** |
||
325 | * @return ResourceRecord[] |
||
326 | 3 | */ |
|
327 | public function getAnswers(): array |
||
331 | 7 | ||
332 | public function addAnswer(ResourceRecord $answer): void |
||
336 | |||
337 | /** |
||
338 | * @param ResourceRecord[] $answers |
||
339 | 7 | */ |
|
340 | public function setAnswers(array $answers): void |
||
347 | |||
348 | /** |
||
349 | * @return ResourceRecord[] |
||
350 | 2 | */ |
|
351 | public function getAuthoritatives(): array |
||
355 | 5 | ||
356 | public function addAuthoritative(ResourceRecord $authoritative): void |
||
360 | |||
361 | /** |
||
362 | * @param ResourceRecord[] $authoritatives |
||
363 | 7 | */ |
|
364 | public function setAuthoritatives(array $authoritatives): void |
||
371 | |||
372 | /** |
||
373 | * @return ResourceRecord[] |
||
374 | 3 | */ |
|
375 | public function getAdditionals(): array |
||
379 | 7 | ||
380 | public function addAdditional(ResourceRecord $additional): void |
||
384 | |||
385 | /** |
||
386 | * @param ResourceRecord[] $additionals |
||
387 | 7 | */ |
|
388 | public function setAdditionals(array $additionals): void |
||
395 | 6 | ||
396 | public function countQuestions(): int |
||
400 | 6 | ||
401 | public function countAnswers(): int |
||
405 | 6 | ||
406 | public function countAuthoritatives(): int |
||
410 | 6 | ||
411 | public function countAdditionals(): int |
||
415 | |||
416 | /** |
||
417 | * @throws UnsetValueException |
||
418 | 3 | */ |
|
419 | public function toWire(): string |
||
450 | |||
451 | /** |
||
452 | * @throws UnsupportedTypeException |
||
453 | 7 | */ |
|
454 | public static function fromWire(string $encoded): Message |
||
491 | } |
||
492 |
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.