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:
1 | <?php |
||
11 | class Option |
||
12 | { |
||
13 | |||
14 | const OPTION_VISIBILITY_NOT_VISIBLE_BY_CONSUMER_OPTIONAL = 'NOT_VISIBLE_BY_CONSUMER_OPTIONAL'; |
||
15 | const OPTION_VISIBILITY_NOT_VISIBLE_BY_CONSUMER_DEFAULT = 'NOT_VISIBLE_BY_CONSUMER_DEFAULT'; |
||
16 | const OPTION_VISIBILITY_VISIBLE_BY_CONSUMER_AND_MANDATORY = 'VISIBLE_BY_CONSUMER_AND_MANDATORY'; |
||
17 | |||
18 | /** @var string */ |
||
19 | private $visibility; |
||
20 | /** @var int */ |
||
21 | private $price; |
||
22 | /** @var string */ |
||
23 | private $name; |
||
24 | /** @var Characteristic[] */ |
||
25 | private $characteristics = array(); |
||
26 | |||
27 | /** |
||
28 | * @param SimpleXMLElement $xml |
||
29 | * |
||
30 | * @return Option |
||
31 | */ |
||
32 | View Code Duplication | public static function createFromXML(SimpleXMLElement $xml) |
|
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getVisibility() |
||
61 | |||
62 | /** |
||
63 | * @param string $visibility |
||
64 | */ |
||
65 | public function setVisibility($visibility) |
||
69 | |||
70 | /** |
||
71 | * @return int |
||
72 | */ |
||
73 | public function getPrice() |
||
77 | |||
78 | /** |
||
79 | * @param int $price |
||
80 | */ |
||
81 | public function setPrice($price) |
||
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | public function getName() |
||
93 | |||
94 | /** |
||
95 | * @param string $name |
||
96 | */ |
||
97 | public function setName($name) |
||
101 | |||
102 | /** |
||
103 | * @return Characteristic[] |
||
104 | */ |
||
105 | public function getCharacteristics() |
||
109 | |||
110 | /** |
||
111 | * @param Characteristic $characteristic |
||
112 | */ |
||
113 | public function addCharacteristic(Characteristic $characteristic) |
||
117 | |||
118 | } |
||
119 |
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.