Total Complexity | 42 |
Total Lines | 317 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 |
||
30 | class International implements IBox |
||
31 | { |
||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $product; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $options = array(); |
||
41 | |||
42 | /** |
||
43 | * @var Receiver |
||
44 | */ |
||
45 | private $receiver; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | private $parcelWeight; |
||
51 | |||
52 | /** |
||
53 | * @var CustomsInfo |
||
54 | */ |
||
55 | private $customsInfo; |
||
56 | |||
57 | /** |
||
58 | * Only for shipments outside Europe. |
||
59 | * Might include from 1 to 10 “parcelContent”. |
||
60 | * |
||
61 | * @var array|ParcelContent[] |
||
62 | */ |
||
63 | private $parcelContents = array(); |
||
64 | |||
65 | /** |
||
66 | * @param CustomsInfo $customsInfo |
||
67 | */ |
||
68 | public function setCustomsInfo($customsInfo) |
||
69 | { |
||
70 | $this->customsInfo = $customsInfo; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return CustomsInfo |
||
75 | */ |
||
76 | public function getCustomsInfo() |
||
77 | { |
||
78 | return $this->customsInfo; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param Option[] $options |
||
83 | */ |
||
84 | public function setOptions($options) |
||
85 | { |
||
86 | $this->options = $options; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return Option[] |
||
91 | */ |
||
92 | public function getOptions() |
||
93 | { |
||
94 | return $this->options; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param Option $option |
||
99 | */ |
||
100 | public function addOption(Option $option) |
||
101 | { |
||
102 | $this->options[] = $option; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param int $parcelWeight |
||
107 | */ |
||
108 | public function setParcelWeight($parcelWeight) |
||
109 | { |
||
110 | $this->parcelWeight = $parcelWeight; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return int |
||
115 | */ |
||
116 | public function getParcelWeight() |
||
117 | { |
||
118 | return $this->parcelWeight; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param string $product |
||
123 | * |
||
124 | * @throws BpostInvalidValueException |
||
125 | */ |
||
126 | public function setProduct($product) |
||
127 | { |
||
128 | if (!in_array($product, self::getPossibleProductValues())) { |
||
129 | throw new BpostInvalidValueException('product', $product, self::getPossibleProductValues()); |
||
130 | } |
||
131 | |||
132 | $this->product = $product; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return string |
||
137 | */ |
||
138 | public function getProduct() |
||
139 | { |
||
140 | return $this->product; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return array |
||
145 | */ |
||
146 | public static function getPossibleProductValues() |
||
147 | { |
||
148 | return array( |
||
149 | Product::PRODUCT_NAME_BPACK_WORLD_BUSINESS, |
||
150 | Product::PRODUCT_NAME_BPACK_WORLD_EASY_RETURN, |
||
151 | Product::PRODUCT_NAME_BPACK_WORLD_EXPRESS_PRO, |
||
152 | Product::PRODUCT_NAME_BPACK_EUROPE_BUSINESS, |
||
153 | Product::PRODUCT_NAME_BPACK_AT_BPOST_INTERNATIONAL, |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param Receiver $receiver |
||
159 | */ |
||
160 | public function setReceiver($receiver) |
||
161 | { |
||
162 | $this->receiver = $receiver; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return Receiver |
||
167 | */ |
||
168 | public function getReceiver() |
||
169 | { |
||
170 | return $this->receiver; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return array|ParcelContent[] |
||
175 | */ |
||
176 | public function getParcelContents() |
||
177 | { |
||
178 | return $this->parcelContents; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param array|ParcelContent[] $parcelContents |
||
183 | * |
||
184 | * @return self |
||
185 | * |
||
186 | * @throws BpostInvalidValueException |
||
187 | */ |
||
188 | public function setParcelContents(array $parcelContents) |
||
189 | { |
||
190 | foreach ($parcelContents as $parcelContent) { |
||
191 | if (!$parcelContent instanceof ParcelContent) { |
||
192 | throw new BpostInvalidValueException( |
||
193 | 'parcelContents', |
||
194 | get_class($parcelContent), |
||
195 | array('Bpost\BpostApiClient\Bpost\Order\Box\International\ParcelContent') |
||
196 | ); |
||
197 | } |
||
198 | |||
199 | $this->addParcelContent($parcelContent); |
||
200 | } |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | public function addParcelContent(ParcelContent $parcelContent) |
||
206 | { |
||
207 | $this->parcelContents[] = $parcelContent; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Return the object as an array for usage in the XML |
||
212 | * |
||
213 | * @param DomDocument $document |
||
214 | * @param string $prefix |
||
215 | * |
||
216 | * @return DOMElement |
||
217 | * |
||
218 | * @throws DOMException |
||
219 | */ |
||
220 | public function toXML(DOMDocument $document, $prefix = null) |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @param SimpleXMLElement $xml |
||
280 | * |
||
281 | * @return International |
||
282 | * |
||
283 | * @throws BpostInvalidLengthException |
||
284 | * @throws BpostInvalidValueException |
||
285 | * @throws BpostNotImplementedException |
||
286 | */ |
||
287 | public static function createFromXML(SimpleXMLElement $xml) |
||
288 | { |
||
289 | $international = new International(); |
||
290 | |||
291 | if (isset($xml->international->product) && $xml->international->product != '') { |
||
349 |