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 Label 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 Label, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Label implements RenderingInterface |
||
24 | { |
||
25 | |||
26 | use AffixesTrait, |
||
27 | FormattingTrait, |
||
28 | TextCaseTrait; |
||
29 | |||
30 | private $variable; |
||
31 | |||
32 | /** |
||
33 | * Selects the form of the term, with allowed values: |
||
34 | * |
||
35 | * - “long” - (default), e.g. “page”/”pages” for the “page” term |
||
36 | * - “short” - e.g. “p.”/”pp.” for the “page” term |
||
37 | * - “symbol” - e.g. “§”/”§§” for the “section” term |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | private $form = ""; |
||
42 | |||
43 | /** |
||
44 | * Sets pluralization of the term, with allowed values: |
||
45 | * |
||
46 | * - “contextual” - (default), the term plurality matches that of the variable content. Content is considered |
||
47 | * plural when it contains multiple numbers (e.g. “page 1”, “pages 1-3”, “volume 2”, “volumes 2 & 4”), or, in |
||
48 | * the case of the “number-of-pages” and “number-of-volumes” variables, when the number is higher than 1 |
||
49 | * (“1 volume” and “3 volumes”). |
||
50 | * - “always” - always use the plural form, e.g. “pages 1” and “pages 1-3” |
||
51 | * - “never” - always use the singular form, e.g. “page 1” and “page 1-3” |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $plural = "contextual"; |
||
56 | |||
57 | /** |
||
58 | * Label constructor. |
||
59 | * @param \SimpleXMLElement $node |
||
60 | */ |
||
61 | public function __construct(\SimpleXMLElement $node) |
||
82 | |||
83 | /** |
||
84 | * @param \stdClass $data |
||
85 | * @param int|null $citationNumber |
||
86 | * @return string |
||
87 | */ |
||
88 | public function render($data, $citationNumber = null) |
||
141 | |||
142 | |||
143 | private function evaluateStringPluralism($data, $variable) |
||
167 | |||
168 | /** |
||
169 | * @param string $variable |
||
170 | */ |
||
171 | public function setVariable($variable) |
||
175 | |||
176 | /** |
||
177 | * @param $data |
||
178 | * @param $plural |
||
179 | * @param $variable |
||
180 | * @return string |
||
181 | */ |
||
182 | protected function getPlural($data, $plural, $variable) |
||
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | public function getForm() |
||
216 | |||
217 | /** |
||
218 | * @param string $form |
||
219 | */ |
||
220 | public function setForm($form) |
||
224 | |||
225 | |||
226 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.