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 Normalizer 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 Normalizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Normalizer |
||
| 23 | { |
||
| 24 | const NONE = 1; |
||
| 25 | const FORM_D = 2; |
||
| 26 | const NFD = 2; |
||
| 27 | const FORM_KD = 3; |
||
| 28 | const NFKD = 3; |
||
| 29 | const FORM_C = 4; |
||
| 30 | const NFC = 4; |
||
| 31 | const FORM_KC = 5; |
||
| 32 | const NFKC = 5; |
||
| 33 | |||
| 34 | protected static $C, $D, $KD, $cC; |
||
|
|
|||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected static $ulen_mask = array( |
||
| 40 | "\xC0" => 2, |
||
| 41 | "\xD0" => 2, |
||
| 42 | "\xE0" => 3, |
||
| 43 | "\xF0" => 4, |
||
| 44 | ); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected static $ASCII = "\x20\x65\x69\x61\x73\x6E\x74\x72\x6F\x6C\x75\x64\x5D\x5B\x63\x6D\x70\x27\x0A\x67\x7C\x68\x76\x2E\x66\x62\x2C\x3A\x3D\x2D\x71\x31\x30\x43\x32\x2A\x79\x78\x29\x28\x4C\x39\x41\x53\x2F\x50\x22\x45\x6A\x4D\x49\x6B\x33\x3E\x35\x54\x3C\x44\x34\x7D\x42\x7B\x38\x46\x77\x52\x36\x37\x55\x47\x4E\x3B\x4A\x7A\x56\x23\x48\x4F\x57\x5F\x26\x21\x4B\x3F\x58\x51\x25\x59\x5C\x09\x5A\x2B\x7E\x5E\x24\x40\x60\x7F\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * is normalized |
||
| 53 | * |
||
| 54 | * @param string $str |
||
| 55 | * @param int $form |
||
| 56 | * |
||
| 57 | * @return bool |
||
| 58 | */ |
||
| 59 | 8 | public static function isNormalized($str, $form = self::NFC) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * normalize |
||
| 80 | * |
||
| 81 | * @param string $str |
||
| 82 | * @param int $form |
||
| 83 | * |
||
| 84 | * @return false|string false on error |
||
| 85 | */ |
||
| 86 | 20 | public static function normalize($str, $form = self::NFC) |
|
| 87 | { |
||
| 88 | 20 | if (!preg_match('//u', $str .= '')) { |
|
| 89 | 7 | return false; |
|
| 90 | } |
||
| 91 | |||
| 92 | switch ($form) { |
||
| 93 | 18 | case self::NONE: |
|
| 94 | 1 | return $str; |
|
| 95 | 18 | case self::NFC: |
|
| 96 | 7 | $C = true; |
|
| 97 | 7 | $K = false; |
|
| 98 | 7 | break; |
|
| 99 | 13 | case self::NFD: |
|
| 100 | 11 | $C = false; |
|
| 101 | 11 | $K = false; |
|
| 102 | 11 | break; |
|
| 103 | 5 | case self::NFKC: |
|
| 104 | 5 | $C = true; |
|
| 105 | 5 | $K = true; |
|
| 106 | 5 | break; |
|
| 107 | 2 | case self::NFKD: |
|
| 108 | 2 | $C = false; |
|
| 109 | 2 | $K = true; |
|
| 110 | 2 | break; |
|
| 111 | 1 | default: |
|
| 112 | 1 | return false; |
|
| 113 | 1 | } |
|
| 114 | |||
| 115 | 18 | if ('' === $str) { |
|
| 116 | 3 | return ''; |
|
| 117 | } |
||
| 118 | |||
| 119 | 18 | if ($K && empty(self::$KD)) { |
|
| 120 | 1 | self::$KD = static::getData('compatibilityDecomposition'); |
|
| 121 | 1 | } |
|
| 122 | |||
| 123 | 18 | if (empty(self::$D)) { |
|
| 124 | 1 | self::$D = static::getData('canonicalDecomposition'); |
|
| 125 | 1 | self::$cC = static::getData('combiningClass'); |
|
| 126 | 1 | } |
|
| 127 | |||
| 128 | 18 | if ($C) { |
|
| 129 | 10 | if (empty(self::$C)) { |
|
| 130 | 1 | self::$C = static::getData('canonicalComposition'); |
|
| 131 | 1 | } |
|
| 132 | |||
| 133 | 10 | return self::recompose(self::decompose($str, $K)); |
|
| 134 | } else { |
||
| 135 | 11 | return self::decompose($str, $K); |
|
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * getData |
||
| 141 | * |
||
| 142 | * @param string $file |
||
| 143 | * |
||
| 144 | * @return bool|mixed |
||
| 145 | */ |
||
| 146 | 2 | View Code Duplication | protected static function getData($file) |
| 147 | { |
||
| 148 | 2 | $file = __DIR__ . '/unidata/' . $file . '.ser'; |
|
| 149 | 2 | if (file_exists($file)) { |
|
| 150 | 2 | return unserialize(file_get_contents($file)); |
|
| 151 | } else { |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * recompose |
||
| 158 | * |
||
| 159 | * @param string $s |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | 10 | protected static function recompose($s) |
|
| 164 | { |
||
| 165 | 10 | $ASCII = self::$ASCII; |
|
| 166 | 10 | $compMap = self::$C; |
|
| 167 | 10 | $combClass = self::$cC; |
|
| 168 | 10 | $ulen_mask = self::$ulen_mask; |
|
| 169 | |||
| 170 | 10 | $result = $tail = ''; |
|
| 171 | |||
| 172 | 10 | $i = $s[0] < "\x80" ? 1 : $ulen_mask[$s[0] & "\xF0"]; |
|
| 173 | 10 | $len = strlen($s); |
|
| 174 | |||
| 175 | 10 | $last_uchr = substr($s, 0, $i); |
|
| 176 | 10 | $last_ucls = isset($combClass[$last_uchr]) ? 256 : 0; |
|
| 177 | |||
| 178 | 10 | while ($i < $len) { |
|
| 179 | 10 | if ($s[$i] < "\x80") { |
|
| 180 | // ASCII chars |
||
| 181 | |||
| 182 | 9 | if ($tail) { |
|
| 183 | 2 | $last_uchr .= $tail; |
|
| 184 | 2 | $tail = ''; |
|
| 185 | 2 | } |
|
| 186 | |||
| 187 | 9 | $j = strspn($s, $ASCII, $i + 1); |
|
| 188 | |||
| 189 | 9 | if ($j) { |
|
| 190 | 8 | $last_uchr .= substr($s, $i, $j); |
|
| 191 | 8 | $i += $j; |
|
| 192 | 8 | } |
|
| 193 | |||
| 194 | 9 | $result .= $last_uchr; |
|
| 195 | 9 | $last_uchr = $s[$i]; |
|
| 196 | 9 | ++$i; |
|
| 197 | 9 | } else { |
|
| 198 | 10 | $ulen = $ulen_mask[$s[$i] & "\xF0"]; |
|
| 199 | 10 | $uchr = substr($s, $i, $ulen); |
|
| 200 | |||
| 201 | if ( |
||
| 202 | $last_uchr < "\xE1\x84\x80" |
||
| 203 | 10 | || |
|
| 204 | "\xE1\x84\x92" < $last_uchr |
||
| 205 | 6 | || |
|
| 206 | $uchr < "\xE1\x85\xA1" |
||
| 207 | 4 | || |
|
| 208 | "\xE1\x85\xB5" < $uchr |
||
| 209 | 4 | || |
|
| 210 | $last_ucls |
||
| 211 | 10 | ) { |
|
| 212 | // Table lookup and combining chars composition |
||
| 213 | |||
| 214 | 10 | $ucls = isset($combClass[$uchr]) ? $combClass[$uchr] : 0; |
|
| 215 | |||
| 216 | 10 | if (isset($compMap[$last_uchr . $uchr]) && (!$last_ucls || $last_ucls < $ucls)) { |
|
| 217 | 9 | $last_uchr = $compMap[$last_uchr . $uchr]; |
|
| 218 | 10 | } elseif ($last_ucls = $ucls) { // this "=" isn't a typo |
|
| 219 | 3 | $tail .= $uchr; |
|
| 220 | 3 | } else { |
|
| 221 | 7 | if ($tail) { |
|
| 222 | 3 | $last_uchr .= $tail; |
|
| 223 | 3 | $tail = ''; |
|
| 224 | 3 | } |
|
| 225 | |||
| 226 | 7 | $result .= $last_uchr; |
|
| 227 | 7 | $last_uchr = $uchr; |
|
| 228 | } |
||
| 229 | 10 | } else { |
|
| 230 | // Hangul chars |
||
| 231 | |||
| 232 | 4 | $L = ord($last_uchr[2]) - 0x80; |
|
| 233 | 4 | $V = ord($uchr[2]) - 0xA1; |
|
| 234 | 4 | $T = 0; |
|
| 235 | |||
| 236 | 4 | $uchr = substr($s, $i + $ulen, 3); |
|
| 237 | |||
| 238 | 4 | if ("\xE1\x86\xA7" <= $uchr && $uchr <= "\xE1\x87\x82") { |
|
| 239 | 4 | $T = ord($uchr[2]) - 0xA7; |
|
| 240 | 4 | 0 > $T && $T += 0x40; |
|
| 241 | 4 | $ulen += 3; |
|
| 242 | 4 | } |
|
| 243 | |||
| 244 | 4 | $L = 0xAC00 + ($L * 21 + $V) * 28 + $T; |
|
| 245 | 4 | $last_uchr = chr(0xE0 | $L >> 12) . chr(0x80 | $L >> 6 & 0x3F) . chr(0x80 | $L & 0x3F); |
|
| 246 | } |
||
| 247 | |||
| 248 | 10 | $i += $ulen; |
|
| 249 | } |
||
| 250 | 10 | } |
|
| 251 | |||
| 252 | 10 | $result = $result . $last_uchr . $tail; |
|
| 253 | |||
| 254 | 10 | return $result; |
|
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * decompose |
||
| 259 | * |
||
| 260 | * @param string $s |
||
| 261 | * @param bool $c |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | 18 | protected static function decompose($s, $c) |
|
| 382 | } |
||
| 383 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.