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 PMF_Language_Plurals 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 PMF_Language_Plurals, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class PMF_Language_Plurals |
||
36 | { |
||
37 | /** |
||
38 | * The currently loaded PMF translations |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $PMF_TRANSL = []; |
||
43 | |||
44 | /** |
||
45 | * The number of plural forms for current language $lang |
||
46 | * |
||
47 | * @var integer |
||
48 | */ |
||
49 | private $nPlurals; |
||
50 | |||
51 | /** |
||
52 | * The language code of current language |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $lang; |
||
57 | |||
58 | /** |
||
59 | * True when there is no support for plural forms in current language $lang |
||
60 | * |
||
61 | * @var boolean |
||
62 | */ |
||
63 | private $useDefaultPluralForm; |
||
64 | |||
65 | /** |
||
66 | * Constructor |
||
67 | * |
||
68 | * @param array $translation PMF translation array for current language |
||
69 | * @return void |
||
70 | */ |
||
71 | public function __construct($translation) |
||
86 | |||
87 | /** |
||
88 | * Determines the correct plural form for integer $n |
||
89 | * Returned integer is from interval [0, $nPlurals) |
||
90 | * |
||
91 | * @param integer $n The number used to determine the plural form |
||
92 | * @return integer |
||
93 | * @access private |
||
94 | */ |
||
95 | private function _getPlural($n) |
||
110 | |||
111 | /** |
||
112 | * Helper function that returns the translation template in the correct plural form |
||
113 | * If translation is missing, message in English plural form is returned |
||
114 | * |
||
115 | * @param string $msgID Message identificator |
||
116 | * @param integer $n The number used to determine the plural form |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getMsgTemplate($msgID, $n) |
||
130 | |||
131 | /** |
||
132 | * Returns a translated string in the correct plural form, |
||
133 | * produced according to the formatting of the message. |
||
134 | * |
||
135 | * @param string $msgID Message identificator |
||
136 | * @param integer $n The number used to determine the plural form |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getMsg($msgID, $n) |
||
143 | |||
144 | /** |
||
145 | * Returns the plural form for language $lang or -1 if language $lang is not supported |
||
146 | * |
||
147 | * @param string $lang The language code |
||
148 | * @param integer $n The number used to determine the plural form |
||
149 | * @return integer |
||
150 | * @link http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms |
||
151 | */ |
||
152 | private function plural($lang, $n) |
||
238 | |||
239 | } |
||
240 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.