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 |
||
20 | class International implements IBox |
||
21 | { |
||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $product; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $options; |
||
31 | |||
32 | /** |
||
33 | * @var \Bpost\BpostApiClient\Bpost\Order\Receiver |
||
34 | */ |
||
35 | private $receiver; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | private $parcelWeight; |
||
41 | |||
42 | /** |
||
43 | * @var \Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo\CustomsInfo |
||
44 | */ |
||
45 | private $customsInfo; |
||
46 | |||
47 | /** |
||
48 | * @param \Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo\CustomsInfo $customsInfo |
||
49 | */ |
||
50 | 1 | public function setCustomsInfo($customsInfo) |
|
51 | { |
||
52 | 1 | $this->customsInfo = $customsInfo; |
|
53 | 1 | } |
|
54 | |||
55 | /** |
||
56 | * @return \Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo\CustomsInfo |
||
57 | */ |
||
58 | 2 | public function getCustomsInfo() |
|
59 | { |
||
60 | 2 | return $this->customsInfo; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param Option[] $options |
||
65 | */ |
||
66 | 1 | public function setOptions($options) |
|
67 | { |
||
68 | 1 | $this->options = $options; |
|
69 | 1 | } |
|
70 | |||
71 | /** |
||
72 | * @return Option[] |
||
73 | */ |
||
74 | 2 | public function getOptions() |
|
75 | { |
||
76 | 2 | return $this->options; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param Option $option |
||
81 | */ |
||
82 | 1 | public function addOption(Option $option) |
|
83 | { |
||
84 | 1 | $this->options[] = $option; |
|
85 | 1 | } |
|
86 | |||
87 | /** |
||
88 | * @param int $parcelWeight |
||
89 | */ |
||
90 | 1 | public function setParcelWeight($parcelWeight) |
|
91 | { |
||
92 | 1 | $this->parcelWeight = $parcelWeight; |
|
93 | 1 | } |
|
94 | |||
95 | /** |
||
96 | * @return int |
||
97 | */ |
||
98 | 2 | public function getParcelWeight() |
|
99 | { |
||
100 | 2 | return $this->parcelWeight; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param string $product |
||
105 | * @throws BpostInvalidValueException |
||
106 | */ |
||
107 | 3 | public function setProduct($product) |
|
108 | { |
||
109 | 3 | if (!in_array($product, self::getPossibleProductValues())) { |
|
110 | 1 | throw new BpostInvalidValueException('product', $product, self::getPossibleProductValues()); |
|
111 | } |
||
112 | |||
113 | 2 | $this->product = $product; |
|
114 | 2 | } |
|
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | 2 | public function getProduct() |
|
120 | { |
||
121 | 2 | return $this->product; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return array |
||
126 | */ |
||
127 | 3 | public static function getPossibleProductValues() |
|
136 | |||
137 | /** |
||
138 | * @param \Bpost\BpostApiClient\Bpost\Order\Receiver $receiver |
||
139 | */ |
||
140 | 2 | public function setReceiver($receiver) |
|
141 | { |
||
142 | 2 | $this->receiver = $receiver; |
|
143 | 2 | } |
|
144 | |||
145 | /** |
||
146 | * @return \Bpost\BpostApiClient\Bpost\Order\Receiver |
||
147 | */ |
||
148 | 2 | public function getReceiver() |
|
152 | |||
153 | /** |
||
154 | * Return the object as an array for usage in the XML |
||
155 | * |
||
156 | * @param \DomDocument $document |
||
157 | * @param string $prefix |
||
|
|||
158 | * @return \DomElement |
||
159 | */ |
||
160 | 2 | public function toXML(\DOMDocument $document, $prefix = null) |
|
214 | |||
215 | /** |
||
216 | * @param \SimpleXMLElement $xml |
||
217 | * |
||
218 | * @return International |
||
219 | * @throws BpostInvalidValueException |
||
220 | * @throws BpostNotImplementedException |
||
221 | */ |
||
222 | public static function createFromXML(\SimpleXMLElement $xml) |
||
273 | } |
||
274 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.