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) |
|
| 60 | { |
||
| 61 | 8 | if (strspn($str .= '', self::$ASCII) === strlen($str)) { |
|
| 62 | 1 | return true; |
|
| 63 | } |
||
| 64 | |||
| 65 | if ( |
||
| 66 | 8 | self::NFC === $form |
|
| 67 | && |
||
| 68 | 8 | preg_match('//u', $str) |
|
| 69 | && |
||
| 70 | 8 | !preg_match('/[^\x00-\x{2FF}]/u', $str) |
|
| 71 | ) { |
||
| 72 | 3 | return true; |
|
| 73 | } |
||
| 74 | |||
| 75 | 8 | return false; // Pretend false as quick checks implemented in PHP won't be so quick |
|
| 76 | } |
||
| 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 | default: |
||
| 112 | 1 | return false; |
|
| 113 | } |
||
| 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 | } |
||
| 122 | |||
| 123 | 18 | if (empty(self::$D)) { |
|
| 124 | 1 | self::$D = static::getData('canonicalDecomposition'); |
|
| 125 | 1 | self::$cC = static::getData('combiningClass'); |
|
| 126 | } |
||
| 127 | |||
| 128 | 18 | if ($C) { |
|
| 129 | 10 | if (empty(self::$C)) { |
|
| 130 | 1 | self::$C = static::getData('canonicalComposition'); |
|
| 131 | } |
||
| 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) |
| 155 | |||
| 156 | /** |
||
| 157 | * recompose |
||
| 158 | * |
||
| 159 | * @param string $s |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | 10 | protected static function recompose($s) |
|
| 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.