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 namespace SimpleUPS\Track\SmallPackage; |
||
8 | class Message extends \SimpleUPS\Model |
||
9 | { |
||
10 | |||
11 | private |
||
12 | $CODE_ON_TIME = '01', |
||
13 | $CODE_RESCHEDULED = '02', |
||
14 | $CODE_RETURNED_TO_SHIPPER = '03'; |
||
15 | private |
||
16 | /* @var string $code */ |
||
17 | $code, |
||
18 | |||
19 | /* @var string $description */ |
||
20 | $description; |
||
21 | |||
22 | /** |
||
23 | * @internal |
||
24 | * |
||
25 | * @param string $code |
||
26 | * |
||
27 | * @return Message |
||
28 | */ |
||
29 | public function setCode($code) |
||
34 | |||
35 | /** |
||
36 | * @return string |
||
37 | */ |
||
38 | public function getCode() |
||
42 | |||
43 | /** |
||
44 | * @internal |
||
45 | * |
||
46 | * @param string $description |
||
47 | * |
||
48 | * @return Message |
||
49 | */ |
||
50 | public function setDescription($description) |
||
55 | |||
56 | /** |
||
57 | * @return string |
||
58 | */ |
||
59 | public function getDescription() |
||
63 | |||
64 | /** |
||
65 | * @return bool |
||
66 | */ |
||
67 | public function isOnTime() |
||
71 | |||
72 | /** |
||
73 | * @return bool |
||
74 | */ |
||
75 | public function isRescheduled() |
||
79 | |||
80 | /** |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function isReturnedToShipper() |
||
87 | |||
88 | /** |
||
89 | * @internal |
||
90 | * |
||
91 | * @param \SimpleXMLElement $xml |
||
92 | * |
||
93 | * @return Message |
||
94 | */ |
||
95 | View Code Duplication | public static function fromXml(\SimpleXMLElement $xml) |
|
110 | } |
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.