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 RData 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 RData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class RData |
||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $rdataFormats = [ |
||
25 | 'SOA' => [ |
||
26 | 'MNAME' => 'defaultExtractor', |
||
27 | 'RNAME' => 'defaultExtractor', |
||
28 | 'SERIAL' => 'defaultExtractor', |
||
29 | 'REFRESH' => 'defaultExtractor', |
||
30 | 'RETRY' => 'defaultExtractor', |
||
31 | 'EXPIRE' => 'defaultExtractor', |
||
32 | 'MINIMUM' => 'defaultExtractor', |
||
33 | ], |
||
34 | 'A' => [ |
||
35 | 'ADDRESS' => 'defaultExtractor' |
||
36 | ], |
||
37 | 'AAAA' => [ |
||
38 | 'ADDRESS' => 'defaultExtractor' |
||
39 | ], |
||
40 | 'CNAME' => [ |
||
41 | 'CNAME' => 'defaultExtractor' |
||
42 | ], |
||
43 | 'MX' => [ |
||
44 | 'PREFERENCE' => 'defaultExtractor', |
||
45 | 'EXCHANGE' => 'defaultExtractor' |
||
46 | ], |
||
47 | 'NS' => [ |
||
48 | 'NSDNAME' => 'defaultExtractor' |
||
49 | ], |
||
50 | 'PTR' => [ |
||
51 | 'PTRDNAME' => 'defaultExtractor' |
||
52 | ], |
||
53 | 'SRV' => [ |
||
54 | 'PRIORITY' => 'defaultExtractor', |
||
55 | 'WEIGHT' => 'defaultExtractor', |
||
56 | 'PORT' => 'defaultExtractor', |
||
57 | 'TARGET' => 'defaultExtractor' |
||
58 | ], |
||
59 | 'TXT' => [ |
||
60 | 'TXTDATA' => 'txtExtractor' |
||
61 | ], |
||
62 | 'CAA' => [ |
||
63 | 'FLAGS' => 'defaultExtractor', |
||
64 | 'TAG' => 'defaultExtractor', |
||
65 | 'VALUE' => 'defaultExtractor' |
||
66 | ], |
||
67 | 'NAPTR' => [ |
||
68 | 'ORDER' => 'defaultExtractor', |
||
69 | 'PREFERENCE' => 'defaultExtractor', |
||
70 | 'FLAGS' => 'defaultExtractor', |
||
71 | 'SERVICES' => 'defaultExtractor', |
||
72 | 'REGEXP' => 'defaultExtractor', |
||
73 | 'REPLACEMENT' => 'defaultExtractor' |
||
74 | ] |
||
75 | ]; |
||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | private $type; |
||
80 | /** |
||
81 | * @var array |
||
82 | */ |
||
83 | private $tokens = []; |
||
84 | |||
85 | /** |
||
86 | * @var bool |
||
87 | */ |
||
88 | 6 | private $commentOpen = false; |
|
89 | |||
90 | 6 | /** |
|
91 | * @var bool |
||
92 | */ |
||
93 | private $multiLineOpened = false; |
||
94 | 6 | ||
95 | 6 | /** |
|
96 | 6 | * RData constructor. |
|
97 | * |
||
98 | * @param StringStream $stream |
||
99 | * @param string $type |
||
100 | */ |
||
101 | public function __construct(StringStream $stream, string $type) |
||
110 | 6 | ||
111 | /** |
||
112 | 6 | * @param string $type |
|
113 | 6 | * @return bool |
|
114 | */ |
||
115 | public static function isKnownType(string $type) : bool |
||
119 | |||
120 | /** |
||
121 | * @return array |
||
122 | */ |
||
123 | public function tokenize() : array |
||
133 | |||
134 | 6 | /** |
|
135 | 6 | * @param string $tokenName |
|
136 | */ |
||
137 | protected function defaultExtractor(string $tokenName) |
||
211 | |||
212 | 6 | View Code Duplication | private function ignoreComment() |
220 | 5 | ||
221 | 2 | protected function endRecord() |
|
253 | 1 | ||
254 | /** |
||
255 | 1 | * @param string $tokenName |
|
256 | */ |
||
257 | private function txtExtractor(string $tokenName) |
||
318 | |||
319 | /** |
||
320 | * @param string $tokenName |
||
321 | 1 | */ |
|
322 | 1 | private function extractCharSet(string $tokenName) |
|
345 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: