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 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 Plurals, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Plurals |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * The currently loaded PMF translations. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $PMF_TRANSL = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The number of plural forms for current language $lang. |
||
| 47 | * |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | private $nPlurals; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The language code of current language. |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $lang; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * True when there is no support for plural forms in current language $lang. |
||
| 61 | * |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | private $useDefaultPluralForm; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Constructor. |
||
| 68 | * |
||
| 69 | * @param array $translation PMF translation array for current language |
||
| 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 int $n The number used to determine the plural form |
||
| 92 | * |
||
| 93 | * @return int |
||
| 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 int $n The number used to determine the plural form |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getMsgTemplate($msgID, $n) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns a translated string in the correct plural form, |
||
| 134 | * produced according to the formatting of the message. |
||
| 135 | * |
||
| 136 | * @param string $msgID Message identificator |
||
| 137 | * @param int $n The number used to determine the plural form |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function getMsg($msgID, $n) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the plural form for language $lang or -1 if language $lang is not supported. |
||
| 148 | * |
||
| 149 | * @param string $lang The language code |
||
| 150 | * @param int $n The number used to determine the plural form |
||
| 151 | * |
||
| 152 | * @return int |
||
| 153 | * |
||
| 154 | * @link http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms |
||
| 155 | */ |
||
| 156 | private function plural($lang, $n) |
||
| 242 | } |
||
| 243 |