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 |
||
20 | class Inflector |
||
21 | { |
||
22 | /** |
||
23 | * @var array List of uncountable words |
||
24 | */ |
||
25 | protected $uncountableWords = array( |
||
26 | 'equipment', 'information', 'rice', 'money', |
||
27 | 'species', 'series', 'fish', 'meta' |
||
28 | ); |
||
29 | |||
30 | /** |
||
31 | * @var array Regex rules for pluralisation |
||
32 | */ |
||
33 | protected $pluralRules = array( |
||
34 | '/^(ox)$/i' => '\1\2en', // ox |
||
35 | '/([m|l])ouse$/i' => '\1ice', // mouse, louse |
||
36 | '/(matr|vert|ind)ix|ex$/i' => '\1ices', // matrix, vertex, index |
||
37 | '/(x|ch|ss|sh)$/i' => '\1es', // search, switch, fix, box, process, address |
||
38 | '/([^aeiouy]|qu)y$/i' => '\1ies', // query, ability, agency |
||
39 | '/(hive)$/i' => '\1s', // archive, hive |
||
40 | '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', // half, safe, wife |
||
41 | '/sis$/i' => 'ses', // basis, diagnosis |
||
42 | '/([ti])um$/i' => '\1a', // datum, medium |
||
43 | '/(p)erson$/i' => '\1eople', // person, salesperson |
||
44 | '/(m)an$/i' => '\1en', // man, woman, spokesman |
||
45 | '/(c)hild$/i' => '\1hildren', // child |
||
46 | '/(buffal|tomat)o$/i' => '\1\2oes', // buffalo, tomato |
||
47 | '/(bu|campu)s$/i' => '\1\2ses', // bus, campus |
||
48 | '/(alias|status|virus)$/i' => '\1es', // alias |
||
49 | '/(octop)us$/i' => '\1i', // octopus |
||
50 | '/(ax|cris|test)is$/i' => '\1es', // axis, crisis |
||
51 | '/s$/' => 's', // no change (compatibility) |
||
52 | '/$/' => 's', |
||
53 | ); |
||
54 | |||
55 | /** |
||
56 | * @var array Regex rules for singularisation |
||
57 | */ |
||
58 | protected $singularRules = array( |
||
59 | '/(matr)ices$/i' => '\1ix', |
||
60 | '/(vert|ind)ices$/i' => '\1ex', |
||
61 | '/^(ox)en/i' => '\1', |
||
62 | '/(alias)es$/i' => '\1', |
||
63 | '/([octop|vir])i$/i' => '\1us', |
||
64 | '/(cris|ax|test)es$/i' => '\1is', |
||
65 | '/(shoe)s$/i' => '\1', |
||
66 | '/(o)es$/i' => '\1', |
||
67 | '/(bus|campus)es$/i' => '\1', |
||
68 | '/([m|l])ice$/i' => '\1ouse', |
||
69 | '/(x|ch|ss|sh)es$/i' => '\1', |
||
70 | '/(m)ovies$/i' => '\1\2ovie', |
||
71 | '/(s)eries$/i' => '\1\2eries', |
||
72 | '/([^aeiouy]|qu)ies$/i' => '\1y', |
||
73 | '/([lr])ves$/i' => '\1f', |
||
74 | '/(tive)s$/i' => '\1', |
||
75 | '/(hive)s$/i' => '\1', |
||
76 | '/([^f])ves$/i' => '\1fe', |
||
77 | '/(^analy)ses$/i' => '\1sis', |
||
78 | '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', |
||
79 | '/([ti])a$/i' => '\1um', |
||
80 | '/(p)eople$/i' => '\1\2erson', |
||
81 | '/(m)en$/i' => '\1an', |
||
82 | '/(s)tatuses$/i' => '\1\2tatus', |
||
83 | '/(c)hildren$/i' => '\1\2hild', |
||
84 | '/(n)ews$/i' => '\1\2ews', |
||
85 | '/([^us])s$/i' => '\1', |
||
86 | ); |
||
87 | |||
88 | /** |
||
89 | * @var Fuel\Config\Config Instance of a Fuel config container |
||
90 | */ |
||
91 | protected $config; |
||
92 | |||
93 | /** |
||
94 | * @var Fuel\Security\Manager Instance of a Fuel security manager |
||
95 | */ |
||
96 | protected $security; |
||
97 | |||
98 | /** |
||
99 | * @var Fuel\Common\Str Instance of a Fuel string helper |
||
100 | */ |
||
101 | protected $str; |
||
102 | |||
103 | /** |
||
104 | * Class constructor |
||
105 | * |
||
106 | * @param Fuel\Config\Config $config Instance of a Fuel config container |
||
107 | * @param Fuel\Security\Manager $security Instance of a Fuel security manager |
||
108 | * |
||
109 | * @return void |
||
|
|||
110 | * |
||
111 | * @since 2.0.0 |
||
112 | */ |
||
113 | public function __construct($config = null, $security = null, $str = null) |
||
119 | |||
120 | /** |
||
121 | * Add order suffix to numbers ex. 1st 2nd 3rd 4th 5th |
||
122 | * |
||
123 | * @param int the number to ordinalize |
||
124 | * @return string the ordinalized version of $number |
||
125 | * @link http://snipplr.com/view/4627/a-function-to-add-a-prefix-to-numbers-ex-1st-2nd-3rd-4th-5th/ |
||
126 | */ |
||
127 | public function ordinalize($number) |
||
157 | |||
158 | /** |
||
159 | * Gets the plural version of the given word |
||
160 | * |
||
161 | * @param string the word to pluralize |
||
162 | * @param int number of instances |
||
163 | * @return string the plural version of $word |
||
164 | */ |
||
165 | public function pluralize($word, $count = 0) |
||
192 | |||
193 | /** |
||
194 | * Gets the singular version of the given word |
||
195 | * |
||
196 | * @param string the word to singularize |
||
197 | * @return string the singular version of $word |
||
198 | */ |
||
199 | public function singularize($word) |
||
219 | |||
220 | /** |
||
221 | * Takes a string that has words seperated by underscores and turns it into |
||
222 | * a CamelCased string. |
||
223 | * |
||
224 | * @param string the underscored word |
||
225 | * @return string the CamelCased version of $underscored_word |
||
226 | */ |
||
227 | public function camelize($underscored_word) |
||
238 | |||
239 | /** |
||
240 | * Takes a CamelCased string and returns an underscore separated version. |
||
241 | * |
||
242 | * @param string the CamelCased word |
||
243 | * @return string an underscore separated version of $camel_cased_word |
||
244 | */ |
||
245 | public function underscore($camel_cased_word) |
||
249 | |||
250 | /** |
||
251 | * Translate string to 7-bit ASCII |
||
252 | * Only works with UTF-8. |
||
253 | * |
||
254 | * @param string $str string to translate |
||
255 | * @param bool $allow_non_ascii wether to remove non ascii |
||
256 | * @return string translated string |
||
257 | */ |
||
258 | public function ascii($str, $allow_non_ascii = false) |
||
273 | |||
274 | /** |
||
275 | * Converts your text to a URL-friendly title so it can be used in the URL. |
||
276 | * Only works with UTF8 input and and only outputs 7 bit ASCII characters. |
||
277 | * |
||
278 | * @param string $str the text |
||
279 | * @param string $sep the separator (either - or _) |
||
280 | * @param bool $lowercase wether to convert to lowercase |
||
281 | * @param bool $allow_non_ascii wether to allow non ascii |
||
282 | * @return string the new title |
||
283 | */ |
||
284 | public function friendlyTitle($str, $sep = '-', $lowercase = false, $allow_non_ascii = false) |
||
325 | |||
326 | /** |
||
327 | * Turns an underscore or dash separated word and turns it into a human looking string. |
||
328 | * |
||
329 | * @param string the word |
||
330 | * @param string the separator (either _ or -) |
||
331 | * @param bool lowercare string and upper case first |
||
332 | * @return string the human version of given string |
||
333 | */ |
||
334 | public function humanize($str, $sep = '_', $lowercase = true) |
||
346 | |||
347 | /** |
||
348 | * Takes the class name out of a modulized string. |
||
349 | * |
||
350 | * @param string the modulized class |
||
351 | * @return string the string without the class name |
||
352 | */ |
||
353 | public function demodulize($class_name_in_module) |
||
357 | |||
358 | /** |
||
359 | * Takes the namespace off the given class name. |
||
360 | * |
||
361 | * @param string the class name |
||
362 | * @return string the string without the namespace |
||
363 | */ |
||
364 | public function denamespace($class_name) |
||
373 | |||
374 | /** |
||
375 | * Returns the namespace of the given class name. |
||
376 | * |
||
377 | * @param string $class_name the class name |
||
378 | * @return string the string without the namespace |
||
379 | */ |
||
380 | public function getNamespace($class_name) |
||
389 | |||
390 | /** |
||
391 | * Takes a class name and determines the table name. The table name is a |
||
392 | * pluralized version of the class name. |
||
393 | * |
||
394 | * @param string the table name |
||
395 | * @return string the table name |
||
396 | */ |
||
397 | View Code Duplication | public function tableize($class_name) |
|
406 | |||
407 | /** |
||
408 | * Takes an underscored classname and uppercases all letters after the underscores. |
||
409 | * |
||
410 | * @param string classname |
||
411 | * @param string separator |
||
412 | * @return string |
||
413 | */ |
||
414 | public function wordsToUpper($class, $sep = '_') |
||
418 | |||
419 | /** |
||
420 | * Takes a table name and creates the class name. |
||
421 | * |
||
422 | * @param string the table name |
||
423 | * @param bool whether to singularize the table name or not |
||
424 | * @return string the class name |
||
425 | */ |
||
426 | public function classify($name, $force_singular = true) |
||
431 | |||
432 | /** |
||
433 | * Gets the foreign key for a given class. |
||
434 | * |
||
435 | * @param string the class name |
||
436 | * @param bool $use_underscore whether to use an underscore or not |
||
437 | * @return string the foreign key |
||
438 | */ |
||
439 | View Code Duplication | public function foreignKey($class_name, $use_underscore = true) |
|
448 | |||
449 | /** |
||
450 | * Checks if the given word has a plural version. |
||
451 | * |
||
452 | * @param string the word to check |
||
453 | * @return bool if the word is countable |
||
454 | */ |
||
455 | public function isCountable($word) |
||
459 | } |
||
460 | |||
461 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.