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 DeliveryMethod |
||
12 | { |
||
13 | const DELIVERY_METHOD_NAME_HOME_OR_OFFICE = 'home or office'; |
||
14 | const DELIVERY_METHOD_NAME_PICKUP_POINT = 'pick-up point'; |
||
15 | const DELIVERY_METHOD_NAME_PARCEL_LOCKER = 'parcel locker'; |
||
16 | const DELIVERY_METHOD_NAME_CLICK_AND_COLLECT = 'Click & Collect'; |
||
17 | |||
18 | const DELIVERY_METHOD_VISIBILITY_VISIBLE = 'VISIBLE'; |
||
19 | const DELIVERY_METHOD_VISIBILITY_GREYED_OUT = 'GREYED_OUT'; |
||
20 | const DELIVERY_METHOD_VISIBILITY_INVISIBLE = 'INVISIBLE'; |
||
21 | |||
22 | /** @var string */ |
||
23 | private $name; |
||
24 | /** @var string */ |
||
25 | private $visibility; |
||
26 | /** @var Product[] */ |
||
27 | private $products = array(); |
||
28 | |||
29 | /** |
||
30 | * @param SimpleXMLElement $xml |
||
31 | * |
||
32 | * @return DeliveryMethod |
||
33 | */ |
||
34 | View Code Duplication | public static function createFromXML(SimpleXMLElement $xml) |
|
54 | |||
55 | /** |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getName() |
||
62 | |||
63 | /** |
||
64 | * @param string $name |
||
65 | */ |
||
66 | public function setName($name) |
||
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | * @see Constants self::VISIBLITY_* |
||
74 | */ |
||
75 | public function getVisibility() |
||
79 | |||
80 | /** |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function isVisibleAndActive() |
||
87 | |||
88 | /** |
||
89 | * @param string $visibility |
||
90 | * |
||
91 | * @see Constants self::VISIBLITY_* |
||
92 | */ |
||
93 | public function setVisibility($visibility) |
||
97 | |||
98 | /** |
||
99 | * @return Product[] |
||
100 | */ |
||
101 | public function getProducts() |
||
105 | |||
106 | /** |
||
107 | * @param Product $product |
||
108 | */ |
||
109 | public function addProduct(Product $product) |
||
113 | |||
114 | |||
115 | } |
||
116 |
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.