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 Inflector 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 Inflector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Inflector |
||
| 11 | { |
||
| 12 | |||
| 13 | protected $plural = [ |
||
| 14 | '/(s)tatus$/i' => '\1tatuses', |
||
| 15 | '/(quiz)$/i' => '\1zes', |
||
| 16 | '/^(ox)$/i' => '\1\2en', |
||
| 17 | '/([m|l])ouse$/i' => '\1ice', |
||
| 18 | '/(matr|vert|ind)(ix|ex)$/i' => '\1ices', |
||
| 19 | '/(x|ch|ss|sh)$/i' => '\1es', |
||
| 20 | '/([^aeiouy]|qu)y$/i' => '\1ies', |
||
| 21 | '/(hive)$/i' => '\1s', |
||
| 22 | '/(chef)$/i' => '\1s', |
||
| 23 | '/(?:([^f])fe|([lre])f)$/i' => '\1\2ves', |
||
| 24 | '/sis$/i' => 'ses', |
||
| 25 | '/([ti])um$/i' => '\1a', |
||
| 26 | '/(p)erson$/i' => '\1eople', |
||
| 27 | '/(?<!u)(m)an$/i' => '\1en', |
||
| 28 | '/(c)hild$/i' => '\1hildren', |
||
| 29 | '/(buffal|tomat)o$/i' => '\1\2oes', |
||
| 30 | '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin)us$/i' => '\1i', |
||
| 31 | '/us$/i' => 'uses', |
||
| 32 | '/(alias)$/i' => '\1es', |
||
| 33 | '/(ax|cris|test)is$/i' => '\1es', |
||
| 34 | '/s$/' => 's', |
||
| 35 | '/^$/' => '', |
||
| 36 | '/$/' => 's', |
||
| 37 | ]; |
||
| 38 | protected $singular = [ |
||
| 39 | '/(s)tatuses$/i' => '\1\2tatus', |
||
| 40 | '/^(.*)(menu)s$/i' => '\1\2', |
||
| 41 | '/(quiz)zes$/i' => '\\1', |
||
| 42 | '/(matr)ices$/i' => '\1ix', |
||
| 43 | '/(vert|ind)ices$/i' => '\1ex', |
||
| 44 | '/^(ox)en/i' => '\1', |
||
| 45 | '/(alias)(es)*$/i' => '\1', |
||
| 46 | '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us', |
||
| 47 | '/([ftw]ax)es/i' => '\1', |
||
| 48 | '/(cris|ax|test)es$/i' => '\1is', |
||
| 49 | '/(shoe)s$/i' => '\1', |
||
| 50 | '/(o)es$/i' => '\1', |
||
| 51 | '/ouses$/' => 'ouse', |
||
| 52 | '/([^a])uses$/' => '\1us', |
||
| 53 | '/([m|l])ice$/i' => '\1ouse', |
||
| 54 | '/(x|ch|ss|sh)es$/i' => '\1', |
||
| 55 | '/(m)ovies$/i' => '\1\2ovie', |
||
| 56 | '/(s)eries$/i' => '\1\2eries', |
||
| 57 | '/([^aeiouy]|qu)ies$/i' => '\1y', |
||
| 58 | '/(tive)s$/i' => '\1', |
||
| 59 | '/(hive)s$/i' => '\1', |
||
| 60 | '/(drive)s$/i' => '\1', |
||
| 61 | '/([le])ves$/i' => '\1f', |
||
| 62 | '/([^rfoa])ves$/i' => '\1fe', |
||
| 63 | '/(^analy)ses$/i' => '\1sis', |
||
| 64 | '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', |
||
| 65 | '/([ti])a$/i' => '\1um', |
||
| 66 | '/(p)eople$/i' => '\1\2erson', |
||
| 67 | '/(m)en$/i' => '\1an', |
||
| 68 | '/(c)hildren$/i' => '\1\2hild', |
||
| 69 | '/(n)ews$/i' => '\1\2ews', |
||
| 70 | '/eaus$/' => 'eau', |
||
| 71 | '/^(.*us)$/' => '\\1', |
||
| 72 | '/s$/i' => '' |
||
| 73 | ]; |
||
| 74 | protected $uncountable = [ |
||
| 75 | '.*[nrlm]ese', |
||
| 76 | '.*data', |
||
| 77 | '.*deer', |
||
| 78 | '.*fish', |
||
| 79 | '.*measles', |
||
| 80 | '.*ois', |
||
| 81 | '.*pox', |
||
| 82 | '.*sheep', |
||
| 83 | 'people', |
||
| 84 | 'feedback', |
||
| 85 | 'stadia', |
||
| 86 | '.*?media', |
||
| 87 | 'chassis', |
||
| 88 | 'clippers', |
||
| 89 | 'debris', |
||
| 90 | 'diabetes', |
||
| 91 | 'equipment', |
||
| 92 | 'gallows', |
||
| 93 | 'graffiti', |
||
| 94 | 'headquarters', |
||
| 95 | 'information', |
||
| 96 | 'innings', |
||
| 97 | 'news', |
||
| 98 | 'nexus', |
||
| 99 | 'pokemon', |
||
| 100 | 'proceedings', |
||
| 101 | 'research', |
||
| 102 | 'sea[- ]bass', |
||
| 103 | 'series', |
||
| 104 | 'species', |
||
| 105 | 'weather' |
||
| 106 | ]; |
||
| 107 | protected $irregular = [ |
||
| 108 | 'atlas' => 'atlases', |
||
| 109 | 'beef' => 'beefs', |
||
| 110 | 'brief' => 'briefs', |
||
| 111 | 'brother' => 'brothers', |
||
| 112 | 'cafe' => 'cafes', |
||
| 113 | 'child' => 'children', |
||
| 114 | 'cookie' => 'cookies', |
||
| 115 | 'corpus' => 'corpuses', |
||
| 116 | 'cow' => 'cows', |
||
| 117 | 'criterion' => 'criteria', |
||
| 118 | 'ganglion' => 'ganglions', |
||
| 119 | 'genie' => 'genies', |
||
| 120 | 'genus' => 'genera', |
||
| 121 | 'graffito' => 'graffiti', |
||
| 122 | 'hoof' => 'hoofs', |
||
| 123 | 'loaf' => 'loaves', |
||
| 124 | 'man' => 'men', |
||
| 125 | 'money' => 'monies', |
||
| 126 | 'mongoose' => 'mongooses', |
||
| 127 | 'move' => 'moves', |
||
| 128 | 'mythos' => 'mythoi', |
||
| 129 | 'niche' => 'niches', |
||
| 130 | 'numen' => 'numina', |
||
| 131 | 'occiput' => 'occiputs', |
||
| 132 | 'octopus' => 'octopuses', |
||
| 133 | 'opus' => 'opuses', |
||
| 134 | 'ox' => 'oxen', |
||
| 135 | 'penis' => 'penises', |
||
| 136 | 'person' => 'people', |
||
| 137 | 'sex' => 'sexes', |
||
| 138 | 'soliloquy' => 'soliloquies', |
||
| 139 | 'testis' => 'testes', |
||
| 140 | 'trilby' => 'trilbys', |
||
| 141 | 'turf' => 'turfs', |
||
| 142 | 'potato' => 'potatoes', |
||
| 143 | 'hero' => 'heroes', |
||
| 144 | 'tooth' => 'teeth', |
||
| 145 | 'goose' => 'geese', |
||
| 146 | 'foot' => 'feet', |
||
| 147 | 'foe' => 'foes', |
||
| 148 | 'sieve' => 'sieves' |
||
| 149 | ]; |
||
| 150 | |||
| 151 | protected $dictionary; |
||
| 152 | |||
| 153 | protected $cacheFile = null; |
||
| 154 | |||
| 155 | protected $toCache = false; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Inflector constructor. |
||
| 159 | */ |
||
| 160 | 19 | public function __construct() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param $directory |
||
| 166 | */ |
||
| 167 | public function setCachePath($directory) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param null|string $cacheFile |
||
| 175 | */ |
||
| 176 | public function setCacheFile($cacheFile) |
||
| 180 | |||
| 181 | public function readCache() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function isCached() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | public function hasCacheFile() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return int |
||
| 224 | */ |
||
| 225 | public function getCacheTTL() |
||
| 236 | |||
| 237 | public function __destruct() |
||
| 243 | |||
| 244 | public function writeCache() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param $word |
||
| 255 | * @return mixed |
||
| 256 | */ |
||
| 257 | 21 | public function unclassify($word) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @param $name |
||
| 264 | * @param $word |
||
| 265 | * @return mixed |
||
| 266 | */ |
||
| 267 | 47 | public function doInflection($name, $word) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param $word |
||
| 280 | * @return mixed |
||
| 281 | */ |
||
| 282 | 6 | public function singularize($word) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * @param $word |
||
| 289 | * @return mixed |
||
| 290 | */ |
||
| 291 | 22 | public function camelize($word) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * @param $word |
||
| 298 | * @return mixed |
||
| 299 | */ |
||
| 300 | 9 | public function classify($word) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param $name |
||
| 307 | * @param $arguments |
||
| 308 | * @return mixed |
||
| 309 | */ |
||
| 310 | public function __call($name, $arguments) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param $word |
||
| 319 | * @return bool|mixed |
||
| 320 | */ |
||
| 321 | 4 | View Code Duplication | protected function doPluralize($word) |
| 322 | { |
||
| 323 | 4 | $lowerCased_word = strtolower($word); |
|
| 324 | |||
| 325 | 4 | foreach ($this->uncountable as $_uncountable) { |
|
| 326 | 4 | if (substr($lowerCased_word, (-1 * strlen($_uncountable))) == $_uncountable) { |
|
| 327 | 4 | return $word; |
|
| 328 | } |
||
| 329 | } |
||
| 330 | |||
| 331 | 4 | foreach ($this->irregular as $_plural => $_singular) { |
|
| 332 | 4 | if (preg_match('/(' . $_plural . ')$/i', $word, $arr)) { |
|
| 333 | 4 | return preg_replace('/(' . $_plural . ')$/i', substr($arr[0], 0, 1) . substr($_singular, 1), $word); |
|
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | 4 | foreach ($this->plural as $rule => $replacement) { |
|
| 338 | 4 | if (preg_match($rule, $word)) { |
|
| 339 | 4 | return preg_replace($rule, $replacement, $word); |
|
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | return false; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param $word |
||
| 348 | * @return mixed |
||
| 349 | */ |
||
| 350 | 4 | View Code Duplication | protected function doSingularize($word) |
| 373 | |||
| 374 | /** |
||
| 375 | * @param $word |
||
| 376 | * @return mixed |
||
| 377 | */ |
||
| 378 | 13 | protected function doCamelize($word) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @param $word |
||
| 385 | * @return mixed |
||
| 386 | */ |
||
| 387 | protected function doHyphenize($word) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param $word |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | 16 | protected function doUnderscore($word) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Converts a class name to its table name according to rails |
||
| 406 | * naming conventions. |
||
| 407 | * |
||
| 408 | * Converts "Person" to "people" |
||
| 409 | * |
||
| 410 | * @param string $class_name Class name for getting related table_name. |
||
| 411 | * @return string plural_table_name |
||
| 412 | */ |
||
| 413 | protected function doTableize($class_name) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param $word |
||
| 420 | * @return mixed |
||
| 421 | */ |
||
| 422 | 4 | public function pluralize($word) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @param $word |
||
| 429 | * @return mixed |
||
| 430 | */ |
||
| 431 | 19 | public function underscore($word) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Converts lowercase string to underscored camelize class format |
||
| 438 | * |
||
| 439 | * @param string $string |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | 9 | protected function doClassify($string) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Reverses classify() |
||
| 452 | * |
||
| 453 | * @param string $string |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | 16 | protected function doUnclassify($string) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * @param $number |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | protected function doOrdinalize($number) |
||
| 489 | } |
||
| 490 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.