Total Complexity | 43 |
Total Lines | 219 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like International 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 International, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class International implements IBox |
||
32 | { |
||
33 | private ?string $product = null; |
||
34 | |||
35 | private array $options = []; |
||
36 | |||
37 | private ?Receiver $receiver = null; |
||
38 | |||
39 | private ?int $parcelWeight = null; |
||
40 | |||
41 | private ?CustomsInfo $customsInfo = null; |
||
42 | |||
43 | private array $parcelContents = []; |
||
44 | |||
45 | public function setCustomsInfo(?CustomsInfo $customsInfo): void |
||
46 | { |
||
47 | $this->customsInfo = $customsInfo; |
||
48 | } |
||
49 | |||
50 | public function getCustomsInfo(): ?CustomsInfo |
||
51 | { |
||
52 | return $this->customsInfo; |
||
53 | } |
||
54 | |||
55 | public function setOptions(array $options): void |
||
56 | { |
||
57 | $this->options = $options; |
||
58 | } |
||
59 | |||
60 | public function getOptions(): array |
||
61 | { |
||
62 | return $this->options; |
||
63 | } |
||
64 | |||
65 | public function addOption(Option $option): void |
||
66 | { |
||
67 | $this->options[] = $option; |
||
68 | } |
||
69 | |||
70 | public function setParcelWeight(?int $parcelWeight): void |
||
71 | { |
||
72 | $this->parcelWeight = $parcelWeight; |
||
73 | } |
||
74 | |||
75 | public function getParcelWeight(): ?int |
||
76 | { |
||
77 | return $this->parcelWeight; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @throws BpostInvalidValueException |
||
82 | */ |
||
83 | public function setProduct(string $product): void |
||
84 | { |
||
85 | if (!in_array($product, self::getPossibleProductValues(), true)) { |
||
86 | throw new BpostInvalidValueException('product', $product, self::getPossibleProductValues()); |
||
87 | } |
||
88 | $this->product = $product; |
||
89 | } |
||
90 | |||
91 | public function getProduct(): ?string |
||
92 | { |
||
93 | return $this->product; |
||
94 | } |
||
95 | |||
96 | public static function getPossibleProductValues(): array |
||
97 | { |
||
98 | return [ |
||
99 | Product::PRODUCT_NAME_BPACK_WORLD_BUSINESS, |
||
100 | Product::PRODUCT_NAME_BPACK_WORLD_EASY_RETURN, |
||
101 | Product::PRODUCT_NAME_BPACK_WORLD_EXPRESS_PRO, |
||
102 | Product::PRODUCT_NAME_BPACK_EUROPE_BUSINESS, |
||
103 | Product::PRODUCT_NAME_BPACK_AT_BPOST_INTERNATIONAL, |
||
104 | ]; |
||
105 | } |
||
106 | |||
107 | public function setReceiver(?Receiver $receiver): void |
||
108 | { |
||
109 | $this->receiver = $receiver; |
||
110 | } |
||
111 | |||
112 | public function getReceiver(): ?Receiver |
||
113 | { |
||
114 | return $this->receiver; |
||
115 | } |
||
116 | |||
117 | public function getParcelContents(): array |
||
118 | { |
||
119 | return $this->parcelContents; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @throws BpostInvalidValueException |
||
124 | */ |
||
125 | public function setParcelContents(array $parcelContents): self |
||
126 | { |
||
127 | foreach ($parcelContents as $parcelContent) { |
||
128 | if (!$parcelContent instanceof ParcelContent) { |
||
129 | throw new BpostInvalidValueException( |
||
130 | 'parcelContents', |
||
131 | is_object($parcelContent) ? get_class($parcelContent) : gettype($parcelContent), |
||
132 | ['Bpost\\BpostApiClient\\Bpost\\Order\\Box\\International\\ParcelContent'] |
||
133 | ); |
||
134 | } |
||
135 | $this->addParcelContent($parcelContent); |
||
136 | } |
||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | public function addParcelContent(ParcelContent $parcelContent): void |
||
141 | { |
||
142 | $this->parcelContents[] = $parcelContent; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @throws DOMException |
||
147 | */ |
||
148 | public function toXML(DOMDocument $document, ?string $prefix = null, ?string $type = null): DOMElement |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @throws BpostInvalidLengthException |
||
199 | * @throws BpostInvalidValueException |
||
200 | * @throws BpostNotImplementedException |
||
201 | */ |
||
202 | public static function createFromXML(SimpleXMLElement $xml): International |
||
203 | { |
||
204 | $international = new self(); |
||
205 | |||
206 | if (isset($xml->international->product) && (string)$xml->international->product !== '') { |
||
207 | $international->setProduct((string)$xml->international->product); |
||
208 | } |
||
209 | |||
210 | if (isset($xml->international->options)) { |
||
250 | } |
||
251 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.