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 Attribute 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 Attribute, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Attribute |
||
9 | { |
||
10 | const BOOL = 'ci_boo'; // boolean |
||
11 | const CI_ENUM = 'ci_enu'; // case-insensitive enumeration |
||
12 | const CI_SSENUM = 'ci_sse'; // case-insensitive space-separated enumeration |
||
13 | const INT = 'ci_int'; // integer |
||
14 | const JS = 'cs_jsc'; // javascript |
||
15 | const CI_STRING = 'ci_str'; // case-insensitive string |
||
16 | const CS_STRING = 'cs_str'; // case-sensitive string |
||
17 | const URI = 'cs_uri'; // uri |
||
18 | |||
19 | /** @var string */ |
||
20 | private $name; |
||
21 | |||
22 | /** @var string */ |
||
23 | private $type; |
||
24 | |||
25 | /** @var null|mixed */ |
||
26 | private $value; |
||
27 | |||
28 | /** @var bool */ |
||
29 | private $isStandard; |
||
30 | |||
31 | /** |
||
32 | * Constructor |
||
33 | */ |
||
34 | 57 | public function __construct($name, $value) |
|
41 | |||
42 | /** |
||
43 | * Getter for 'name'. |
||
44 | */ |
||
45 | 50 | public function getName() |
|
49 | |||
50 | /** |
||
51 | * Getter for 'type'. |
||
52 | */ |
||
53 | public function getType() |
||
57 | |||
58 | /** |
||
59 | * Chainable setter for 'type'. |
||
60 | */ |
||
61 | 49 | public function setType($type) |
|
79 | |||
80 | /** |
||
81 | * Getter for 'value'. |
||
82 | */ |
||
83 | 4 | public function getValue() |
|
87 | |||
88 | /** |
||
89 | * Getter for 'isStandard'. |
||
90 | */ |
||
91 | public function getIsStandard() |
||
95 | |||
96 | /** |
||
97 | * Chainable setter for 'isStandard'. |
||
98 | */ |
||
99 | 49 | public function setIsStandard($isStandard) |
|
105 | |||
106 | 49 | public function clean(Configuration $configuration, Element $element, LoggerInterface $logger) |
|
177 | |||
178 | 1 | private function cleanAttributeBoolean(Configuration $configuration, Element $element, LoggerInterface $logger) |
|
187 | |||
188 | 4 | private function cleanAttributeInteger(Configuration $configuration, Element $element, LoggerInterface $logger) |
|
216 | |||
217 | 14 | private function cleanAttributeUri(Configuration $configuration, Element $element, LoggerInterface $logger) |
|
223 | |||
224 | 51 | public function __toString() |
|
234 | } |
||
235 |
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.