Total Complexity | 108 |
Total Lines | 520 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like Customer 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 Customer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Customer |
||
19 | { |
||
20 | public const CUSTOMER_PREFERRED_LANGUAGE_NL = 'nl-BE'; |
||
21 | public const CUSTOMER_PREFERRED_LANGUAGE_FR = 'fr-BE'; |
||
22 | public const CUSTOMER_PREFERRED_LANGUAGE_EN = 'en-US'; |
||
23 | |||
24 | public const CUSTOMER_TITLE_MR = 'Mr.'; |
||
25 | public const CUSTOMER_TITLE_MS = 'Ms.'; |
||
26 | |||
27 | public function __construct( |
||
28 | private ?bool $activated = null, |
||
29 | private ?string $userID = null, |
||
30 | private ?string $firstName = null, |
||
31 | private ?string $lastName = null, |
||
32 | private ?string $companyName = null, |
||
33 | private ?string $street = null, |
||
34 | private ?string $number = null, |
||
35 | private ?string $email = null, |
||
36 | private string $mobilePrefix = '0032', |
||
37 | private ?string $mobileNumber = null, |
||
38 | private ?string $postalCode = null, |
||
39 | private array $packStations = [], |
||
40 | private ?string $town = null, |
||
41 | private ?string $preferredLanguage = null, |
||
42 | private ?string $title = null, |
||
43 | private ?bool $isComfortZoneUser = null, |
||
44 | private ?DateTime $dateOfBirth = null, |
||
45 | private ?string $deliveryCode = null, |
||
46 | private ?bool $optIn = null, |
||
47 | private ?bool $receivePromotions = null, |
||
48 | private ?bool $useInformationForThirdParty = null, |
||
49 | private ?string $userName = null, |
||
50 | ) {} |
||
51 | |||
52 | |||
53 | public function setActivated(?bool $activated): void |
||
54 | { |
||
55 | $this->activated = $activated; |
||
56 | } |
||
57 | |||
58 | public function getActivated(): ?bool |
||
59 | { |
||
60 | return $this->activated; |
||
61 | } |
||
62 | |||
63 | public function setCompanyName(?string $companyName): void |
||
64 | { |
||
65 | $this->companyName = $companyName; |
||
66 | } |
||
67 | |||
68 | public function getCompanyName(): ?string |
||
69 | { |
||
70 | return $this->companyName; |
||
71 | } |
||
72 | |||
73 | public function setDateOfBirth(?DateTime $dateOfBirth): void |
||
74 | { |
||
75 | $this->dateOfBirth = $dateOfBirth; |
||
76 | } |
||
77 | |||
78 | public function getDateOfBirth(): ?DateTime |
||
79 | { |
||
80 | return $this->dateOfBirth; |
||
81 | } |
||
82 | |||
83 | public function setDeliveryCode(?string $deliveryCode): void |
||
84 | { |
||
85 | $this->deliveryCode = $deliveryCode; |
||
86 | } |
||
87 | |||
88 | public function getDeliveryCode(): ?string |
||
89 | { |
||
90 | return $this->deliveryCode; |
||
91 | } |
||
92 | |||
93 | public function setEmail(?string $email): void |
||
94 | { |
||
95 | $this->email = $email; |
||
96 | } |
||
97 | |||
98 | public function getEmail(): ?string |
||
99 | { |
||
100 | return $this->email; |
||
101 | } |
||
102 | |||
103 | public function setFirstName(?string $firstName): void |
||
104 | { |
||
105 | $this->firstName = $firstName; |
||
106 | } |
||
107 | |||
108 | public function getFirstName(): ?string |
||
109 | { |
||
110 | return $this->firstName; |
||
111 | } |
||
112 | |||
113 | public function setIsComfortZoneUser(?bool $isComfortZoneUser): void |
||
114 | { |
||
115 | $this->isComfortZoneUser = $isComfortZoneUser; |
||
116 | } |
||
117 | |||
118 | public function getIsComfortZoneUser(): ?bool |
||
119 | { |
||
120 | return $this->isComfortZoneUser; |
||
121 | } |
||
122 | |||
123 | public function setLastName(?string $lastName): void |
||
124 | { |
||
125 | $this->lastName = $lastName; |
||
126 | } |
||
127 | |||
128 | public function getLastName(): ?string |
||
129 | { |
||
130 | return $this->lastName; |
||
131 | } |
||
132 | |||
133 | public function setMobileNumber(?string $mobileNumber): void |
||
134 | { |
||
135 | $this->mobileNumber = $mobileNumber; |
||
136 | } |
||
137 | |||
138 | public function getMobileNumber(): ?string |
||
139 | { |
||
140 | return $this->mobileNumber; |
||
141 | } |
||
142 | |||
143 | public function setMobilePrefix(string $mobilePrefix): void |
||
144 | { |
||
145 | $this->mobilePrefix = $mobilePrefix; |
||
146 | } |
||
147 | |||
148 | public function getMobilePrefix(): string |
||
149 | { |
||
150 | return $this->mobilePrefix; |
||
151 | } |
||
152 | |||
153 | public function setNumber(?string $number): void |
||
154 | { |
||
155 | $this->number = $number; |
||
156 | } |
||
157 | |||
158 | public function getNumber(): ?string |
||
159 | { |
||
160 | return $this->number; |
||
161 | } |
||
162 | |||
163 | public function setOptIn(?bool $optIn): void |
||
164 | { |
||
165 | $this->optIn = $optIn; |
||
166 | } |
||
167 | |||
168 | public function getOptIn(): ?bool |
||
169 | { |
||
170 | return $this->optIn; |
||
171 | } |
||
172 | |||
173 | public function addPackStation(CustomerPackStation $packStation): void |
||
174 | { |
||
175 | $this->packStations[] = $packStation; |
||
176 | } |
||
177 | |||
178 | public function setPackStations(array $packStations): void |
||
179 | { |
||
180 | $this->packStations = $packStations; |
||
181 | } |
||
182 | |||
183 | public function getPackStations(): array |
||
184 | { |
||
185 | return $this->packStations; |
||
186 | } |
||
187 | |||
188 | public function setPostalCode(?string $postalCode): void |
||
189 | { |
||
190 | $this->postalCode = $postalCode; |
||
191 | } |
||
192 | |||
193 | public function getPostalCode(): ?string |
||
194 | { |
||
195 | return $this->postalCode; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @throws BpostInvalidValueException |
||
200 | */ |
||
201 | public function setPreferredLanguage(?string $preferredLanguage): void |
||
202 | { |
||
203 | if ($preferredLanguage === null) { |
||
204 | $this->preferredLanguage = null; |
||
205 | return; |
||
206 | } |
||
207 | if (!in_array($preferredLanguage, self::getPossiblePreferredLanguageValues(), true)) { |
||
208 | throw new BpostInvalidValueException('preferred language', $preferredLanguage, self::getPossiblePreferredLanguageValues()); |
||
209 | } |
||
210 | $this->preferredLanguage = $preferredLanguage; |
||
211 | } |
||
212 | |||
213 | public function getPreferredLanguage(): ?string |
||
214 | { |
||
215 | return $this->preferredLanguage; |
||
216 | } |
||
217 | |||
218 | public static function getPossiblePreferredLanguageValues(): array |
||
219 | { |
||
220 | return [ |
||
221 | self::CUSTOMER_PREFERRED_LANGUAGE_NL, |
||
222 | self::CUSTOMER_PREFERRED_LANGUAGE_FR, |
||
223 | self::CUSTOMER_PREFERRED_LANGUAGE_EN, |
||
224 | ]; |
||
225 | } |
||
226 | |||
227 | public function setReceivePromotions(?bool $receivePromotions): void |
||
228 | { |
||
229 | $this->receivePromotions = $receivePromotions; |
||
230 | } |
||
231 | |||
232 | public function getReceivePromotions(): ?bool |
||
235 | } |
||
236 | |||
237 | public function setStreet(?string $street): void |
||
238 | { |
||
239 | $this->street = $street; |
||
240 | } |
||
241 | |||
242 | public function getStreet(): ?string |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @throws BpostInvalidValueException |
||
249 | */ |
||
250 | public function setTitle(?string $title): void |
||
251 | { |
||
252 | if ($title === null) { |
||
253 | $this->title = null; |
||
254 | return; |
||
255 | } |
||
256 | if (!in_array($title, self::getPossibleTitleValues(), true)) { |
||
257 | throw new BpostInvalidValueException('title', $title, self::getPossibleTitleValues()); |
||
258 | } |
||
259 | $this->title = $title; |
||
260 | } |
||
261 | |||
262 | public function getTitle(): ?string |
||
263 | { |
||
264 | return $this->title; |
||
265 | } |
||
266 | |||
267 | public static function getPossibleTitleValues(): array |
||
268 | { |
||
269 | return [ |
||
270 | self::CUSTOMER_TITLE_MR, |
||
271 | self::CUSTOMER_TITLE_MS, |
||
272 | ]; |
||
273 | } |
||
274 | |||
275 | public function setTown(?string $town): void |
||
276 | { |
||
277 | $this->town = $town; |
||
278 | } |
||
279 | |||
280 | public function getTown(): ?string |
||
281 | { |
||
282 | return $this->town; |
||
283 | } |
||
284 | |||
285 | public function setUseInformationForThirdParty(?bool $useInformationForThirdParty): void |
||
286 | { |
||
287 | $this->useInformationForThirdParty = $useInformationForThirdParty; |
||
288 | } |
||
289 | |||
290 | public function getUseInformationForThirdParty(): ?bool |
||
291 | { |
||
292 | return $this->useInformationForThirdParty; |
||
293 | } |
||
294 | |||
295 | public function setUserID(?string $userID): void |
||
296 | { |
||
297 | $this->userID = $userID; |
||
298 | } |
||
299 | |||
300 | public function getUserID(): ?string |
||
301 | { |
||
302 | return $this->userID; |
||
303 | } |
||
304 | |||
305 | public function setUserName(?string $userName): void |
||
308 | } |
||
309 | |||
310 | public function getUserName(): ?string |
||
311 | { |
||
312 | return $this->userName; |
||
313 | } |
||
314 | |||
315 | public function toXML(DOMDocument $document): DOMElement |
||
316 | { |
||
317 | $customer = $document->createElement( |
||
318 | 'Customer' |
||
319 | ); |
||
320 | $customer->setAttribute( |
||
321 | 'xmlns', |
||
322 | 'http://schema.post.be/ServiceController/customer' |
||
323 | ); |
||
324 | $customer->setAttribute( |
||
325 | 'xmlns:xsi', |
||
326 | 'http://www.w3.org/2001/XMLSchema-instance' |
||
327 | ); |
||
328 | $customer->setAttribute( |
||
329 | 'xsi:schemaLocation', |
||
330 | 'http://schema.post.be/ServiceController/customer' |
||
331 | ); |
||
332 | |||
333 | $document->appendChild($customer); |
||
334 | |||
335 | $this->namingToXML($document, $customer); |
||
336 | $this->addressToXML($document, $customer); |
||
337 | $this->contactToXML($document, $customer); |
||
338 | $this->postalCodeToXML($document, $customer); |
||
339 | $this->preferredLanguageToXML($document, $customer); |
||
340 | $this->titleToXML($document, $customer); |
||
341 | |||
342 | return $customer; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @throws \DateMalformedStringException |
||
347 | * @throws BpostXmlNoUserIdFoundException |
||
348 | * @throws BpostInvalidValueException |
||
349 | */ |
||
350 | public static function createFromXML(SimpleXMLElement $xml): Customer |
||
437 | } |
||
438 | |||
439 | private function namingToXML(DOMDocument $document, DOMElement $customer): void |
||
440 | { |
||
441 | if ($this->getFirstName() !== null) { |
||
442 | $customer->appendChild( |
||
443 | $document->createElement( |
||
444 | 'FirstName', |
||
445 | $this->getFirstName() |
||
446 | ) |
||
447 | ); |
||
448 | } |
||
449 | if ($this->getLastName() !== null) { |
||
450 | $customer->appendChild( |
||
451 | $document->createElement( |
||
452 | 'LastName', |
||
453 | $this->getLastName() |
||
454 | ) |
||
455 | ); |
||
456 | } |
||
457 | } |
||
458 | |||
459 | private function contactToXML(DOMDocument $document, DOMElement $customer): void |
||
460 | { |
||
461 | if ($this->getEmail() !== null) { |
||
462 | $customer->appendChild( |
||
463 | $document->createElement( |
||
464 | 'Email', |
||
465 | $this->getEmail() |
||
466 | ) |
||
467 | ); |
||
468 | } |
||
469 | if ($this->getMobilePrefix() !== null) { |
||
|
|||
470 | $customer->appendChild( |
||
471 | $document->createElement( |
||
472 | 'MobilePrefix', |
||
473 | $this->getMobilePrefix() |
||
474 | ) |
||
475 | ); |
||
476 | } |
||
477 | if ($this->getMobileNumber() !== null) { |
||
478 | $customer->appendChild( |
||
479 | $document->createElement( |
||
480 | 'MobileNumber', |
||
481 | $this->getMobileNumber() |
||
482 | ) |
||
483 | ); |
||
484 | } |
||
485 | } |
||
486 | |||
487 | private function addressToXML(DOMDocument $document, DOMElement $customer): void |
||
488 | { |
||
489 | if ($this->getStreet() !== null) { |
||
490 | $customer->appendChild( |
||
491 | $document->createElement( |
||
492 | 'Street', |
||
493 | $this->getStreet() |
||
494 | ) |
||
495 | ); |
||
496 | } |
||
497 | if ($this->getNumber() !== null) { |
||
498 | $customer->appendChild( |
||
499 | $document->createElement( |
||
500 | 'Number', |
||
501 | $this->getNumber() |
||
502 | ) |
||
503 | ); |
||
504 | } |
||
505 | } |
||
506 | |||
507 | private function preferredLanguageToXML(DOMDocument $document, DOMElement $customer): void |
||
508 | { |
||
509 | if ($this->getPreferredLanguage() !== null) { |
||
510 | $customer->appendChild( |
||
511 | $document->createElement( |
||
512 | 'PreferredLanguage', |
||
513 | $this->getPreferredLanguage() |
||
514 | ) |
||
515 | ); |
||
516 | } |
||
517 | } |
||
518 | |||
519 | private function titleToXML(DOMDocument $document, DOMElement $customer): void |
||
526 | ) |
||
527 | ); |
||
528 | } |
||
529 | } |
||
530 | |||
531 | private function postalCodeToXML(DOMDocument $document, DOMElement $customer): void |
||
532 | { |
||
533 | if ($this->getPostalCode() !== null) { |
||
534 | $customer->appendChild( |
||
535 | $document->createElement( |
||
536 | 'PostalCode', |
||
537 | $this->getPostalCode() |
||
538 | ) |
||
539 | ); |
||
540 | } |
||
541 | } |
||
542 | } |
||
543 |