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) |
||
102 | 6 | { |
|
103 | if (! self::isKnownType($type)) { |
||
104 | 6 | throw new SyntaxErrorException($stream); |
|
105 | } |
||
106 | |||
107 | $this->stream = $stream; |
||
|
|||
108 | $this->type = $type; |
||
109 | } |
||
110 | 6 | ||
111 | /** |
||
112 | 6 | * @param string $type |
|
113 | 6 | * @return bool |
|
114 | */ |
||
115 | public static function isKnownType(string $type) : bool |
||
116 | 6 | { |
|
117 | return array_key_exists($type, self::$rdataFormats); |
||
118 | 6 | } |
|
119 | |||
120 | /** |
||
121 | * @return array |
||
122 | */ |
||
123 | public function tokenize() : array |
||
124 | 6 | { |
|
125 | foreach (self::$rdataFormats[$this->type] as $tokenName => $extractor) { |
||
126 | 6 | $this->$extractor($tokenName); |
|
127 | 5 | } |
|
128 | |||
129 | 6 | $this->endRecord(); |
|
130 | |||
131 | return $this->tokens; |
||
132 | 6 | } |
|
133 | |||
134 | 6 | /** |
|
135 | 6 | * @param string $tokenName |
|
136 | */ |
||
137 | protected function defaultExtractor(string $tokenName) |
||
138 | { |
||
139 | if($this->multiLineOpened) { |
||
140 | 6 | $this->stream->ignoreWhitespace(); |
|
141 | } else { |
||
142 | 6 | $this->stream->ignoreHorizontalSpace(); |
|
143 | 4 | } |
|
144 | |||
145 | $this->commentOpen = false; |
||
146 | 6 | ||
147 | 5 | if (!array_key_exists($tokenName, $this->tokens)) { |
|
148 | 5 | $this->tokens[$tokenName] = ''; |
|
149 | 5 | } |
|
150 | 6 | ||
151 | 1 | start: |
|
152 | 1 | ||
153 | 1 | $ord = $this->stream->ord(); |
|
154 | 6 | ||
155 | 4 | if($ord === AsciiChar::NULL || $this->stream->isEnd()) { |
|
156 | 4 | return; |
|
157 | 6 | } |
|
158 | 4 | ||
159 | if($ord === AsciiChar::OPEN_BRACKET && !$this->commentOpen) { |
||
160 | 6 | $this->multiLineOpened = true; |
|
161 | 4 | $this->stream->next(); |
|
162 | 4 | goto start; |
|
163 | } elseif($this->multiLineOpened && !$this->commentOpen && $ord === AsciiChar::CLOSE_BRACKET) { |
||
164 | 3 | $this->multiLineOpened = false; |
|
165 | 3 | $this->stream->next(); |
|
166 | 3 | goto start; |
|
167 | } elseif($this->multiLineOpened && !$this->commentOpen && $ord === AsciiChar::LINE_FEED) { |
||
168 | 1 | $this->stream->next(); |
|
169 | 1 | goto start; |
|
170 | 1 | } elseif($ord === AsciiChar::LINE_FEED && !$this->commentOpen) { |
|
171 | return; |
||
172 | 4 | } else { |
|
173 | 6 | if($ord === AsciiChar::SEMICOLON) { |
|
174 | 3 | $this->stream->previous(); |
|
175 | 3 | if($this->stream->currentAscii()->isHorizontalSpace()) { |
|
176 | 3 | ||
177 | 3 | $this->commentOpen = true; |
|
178 | 6 | $this->stream->next(); |
|
179 | 3 | $this->stream->next(); |
|
180 | 3 | } else { |
|
181 | 3 | $this->stream->next(); |
|
182 | 6 | $this->tokens[$tokenName] .= $this->stream->current(); |
|
183 | 6 | $this->stream->next(); |
|
184 | 4 | } |
|
185 | 4 | goto start; |
|
186 | 6 | } elseif(($this->stream->currentAscii()->isVerticalSpace() || $ord === AsciiChar::NULL) && $this->commentOpen) { |
|
187 | 5 | $this->stream->next(); |
|
188 | $this->stream->ignoreHorizontalSpace(); |
||
189 | 6 | $this->commentOpen = false; |
|
190 | 6 | goto start; |
|
191 | 6 | } elseif($this->commentOpen) { |
|
192 | 4 | $this->commentOpen = true; |
|
193 | $this->ignoreComment(); |
||
194 | 6 | goto start; |
|
195 | } elseif(!$this->commentOpen) { |
||
196 | if($ord === AsciiChar::SPACE && $this->tokens[$tokenName] === '') { |
||
197 | $this->stream->next(); |
||
198 | goto start; |
||
199 | } elseif($this->stream->currentAscii()->isWhiteSpace()) { |
||
200 | 3 | return; |
|
201 | } else { |
||
202 | $this->tokens[$tokenName] .= $this->stream->current(); |
||
203 | 3 | $this->stream->next(); |
|
204 | 3 | if($this->stream->isEnd()) { |
|
205 | 3 | $this->tokens[$tokenName] = trim($this->tokens[$tokenName]); |
|
206 | } |
||
207 | 3 | goto start; |
|
208 | } |
||
209 | 6 | } |
|
210 | } |
||
211 | } |
||
212 | 6 | ||
213 | 6 | View Code Duplication | private function ignoreComment() |
221 | 2 | ||
222 | 2 | protected function endRecord() |
|
223 | 2 | { |
|
224 | start: |
||
225 | 2 | $ord = $this->stream->ord(); |
|
254 | |||
255 | 1 | /** |
|
256 | * @param string $tokenName |
||
257 | */ |
||
258 | private function txtExtractor(string $tokenName) |
||
319 | |||
320 | /** |
||
321 | 1 | * @param string $tokenName |
|
322 | 1 | */ |
|
323 | private function extractCharSet(string $tokenName) |
||
346 | } |
||
347 |
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: