Total Complexity | 49 |
Total Lines | 216 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AtBpost 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.
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 AtBpost, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class AtBpost extends National |
||
29 | { |
||
30 | protected ?string $product = Product::PRODUCT_NAME_BPACK_AT_BPOST; |
||
31 | private ?string $pugoId = null; |
||
32 | private ?string $pugoName = null; |
||
33 | private ?PugoAddress $pugoAddress = null; |
||
34 | private ?string $receiverName = null; |
||
35 | private ?string $receiverCompany = null; |
||
36 | protected ?string $requestedDeliveryDate = null; |
||
37 | private ?ShopHandlingInstruction $shopHandlingInstruction = null; |
||
38 | |||
39 | /** |
||
40 | * @throws BpostInvalidValueException |
||
41 | */ |
||
42 | public function setProduct(string $product): void |
||
43 | { |
||
44 | if (!in_array($product, self::getPossibleProductValues(), true)) { |
||
45 | throw new BpostInvalidValueException('product', $product, self::getPossibleProductValues()); |
||
46 | } |
||
47 | parent::setProduct($product); |
||
48 | } |
||
49 | |||
50 | public static function getPossibleProductValues(): array |
||
51 | { |
||
52 | return [ |
||
53 | Product::PRODUCT_NAME_BPACK_AT_BPOST, |
||
54 | ]; |
||
55 | } |
||
56 | |||
57 | public function setPugoAddress(?PugoAddress $pugoAddress): void |
||
58 | { |
||
59 | $this->pugoAddress = $pugoAddress; |
||
60 | } |
||
61 | |||
62 | public function getPugoAddress(): ?PugoAddress |
||
63 | { |
||
64 | return $this->pugoAddress; |
||
65 | } |
||
66 | |||
67 | public function setPugoId(?string $pugoId): void |
||
68 | { |
||
69 | $this->pugoId = $pugoId; |
||
70 | } |
||
71 | |||
72 | public function getPugoId(): ?string |
||
73 | { |
||
74 | return $this->pugoId; |
||
75 | } |
||
76 | |||
77 | public function setPugoName(?string $pugoName): void |
||
78 | { |
||
79 | $this->pugoName = $pugoName; |
||
80 | } |
||
81 | |||
82 | public function getPugoName(): ?string |
||
83 | { |
||
84 | return $this->pugoName; |
||
85 | } |
||
86 | |||
87 | public function setReceiverCompany(?string $receiverCompany): void |
||
88 | { |
||
89 | $this->receiverCompany = $receiverCompany; |
||
90 | } |
||
91 | |||
92 | public function getReceiverCompany(): ?string |
||
93 | { |
||
94 | return $this->receiverCompany; |
||
95 | } |
||
96 | |||
97 | public function setReceiverName(?string $receiverName): void |
||
98 | { |
||
99 | $this->receiverName = $receiverName; |
||
100 | } |
||
101 | |||
102 | public function getReceiverName(): ?string |
||
103 | { |
||
104 | return $this->receiverName; |
||
105 | } |
||
106 | |||
107 | public function getRequestedDeliveryDate(): ?string |
||
108 | { |
||
109 | return $this->requestedDeliveryDate; |
||
110 | } |
||
111 | |||
112 | public function setRequestedDeliveryDate(?string $requestedDeliveryDate): void |
||
113 | { |
||
114 | $this->requestedDeliveryDate = $requestedDeliveryDate; |
||
115 | } |
||
116 | |||
117 | public function getShopHandlingInstruction(): ?string |
||
120 | } |
||
121 | |||
122 | public function setShopHandlingInstruction(?string $shopHandlingInstruction): void |
||
123 | { |
||
124 | $this->shopHandlingInstruction = $shopHandlingInstruction !== null |
||
125 | ? new ShopHandlingInstruction($shopHandlingInstruction) |
||
126 | : null; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @throws \DOMException |
||
131 | */ |
||
132 | public function toXML(DOMDocument $document, ?string $prefix = null, ?string $type = null): DOMElement |
||
133 | { |
||
134 | $nationalElement = $document->createElement(XmlHelper::getPrefixedTagName('nationalBox', $prefix)); |
||
135 | $boxElement = parent::toXML($document, null, 'atBpost'); |
||
136 | $nationalElement->appendChild($boxElement); |
||
137 | |||
138 | if ($this->pugoId !== null) { |
||
139 | $boxElement->appendChild($document->createElement('pugoId', $this->pugoId)); |
||
140 | } |
||
141 | if ($this->pugoName !== null) { |
||
142 | $boxElement->appendChild($document->createElement('pugoName', $this->pugoName)); |
||
143 | } |
||
144 | if ($this->pugoAddress !== null) { |
||
145 | $boxElement->appendChild($this->pugoAddress->toXML($document)); |
||
146 | } |
||
147 | if ($this->receiverName !== null) { |
||
148 | $boxElement->appendChild($document->createElement('receiverName', $this->receiverName)); |
||
149 | } |
||
150 | if ($this->receiverCompany !== null) { |
||
151 | $boxElement->appendChild($document->createElement('receiverCompany', $this->receiverCompany)); |
||
152 | } |
||
153 | |||
154 | $this->addToXmlRequestedDeliveryDate($document, $boxElement, $prefix); |
||
155 | $this->addToXmlShopHandlingInstruction($document, $boxElement, $prefix); |
||
156 | |||
157 | return $nationalElement; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @throws \DOMException |
||
162 | */ |
||
163 | protected function addToXmlRequestedDeliveryDate(DOMDocument $document, DOMElement $typeElement, ?string $prefix): void |
||
168 | ); |
||
169 | } |
||
170 | } |
||
171 | |||
172 | private function addToXmlShopHandlingInstruction(DOMDocument $document, DOMElement $typeElement, ?string $prefix): void |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @throws BpostInvalidValueException |
||
182 | * @throws BpostNotImplementedException |
||
183 | * @throws BpostInvalidLengthException |
||
184 | */ |
||
185 | public static function createFromXML(SimpleXMLElement $xml, National $self = null): AtBpost |
||
186 | { |
||
187 | $atBpost = new AtBpost(); |
||
244 | } |
||
245 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.