Complex classes like ShipmentServiceOptions 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 ShipmentServiceOptions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ShipmentServiceOptions implements NodeInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var |
||
18 | */ |
||
19 | public $SaturdayPickup; |
||
20 | |||
21 | /** |
||
22 | * @var |
||
23 | */ |
||
24 | public $SaturdayDelivery; |
||
25 | |||
26 | /** |
||
27 | * @var |
||
28 | */ |
||
29 | public $COD; |
||
30 | |||
31 | /** |
||
32 | * @var CallTagARS |
||
33 | */ |
||
34 | public $CallTagARS; |
||
35 | |||
36 | /** |
||
37 | * @var |
||
38 | */ |
||
39 | public $NegotiatedRatesIndicator; |
||
40 | |||
41 | /** |
||
42 | * @var |
||
43 | */ |
||
44 | public $DirectDeliveryOnlyIndicator; |
||
45 | |||
46 | /** |
||
47 | * @var |
||
48 | */ |
||
49 | public $DeliverToAddresseeOnlyIndicator; |
||
50 | |||
51 | /** |
||
52 | * @var |
||
53 | */ |
||
54 | private $internationalForms; |
||
55 | |||
56 | /** |
||
57 | * @var null|LabelMethod |
||
58 | */ |
||
59 | private $labelMethod; |
||
60 | |||
61 | /** |
||
62 | * @var null|LabelDelivery |
||
63 | */ |
||
64 | private $labelDelivery; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | private $notifications = []; |
||
70 | |||
71 | /** |
||
72 | * @var AccessPointCOD |
||
73 | */ |
||
74 | private $accessPointCOD; |
||
75 | |||
76 | /** |
||
77 | * @var boolean |
||
78 | */ |
||
79 | private $importControlIndicator; |
||
80 | |||
81 | /** |
||
82 | * @var DeliveryConfirmation |
||
83 | */ |
||
84 | private $deliveryConfirmation; |
||
85 | |||
86 | /** |
||
87 | * @param null $response |
||
88 | */ |
||
89 | 9 | public function __construct($response = null) |
|
135 | |||
136 | /** |
||
137 | * @param null|DOMDocument $document |
||
138 | * |
||
139 | * @return DOMElement |
||
140 | */ |
||
141 | 2 | public function toNode(DOMDocument $document = null) |
|
213 | |||
214 | /** |
||
215 | * @return AccessPointCOD |
||
216 | */ |
||
217 | 2 | public function getAccessPointCOD() |
|
221 | |||
222 | /** |
||
223 | * @param AccessPointCOD $accessPointCOD |
||
224 | * @return $this |
||
225 | */ |
||
226 | public function setAccessPointCOD($accessPointCOD) |
||
231 | |||
232 | /** |
||
233 | * @param InternationalForms $data |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function setInternationalForms(InternationalForms $data) |
||
241 | |||
242 | /** |
||
243 | * @return InternationalForms |
||
244 | */ |
||
245 | public function getInternationalForms() |
||
249 | |||
250 | /** |
||
251 | * @param LabelMethod $data |
||
252 | * @return $this |
||
253 | */ |
||
254 | 2 | public function setLabelMethod(LabelMethod $data) |
|
259 | |||
260 | /** |
||
261 | * @return null|LabelMethod |
||
262 | */ |
||
263 | 1 | public function getLabelMethod() |
|
267 | |||
268 | /** |
||
269 | * @param LabelDelivery $data |
||
270 | * @return $this |
||
271 | */ |
||
272 | public function setLabelDelivery(LabelDelivery $data) |
||
277 | |||
278 | /** |
||
279 | * @return null|LabelDelivery |
||
280 | */ |
||
281 | public function getLabelDelivery() |
||
285 | |||
286 | /** |
||
287 | * @param Notification $notification |
||
288 | * |
||
289 | * @throws \Exception |
||
290 | * |
||
291 | * @return $this |
||
292 | */ |
||
293 | public function addNotification(Notification $notification) |
||
303 | |||
304 | /** |
||
305 | * @return array |
||
306 | */ |
||
307 | public function getNotifications() |
||
311 | |||
312 | /** |
||
313 | * @return mixed |
||
314 | */ |
||
315 | public function getSaturdayPickup() |
||
319 | |||
320 | /** |
||
321 | * @param mixed $SaturdayPickup |
||
322 | * @return ShipmentServiceOptions |
||
323 | */ |
||
324 | public function setSaturdayPickup($SaturdayPickup) |
||
329 | |||
330 | /** |
||
331 | * @return mixed |
||
332 | */ |
||
333 | public function getSaturdayDelivery() |
||
337 | |||
338 | /** |
||
339 | * @param mixed $SaturdayDelivery |
||
340 | * @return ShipmentServiceOptions |
||
341 | */ |
||
342 | public function setSaturdayDelivery($SaturdayDelivery) |
||
347 | |||
348 | /** |
||
349 | * @return mixed |
||
350 | */ |
||
351 | 2 | public function getCOD() |
|
355 | |||
356 | /** |
||
357 | * @param mixed $COD |
||
358 | * @return ShipmentServiceOptions |
||
359 | */ |
||
360 | public function setCOD($COD) |
||
365 | |||
366 | |||
367 | /** |
||
368 | * @return CallTagARS |
||
369 | */ |
||
370 | public function getCallTagARS() |
||
374 | |||
375 | /** |
||
376 | * @param CallTagARS $CallTagARS |
||
377 | * @return ShipmentServiceOptions |
||
378 | */ |
||
379 | public function setCallTagARS($CallTagARS) |
||
384 | |||
385 | /** |
||
386 | * @return boolean |
||
387 | */ |
||
388 | public function isNegotiatedRatesIndicator() |
||
392 | |||
393 | /** |
||
394 | * @param boolean $NegotiatedRatesIndicator |
||
395 | * @return ShipmentServiceOptions |
||
396 | */ |
||
397 | public function setNegotiatedRatesIndicator($NegotiatedRatesIndicator) |
||
402 | |||
403 | /** |
||
404 | * @return boolean |
||
405 | */ |
||
406 | 2 | public function isImportControlIndicator() |
|
410 | |||
411 | /** |
||
412 | * @param boolean $importControlIndicator |
||
413 | * @return ShipmentServiceOptions |
||
414 | */ |
||
415 | 3 | public function setImportControlIndicator($importControlIndicator) |
|
420 | |||
421 | /** |
||
422 | * @param DeliveryConfirmation $deliveryConfirmation |
||
423 | * @return ShipmentServiceOptions |
||
424 | */ |
||
425 | public function setDeliveryConfirmation(DeliveryConfirmation $deliveryConfirmation) |
||
430 | |||
431 | /** |
||
432 | * @return DeliveryConfirmation|null |
||
433 | */ |
||
434 | public function getDeliveryConfirmation() |
||
438 | |||
439 | /** |
||
440 | * @return boolean |
||
441 | */ |
||
442 | public function isDirectDeliveryOnlyIndicator() |
||
446 | |||
447 | /** |
||
448 | * @param boolean $DirectDeliveryOnlyIndicator |
||
449 | * @return ShipmentServiceOptions |
||
450 | */ |
||
451 | public function setDirectDeliveryOnlyIndicator($DirectDeliveryOnlyIndicator) |
||
456 | |||
457 | /** |
||
458 | * @return mixed |
||
459 | */ |
||
460 | public function getDeliverToAddresseeOnlyIndicator() |
||
464 | |||
465 | /** |
||
466 | * @param mixed $DeliverToAddresseeOnlyIndicator |
||
467 | * @return ShipmentServiceOptions |
||
468 | */ |
||
469 | public function setDeliverToAddresseeOnlyIndicator($DeliverToAddresseeOnlyIndicator) |
||
474 | } |
||
475 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.