Complex classes like Package 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 Package, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Package implements NodeInterface |
||
10 | { |
||
11 | const PKG_OVERSIZE1 = '1'; |
||
12 | const PKG_OVERSIZE2 = '2'; |
||
13 | const PKG_LARGE = '4'; |
||
14 | |||
15 | /** |
||
16 | * @var PackagingType |
||
17 | */ |
||
18 | private $packagingType; |
||
19 | |||
20 | /** |
||
21 | * @var PackageWeight |
||
22 | */ |
||
23 | private $packageWeight; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $description; |
||
29 | |||
30 | /** |
||
31 | * @var PackageServiceOptions |
||
32 | */ |
||
33 | private $packageServiceOptions; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $upsPremiumCareIndicator; |
||
39 | |||
40 | /** |
||
41 | * @var ReferenceNumber |
||
42 | */ |
||
43 | private $referenceNumber; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $trackingNumber; |
||
49 | |||
50 | /** |
||
51 | * @var bool |
||
52 | */ |
||
53 | private $isLargePackage; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $additionalHandling; |
||
59 | |||
60 | /** |
||
61 | * @var Dimensions|null |
||
62 | */ |
||
63 | private $dimensions; |
||
64 | |||
65 | /** |
||
66 | * @var Activity[] |
||
67 | */ |
||
68 | private $activities = []; |
||
69 | |||
70 | /** |
||
71 | * @param null|object $attributes |
||
72 | */ |
||
73 | public function __construct($attributes = null) |
||
74 | { |
||
75 | $this->setPackagingType(new PackagingType( |
||
76 | isset($attributes->PackagingType) ? $attributes->PackagingType : null) |
||
77 | ); |
||
78 | $this->setReferenceNumber(new ReferenceNumber()); |
||
79 | $this->setPackageWeight(new PackageWeight()); |
||
80 | $this->setPackageServiceOptions(new PackageServiceOptions()); |
||
81 | |||
82 | if (null !== $attributes) { |
||
83 | if (isset($attributes->PackageWeight)) { |
||
84 | $this->setPackageWeight(new PackageWeight($attributes->PackageWeight)); |
||
85 | } |
||
86 | if (isset($attributes->Description)) { |
||
87 | $this->setDescription($attributes->Description); |
||
88 | } |
||
89 | if (isset($attributes->PackageServiceOptions)) { |
||
90 | $this->setPackageServiceOptions(new PackageServiceOptions($attributes->PackageServiceOptions)); |
||
91 | } |
||
92 | if (isset($attributes->UPSPremiumCareIndicator)) { |
||
93 | $this->setUpsPremiumCareIndicator($attributes->UPSPremiumCareIndicator); |
||
94 | } |
||
95 | if (isset($attributes->ReferenceNumber)) { |
||
96 | $this->setReferenceNumber(new ReferenceNumber($attributes->ReferenceNumber)); |
||
97 | } |
||
98 | if (isset($attributes->TrackingNumber)) { |
||
99 | $this->setTrackingNumber($attributes->TrackingNumber); |
||
100 | } |
||
101 | if (isset($attributes->LargePackage)) { |
||
102 | $this->setLargePackage($attributes->LargePackage); |
||
103 | } |
||
104 | if (isset($attributes->Dimensions)) { |
||
105 | $this->setDimensions(new Dimensions($attributes->Dimensions)); |
||
106 | } |
||
107 | if (isset($attributes->Activity)) { |
||
108 | $activities = $this->getActivities(); |
||
109 | if (is_array($attributes->Activity)) { |
||
110 | foreach ($attributes->Activity as $Activity) { |
||
111 | $activities[] = new Activity($Activity); |
||
112 | } |
||
113 | } else { |
||
114 | $activities[] = new Activity($attributes->Activity); |
||
115 | } |
||
116 | $this->setActivities($activities); |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param null|DOMDocument $document |
||
123 | * |
||
124 | * @return DOMElement |
||
125 | */ |
||
126 | public function toNode(DOMDocument $document = null) |
||
127 | { |
||
128 | if (null === $document) { |
||
129 | $document = new DOMDocument(); |
||
130 | } |
||
131 | |||
132 | $packageNode = $document->createElement('Package'); |
||
133 | |||
134 | if ($this->getDescription()) { |
||
135 | $packageNode->appendChild($document->createElement('Description', $this->getDescription())); |
||
136 | } |
||
137 | $packageNode->appendChild($this->getPackagingType()->toNode($document)); |
||
138 | $packageNode->appendChild($this->getPackageWeight()->toNode($document)); |
||
139 | |||
140 | |||
141 | if (null !== $this->getDimensions()) { |
||
142 | $packageNode->appendChild($this->getDimensions()->toNode($document)); |
||
143 | } |
||
144 | |||
145 | if ($this->isLargePackage()) { |
||
146 | $packageNode->appendChild($document->createElement('LargePackageIndicator')); |
||
147 | } |
||
148 | |||
149 | if ($this->getAdditionalHandling()) { |
||
150 | $packageNode->appendChild($document->createElement('AdditionalHandling')); |
||
151 | } |
||
152 | |||
153 | if ($this->getPackageServiceOptions()) { |
||
154 | $packageNode->appendChild($this->getPackageServiceOptions()->toNode($document)); |
||
155 | } |
||
156 | |||
157 | if ($this->getReferenceNumber() |
||
158 | && !is_null($this->getReferenceNumber()->getCode()) |
||
159 | && !is_null($this->getReferenceNumber()->getValue()) |
||
160 | ) { |
||
161 | $packageNode->appendChild($this->getReferenceNumber()->toNode($document)); |
||
162 | } |
||
163 | |||
164 | return $packageNode; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @return Activity[] |
||
169 | */ |
||
170 | public function getActivities() |
||
174 | |||
175 | /** |
||
176 | * @param Activity[] $activities |
||
177 | * |
||
178 | * @return Package |
||
179 | */ |
||
180 | public function setActivities(array $activities) |
||
181 | { |
||
182 | $this->activities = $activities; |
||
183 | |||
184 | return $this; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getDescription() |
||
194 | |||
195 | /** |
||
196 | * @param string $description |
||
197 | * |
||
198 | * @return Package |
||
199 | */ |
||
200 | public function setDescription($description) |
||
201 | { |
||
202 | $this->description = $description; |
||
203 | |||
204 | return $this; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @return Dimensions|null |
||
209 | */ |
||
210 | public function getDimensions() |
||
214 | |||
215 | /** |
||
216 | * @param Dimensions $dimensions |
||
217 | * |
||
218 | * @return Package |
||
219 | */ |
||
220 | public function setDimensions(Dimensions $dimensions) |
||
221 | { |
||
222 | $this->dimensions = $dimensions; |
||
223 | |||
224 | return $this; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function isLargePackage() |
||
234 | |||
235 | /** |
||
236 | * @param bool $largePackage |
||
237 | * |
||
238 | * @return Package |
||
239 | */ |
||
240 | public function setLargePackage($largePackage) |
||
241 | { |
||
242 | $this->isLargePackage = $largePackage; |
||
243 | |||
244 | return $this; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @return PackageServiceOptions |
||
249 | */ |
||
250 | public function getPackageServiceOptions() |
||
254 | |||
255 | /** |
||
256 | * @param PackageServiceOptions $packageServiceOptions |
||
257 | * |
||
258 | * @return Package |
||
259 | */ |
||
260 | public function setPackageServiceOptions(PackageServiceOptions $packageServiceOptions) |
||
261 | { |
||
262 | $this->packageServiceOptions = $packageServiceOptions; |
||
263 | |||
264 | return $this; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @return PackageWeight |
||
269 | */ |
||
270 | public function getPackageWeight() |
||
274 | |||
275 | /** |
||
276 | * @param PackageWeight $packageWeight |
||
277 | * |
||
278 | * @return Package |
||
279 | */ |
||
280 | public function setPackageWeight(PackageWeight $packageWeight) |
||
281 | { |
||
282 | $this->packageWeight = $packageWeight; |
||
283 | |||
284 | return $this; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * @return PackagingType |
||
289 | */ |
||
290 | public function getPackagingType() |
||
294 | |||
295 | /** |
||
296 | * @param PackagingType $packagingType |
||
297 | * |
||
298 | * @return Package |
||
299 | */ |
||
300 | public function setPackagingType(PackagingType $packagingType) |
||
301 | { |
||
302 | $this->packagingType = $packagingType; |
||
303 | |||
304 | return $this; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @return ReferenceNumber |
||
309 | */ |
||
310 | public function getReferenceNumber() |
||
314 | |||
315 | /** |
||
316 | * @param ReferenceNumber $referenceNumber |
||
317 | * |
||
318 | * @return Package |
||
319 | */ |
||
320 | public function setReferenceNumber(ReferenceNumber $referenceNumber) |
||
321 | { |
||
322 | $this->referenceNumber = $referenceNumber; |
||
323 | |||
324 | return $this; |
||
325 | } |
||
326 | |||
327 | public function removeReferenceNumber() |
||
331 | |||
332 | /** |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getTrackingNumber() |
||
339 | |||
340 | /** |
||
341 | * @param string $trackingNumber |
||
342 | * |
||
343 | * @return Package |
||
344 | */ |
||
345 | public function setTrackingNumber($trackingNumber) |
||
346 | { |
||
347 | $this->trackingNumber = $trackingNumber; |
||
348 | |||
349 | return $this; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @return string |
||
354 | */ |
||
355 | public function getUpsPremiumCareIndicator() |
||
359 | |||
360 | /** |
||
361 | * @param string $upsPremiumCareIndicator |
||
362 | * |
||
363 | * @return Package |
||
364 | */ |
||
365 | public function setUpsPremiumCareIndicator($upsPremiumCareIndicator) |
||
366 | { |
||
367 | $this->upsPremiumCareIndicator = $upsPremiumCareIndicator; |
||
368 | |||
369 | return $this; |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * @return boolean |
||
374 | */ |
||
375 | public function getAdditionalHandling() |
||
379 | |||
380 | /** |
||
381 | * @param boolean $additionalHandling |
||
382 | */ |
||
383 | public function setAdditionalHandling($additionalHandling) |
||
387 | } |
||
388 |