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 DNParser 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 DNParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class DNParser |
||
16 | { |
||
17 | /** |
||
18 | * DN string. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private $_dn; |
||
23 | |||
24 | /** |
||
25 | * DN string length. |
||
26 | * |
||
27 | * @var int |
||
28 | */ |
||
29 | private $_len; |
||
30 | |||
31 | /** |
||
32 | * RFC 2253 special characters. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | const SPECIAL_CHARS = ",=+<>#;"; |
||
37 | |||
38 | /** |
||
39 | * Parse distinguished name string to name-components. |
||
40 | * |
||
41 | * @param string $dn |
||
42 | * @return array |
||
43 | */ |
||
44 | 69 | public static function parseString($dn) { |
|
48 | |||
49 | /** |
||
50 | * Escape a AttributeValue string conforming to RFC 2253. |
||
51 | * |
||
52 | * @link https://tools.ietf.org/html/rfc2253#section-2.4 |
||
53 | * @param string $str |
||
54 | * @return string |
||
55 | */ |
||
56 | 30 | public static function escapeString($str) { |
|
75 | |||
76 | /** |
||
77 | * Constructor |
||
78 | * |
||
79 | * @param string $dn Distinguised name |
||
80 | */ |
||
81 | 69 | protected function __construct($dn) { |
|
85 | |||
86 | /** |
||
87 | * Parse DN to name-components. |
||
88 | * |
||
89 | * @throws \RuntimeException |
||
90 | * @return array |
||
91 | */ |
||
92 | 69 | protected function parse() { |
|
103 | |||
104 | /** |
||
105 | * Parse 'name'. |
||
106 | * |
||
107 | * name-component *("," name-component) |
||
108 | * |
||
109 | * @param int $offset |
||
110 | * @return array Array of name-components |
||
111 | */ |
||
112 | 69 | private function _parseName(&$offset) { |
|
130 | |||
131 | /** |
||
132 | * Parse 'name-component'. |
||
133 | * |
||
134 | * attributeTypeAndValue *("+" attributeTypeAndValue) |
||
135 | * |
||
136 | * @param int $offset |
||
137 | * @return array Array of [type, value] tuples |
||
138 | */ |
||
139 | 69 | private function _parseNameComponent(&$offset) { |
|
154 | |||
155 | /** |
||
156 | * Parse 'attributeTypeAndValue'. |
||
157 | * |
||
158 | * attributeType "=" attributeValue |
||
159 | * |
||
160 | * @param int $offset |
||
161 | * @throws \UnexpectedValueException |
||
162 | * @return array A tuple of [type, value]. Value may be either a string or |
||
163 | * an Element, if it's encoded as hexstring. |
||
164 | */ |
||
165 | 69 | private function _parseAttrTypeAndValue(&$offset) { |
|
189 | |||
190 | /** |
||
191 | * Parse 'attributeType'. |
||
192 | * |
||
193 | * (ALPHA 1*keychar) / oid |
||
194 | * |
||
195 | * @param int $offset |
||
196 | * @throws \UnexpectedValueException |
||
197 | * @return string |
||
198 | */ |
||
199 | 69 | View Code Duplication | private function _parseAttrType(&$offset) { |
213 | |||
214 | /** |
||
215 | * Parse 'attributeValue' of string type. |
||
216 | * |
||
217 | * @param int $offset |
||
218 | * @throws \UnexpectedValueException |
||
219 | * @return string |
||
220 | */ |
||
221 | 59 | private function _parseAttrStringValue(&$offset) { |
|
234 | |||
235 | /** |
||
236 | * Parse plain 'attributeValue' string. |
||
237 | * |
||
238 | * @param int $offset |
||
239 | * @throws \UnexpectedValueException |
||
240 | * @return string |
||
241 | */ |
||
242 | 54 | private function _parseAttrString(&$offset) { |
|
278 | |||
279 | /** |
||
280 | * Parse quoted 'attributeValue' string. |
||
281 | * |
||
282 | * @param int $offset Offset to starting quote |
||
283 | * @throws \UnexpectedValueException |
||
284 | * @return string |
||
285 | */ |
||
286 | 4 | private function _parseQuotedAttrString(&$offset) { |
|
305 | |||
306 | /** |
||
307 | * Parse 'attributeValue' of binary type. |
||
308 | * |
||
309 | * @param int $offset |
||
310 | * @throws \UnexpectedValueException |
||
311 | * @return string |
||
312 | */ |
||
313 | 8 | View Code Duplication | private function _parseAttrHexValue(&$offset) { |
323 | |||
324 | /** |
||
325 | * Parse 'pair' after leading slash. |
||
326 | * |
||
327 | * @param int $offset |
||
328 | * @throws \UnexpectedValueException |
||
329 | * @return string |
||
330 | */ |
||
331 | 11 | private function _parsePairAfterSlash(&$offset) { |
|
353 | |||
354 | /** |
||
355 | * Match DN to pattern and extract the last capture group. |
||
356 | * |
||
357 | * Updates offset to fully matched pattern. |
||
358 | * |
||
359 | * @param string $pattern |
||
360 | * @param int $offset |
||
361 | * @return string|null Null if pattern doesn't match |
||
362 | */ |
||
363 | 69 | private function _regexMatch($pattern, &$offset) { |
|
372 | |||
373 | /** |
||
374 | * Skip consecutive spaces. |
||
375 | * |
||
376 | * @param int $offset |
||
377 | */ |
||
378 | 68 | private function _skipWs(&$offset) { |
|
388 | } |
||
389 |
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.