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 At247 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 At247, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class At247 extends National |
||
20 | { |
||
21 | /**@var string */ |
||
22 | private $parcelsDepotId; |
||
23 | |||
24 | /** @var string */ |
||
25 | private $parcelsDepotName; |
||
26 | |||
27 | /** @var \Bpost\BpostApiClient\Bpost\Order\ParcelsDepotAddress */ |
||
28 | private $parcelsDepotAddress; |
||
29 | |||
30 | /** @var string */ |
||
31 | protected $product = Product::PRODUCT_NAME_BPACK_24H_PRO; |
||
32 | |||
33 | /** @var string */ |
||
34 | private $memberId; |
||
35 | |||
36 | /** @var UnregisteredParcelLockerMember */ |
||
37 | private $unregisteredParcelLockerMember; |
||
38 | |||
39 | /** @var string */ |
||
40 | private $receiverName; |
||
41 | |||
42 | /** @var string */ |
||
43 | private $receiverCompany; |
||
44 | |||
45 | /** @var string */ |
||
46 | protected $requestedDeliveryDate; |
||
47 | |||
48 | /** |
||
49 | * @param string $memberId |
||
50 | */ |
||
51 | 1 | public function setMemberId($memberId) |
|
55 | |||
56 | /** |
||
57 | * @return string |
||
58 | */ |
||
59 | 1 | public function getMemberId() |
|
63 | |||
64 | /** |
||
65 | * @param \Bpost\BpostApiClient\Bpost\Order\ParcelsDepotAddress $parcelsDepotAddress |
||
66 | */ |
||
67 | 1 | public function setParcelsDepotAddress($parcelsDepotAddress) |
|
71 | |||
72 | /** |
||
73 | * @return \Bpost\BpostApiClient\Bpost\Order\ParcelsDepotAddress |
||
74 | */ |
||
75 | 1 | public function getParcelsDepotAddress() |
|
79 | |||
80 | /** |
||
81 | * @param string $parcelsDepotId |
||
82 | */ |
||
83 | 1 | public function setParcelsDepotId($parcelsDepotId) |
|
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | 1 | public function getParcelsDepotId() |
|
95 | |||
96 | /** |
||
97 | * @param string $parcelsDepotName |
||
98 | */ |
||
99 | 1 | public function setParcelsDepotName($parcelsDepotName) |
|
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | */ |
||
107 | 1 | public function getParcelsDepotName() |
|
111 | |||
112 | /** |
||
113 | * @return UnregisteredParcelLockerMember |
||
114 | */ |
||
115 | 1 | public function getUnregisteredParcelLockerMember() |
|
119 | |||
120 | /** |
||
121 | * @param UnregisteredParcelLockerMember $unregisteredParcelLockerMember |
||
122 | */ |
||
123 | 1 | public function setUnregisteredParcelLockerMember(UnregisteredParcelLockerMember $unregisteredParcelLockerMember) |
|
127 | |||
128 | /** |
||
129 | * @param string $product Possible values are: bpack 24h Pro |
||
130 | * |
||
131 | * @throws BpostInvalidValueException |
||
132 | 2 | */ |
|
133 | public function setProduct($product) |
||
141 | |||
142 | /** |
||
143 | * @return array |
||
144 | 2 | */ |
|
145 | public static function getPossibleProductValues() |
||
152 | |||
153 | /** |
||
154 | 1 | * @param string $receiverCompany |
|
155 | */ |
||
156 | 1 | public function setReceiverCompany($receiverCompany) |
|
160 | |||
161 | /** |
||
162 | 1 | * @return string |
|
163 | */ |
||
164 | 1 | public function getReceiverCompany() |
|
168 | |||
169 | /** |
||
170 | 1 | * @param string $receiverName |
|
171 | */ |
||
172 | 1 | public function setReceiverName($receiverName) |
|
176 | |||
177 | /** |
||
178 | 1 | * @return string |
|
179 | */ |
||
180 | 1 | public function getReceiverName() |
|
184 | |||
185 | /** |
||
186 | 1 | * @return string |
|
187 | */ |
||
188 | 1 | public function getRequestedDeliveryDate() |
|
192 | |||
193 | /** |
||
194 | 1 | * @param string $requestedDeliveryDate |
|
195 | */ |
||
196 | 1 | public function setRequestedDeliveryDate($requestedDeliveryDate) |
|
200 | |||
201 | /** |
||
202 | * Return the object as an array for usage in the XML |
||
203 | * |
||
204 | * @param \DomDocument $document |
||
205 | * @param string $prefix |
||
|
|||
206 | * @param string $type |
||
207 | 1 | * |
|
208 | * @return \DomElement |
||
209 | 1 | */ |
|
210 | 1 | public function toXML(\DOMDocument $document, $prefix = null, $type = null) |
|
267 | |||
268 | /** |
||
269 | * @param \DOMDocument $document |
||
270 | 1 | * @param \DOMElement $typeElement |
|
271 | * @param string $prefix |
||
272 | 1 | */ |
|
273 | 1 | protected function addToXmlRequestedDeliveryDate(\DOMDocument $document, \DOMElement $typeElement, $prefix) |
|
284 | |||
285 | /** |
||
286 | * @param \DOMDocument $document |
||
287 | 1 | * @param \DOMElement $typeElement |
|
288 | * @param string $prefix |
||
289 | 1 | */ |
|
290 | 1 | protected function addToXmlUnregisteredParcelLockerMember(\DOMDocument $document, \DOMElement $typeElement, $prefix) |
|
298 | |||
299 | /** |
||
300 | * @param \SimpleXMLElement $xml |
||
301 | * |
||
302 | * @return At247 |
||
303 | * @throws BpostInvalidValueException |
||
304 | * @throws BpostNotImplementedException |
||
305 | */ |
||
306 | public static function createFromXML(\SimpleXMLElement $xml) |
||
383 | } |
||
384 |
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.