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 PoHeader 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 PoHeader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class PoHeader { |
||
29 | |||
30 | /** |
||
31 | * Language code. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $_langcode; |
||
36 | |||
37 | /** |
||
38 | * Formula for the plural form. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $_pluralForms; |
||
43 | |||
44 | /** |
||
45 | * Author(s) of the file. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $_authors; |
||
50 | |||
51 | /** |
||
52 | * Date the po file got created. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $_po_date; |
||
57 | |||
58 | /** |
||
59 | * Human readable language name. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | private $_languageName; |
||
64 | |||
65 | /** |
||
66 | * Name of the project the translation belongs to. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | private $_projectName; |
||
71 | |||
72 | /** |
||
73 | * Constructor, creates a PoHeader with default values. |
||
74 | * |
||
75 | * @param string $langcode |
||
76 | * Language code. |
||
77 | */ |
||
78 | public function __construct($langcode = NULL) { |
||
85 | |||
86 | /** |
||
87 | * Gets the plural form. |
||
88 | * |
||
89 | * @return string |
||
90 | * Plural form component from the header, for example: |
||
91 | * 'nplurals=2; plural=(n > 1);'. |
||
92 | */ |
||
93 | function getPluralForms() { |
||
96 | |||
97 | /** |
||
98 | * Set the human readable language name. |
||
99 | * |
||
100 | * @param string $languageName |
||
101 | * Human readable language name. |
||
102 | */ |
||
103 | function setLanguageName($languageName) { |
||
106 | |||
107 | /** |
||
108 | * Gets the human readable language name. |
||
109 | * |
||
110 | * @return string |
||
111 | * The human readable language name. |
||
112 | */ |
||
113 | function getLanguageName() { |
||
116 | |||
117 | /** |
||
118 | * Set the project name. |
||
119 | * |
||
120 | * @param string $projectName |
||
121 | * Human readable project name. |
||
122 | */ |
||
123 | function setProjectName($projectName) { |
||
126 | |||
127 | /** |
||
128 | * Gets the project name. |
||
129 | * |
||
130 | * @return string |
||
131 | * The human readable project name. |
||
132 | */ |
||
133 | function getProjectName() { |
||
136 | |||
137 | /** |
||
138 | * Populate internal values from a string. |
||
139 | * |
||
140 | * @param string $header |
||
141 | * Full header string with key-value pairs. |
||
142 | */ |
||
143 | public function setFromString($header) { |
||
153 | |||
154 | /** |
||
155 | * Generate a Gettext PO formatted header string based on data set earlier. |
||
156 | */ |
||
157 | public function __toString() { |
||
184 | |||
185 | /** |
||
186 | * Parses a Plural-Forms entry from a Gettext Portable Object file header. |
||
187 | * |
||
188 | * @param string $pluralforms |
||
189 | * The Plural-Forms entry value. |
||
190 | * |
||
191 | * @return |
||
192 | * An indexed array of parsed plural formula data. Containing: |
||
193 | * - 'nplurals': The number of plural forms defined by the plural formula. |
||
194 | * - 'plurals': Array of plural positions keyed by plural value. |
||
195 | * |
||
196 | * @throws Exception |
||
197 | */ |
||
198 | function parsePluralForms($pluralforms) { |
||
247 | |||
248 | /** |
||
249 | * Parses a Gettext Portable Object file header. |
||
250 | * |
||
251 | * @param string $header |
||
252 | * A string containing the complete header. |
||
253 | * |
||
254 | * @return array |
||
255 | * An associative array of key-value pairs. |
||
256 | */ |
||
257 | private function parseHeader($header) { |
||
268 | |||
269 | /** |
||
270 | * Parses and sanitizes an arithmetic formula into a plural element stack. |
||
271 | * |
||
272 | * While parsing, we ensure, that the operators have the right |
||
273 | * precedence and associativity. |
||
274 | * |
||
275 | * @param string $string |
||
276 | * A string containing the arithmetic formula. |
||
277 | * |
||
278 | * @return |
||
279 | * A stack of values and operations to be evaluated. |
||
280 | */ |
||
281 | private function parseArithmetic($string) { |
||
369 | |||
370 | /** |
||
371 | * Tokenize the formula. |
||
372 | * |
||
373 | * @param string $formula |
||
374 | * A string containing the arithmetic formula. |
||
375 | * |
||
376 | * @return array |
||
377 | * List of arithmetic tokens identified in the formula. |
||
378 | */ |
||
379 | private function tokenizeFormula($formula) { |
||
434 | |||
435 | /** |
||
436 | * Evaluate the plural element stack using a plural value. |
||
437 | * |
||
438 | * Using an element stack, which represents a plural formula, we calculate |
||
439 | * which plural string should be used for a given plural value. |
||
440 | * |
||
441 | * An example of plural formula parting and evaluation: |
||
442 | * Plural formula: 'n!=1' |
||
443 | * This formula is parsed by parseArithmetic() to a stack (array) of elements: |
||
444 | * array( |
||
445 | * 0 => '$n', |
||
446 | * 1 => '1', |
||
447 | * 2 => '!=', |
||
448 | * ); |
||
449 | * The evaluatePlural() method evaluates the $element_stack using the plural |
||
450 | * value $n. Before the actual evaluation, the '$n' in the array is replaced |
||
451 | * by the value of $n. |
||
452 | * For example: $n = 2 results in: |
||
453 | * array( |
||
454 | * 0 => '2', |
||
455 | * 1 => '1', |
||
456 | * 2 => '!=', |
||
457 | * ); |
||
458 | * The stack is processed until only one element is (the result) is left. In |
||
459 | * every iteration the top elements of the stack, up until the first operator, |
||
460 | * are evaluated. After evaluation the arguments and the operator itself are |
||
461 | * removed and replaced by the evaluation result. This is typically 2 |
||
462 | * arguments and 1 element for the operator. |
||
463 | * Because the operator is '!=' the example stack is evaluated as: |
||
464 | * $f = (int) 2 != 1; |
||
465 | * The resulting stack is: |
||
466 | * array( |
||
467 | * 0 => 1, |
||
468 | * ); |
||
469 | * With only one element left in the stack (the final result) the loop is |
||
470 | * terminated and the result is returned. |
||
471 | * |
||
472 | * @param array $element_stack |
||
473 | * Array of plural formula values and operators create by parseArithmetic(). |
||
474 | * @param integer $n |
||
475 | * The @count number for which we are determining the right plural position. |
||
476 | * |
||
477 | * @return integer |
||
478 | * Number of the plural string to be used for the given plural value. |
||
479 | * |
||
480 | * @see parseArithmetic() |
||
481 | * @throws Exception |
||
482 | */ |
||
483 | protected function evaluatePlural($element_stack, $n) { |
||
567 | } |
||
568 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.