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 Punycode 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 Punycode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Punycode |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Bootstring parameter values |
||
17 | * |
||
18 | */ |
||
19 | const BASE = 36; |
||
20 | const TMIN = 1; |
||
21 | const TMAX = 26; |
||
22 | const SKEW = 38; |
||
23 | const DAMP = 700; |
||
24 | const INITIAL_BIAS = 72; |
||
25 | const INITIAL_N = 128; |
||
26 | const PREFIX = 'xn--'; |
||
27 | const DELIMITER = '-'; |
||
28 | |||
29 | /** |
||
30 | * Encode table |
||
31 | * |
||
32 | * @param array |
||
33 | */ |
||
34 | protected static $encodeTable = array( |
||
35 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', |
||
36 | 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', |
||
37 | 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
||
38 | ); |
||
39 | |||
40 | /** |
||
41 | * Decode table |
||
42 | * |
||
43 | * @param array |
||
44 | */ |
||
45 | protected static $decodeTable = array( |
||
46 | 'a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 5, |
||
47 | 'g' => 6, 'h' => 7, 'i' => 8, 'j' => 9, 'k' => 10, 'l' => 11, |
||
48 | 'm' => 12, 'n' => 13, 'o' => 14, 'p' => 15, 'q' => 16, 'r' => 17, |
||
49 | 's' => 18, 't' => 19, 'u' => 20, 'v' => 21, 'w' => 22, 'x' => 23, |
||
50 | 'y' => 24, 'z' => 25, '0' => 26, '1' => 27, '2' => 28, '3' => 29, |
||
51 | '4' => 30, '5' => 31, '6' => 32, '7' => 33, '8' => 34, '9' => 35 |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Character encoding |
||
56 | * |
||
57 | * @param string |
||
58 | */ |
||
59 | protected $encoding; |
||
60 | |||
61 | /** |
||
62 | * Constructor |
||
63 | * |
||
64 | * @param string $encoding Character encoding |
||
65 | */ |
||
66 | public function __construct($encoding = 'UTF-8') |
||
70 | |||
71 | /** |
||
72 | * Encode a domain to its Punycode version |
||
73 | * |
||
74 | * @param string $input Domain name in Unicode to be encoded |
||
75 | * @return string Punycode representation in ASCII |
||
76 | */ |
||
77 | public function encode($input) |
||
96 | |||
97 | /** |
||
98 | * Encode a part of a domain name, such as tld, to its Punycode version |
||
99 | * |
||
100 | * @param string $input Part of a domain name |
||
101 | * @return string Punycode representation of a domain part |
||
102 | */ |
||
103 | protected function encodePart($input) |
||
169 | |||
170 | /** |
||
171 | * Decode a Punycode domain name to its Unicode counterpart |
||
172 | * |
||
173 | * @param string $input Domain name in Punycode |
||
174 | * @return string Unicode domain name |
||
175 | */ |
||
176 | public function decode($input) |
||
200 | |||
201 | /** |
||
202 | * Decode a part of domain name, such as tld |
||
203 | * |
||
204 | * @param string $input Part of a domain name |
||
205 | * @return string Unicode domain part |
||
206 | */ |
||
207 | protected function decodePart($input) |
||
249 | |||
250 | /** |
||
251 | * Calculate the bias threshold to fall between TMIN and TMAX |
||
252 | * |
||
253 | * @param integer $k |
||
254 | * @param integer $bias |
||
255 | * @return integer |
||
256 | */ |
||
257 | protected function calculateThreshold($k, $bias) |
||
266 | |||
267 | /** |
||
268 | * Bias adaptation |
||
269 | * |
||
270 | * @param integer $delta |
||
271 | * @param integer $numPoints |
||
272 | * @param boolean $firstTime |
||
273 | * @return integer |
||
274 | */ |
||
275 | protected function adapt($delta, $numPoints, $firstTime) |
||
293 | |||
294 | /** |
||
295 | * List code points for a given input |
||
296 | * |
||
297 | * @param string $input |
||
298 | * @return array Multi-dimension array with basic, non-basic and aggregated code points |
||
299 | */ |
||
300 | protected function listCodePoints($input) |
||
321 | |||
322 | /** |
||
323 | * Convert a single or multi-byte character to its code point |
||
324 | * |
||
325 | * @param string $char |
||
326 | * @return integer |
||
327 | */ |
||
328 | protected function charToCodePoint($char) |
||
341 | |||
342 | /** |
||
343 | * Convert a code point to its single or multi-byte character |
||
344 | * |
||
345 | * @param integer $code |
||
346 | * @return string |
||
347 | */ |
||
348 | protected function codePointToChar($code) |
||
360 | } |
||
361 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.