Complex classes like VCard 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 VCard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class VCard extends \stdClass |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $fullName; |
||
16 | |||
17 | /** |
||
18 | * @var string|null |
||
19 | */ |
||
20 | protected $lastName; |
||
21 | |||
22 | /** |
||
23 | * @var string|null |
||
24 | */ |
||
25 | protected $firstName; |
||
26 | |||
27 | /** |
||
28 | * @var string|null |
||
29 | */ |
||
30 | protected $additional; |
||
31 | |||
32 | /** |
||
33 | * @var string|null |
||
34 | */ |
||
35 | protected $prefix; |
||
36 | |||
37 | /** |
||
38 | * @var string|null |
||
39 | */ |
||
40 | protected $suffix; |
||
41 | |||
42 | /** |
||
43 | * @var \DateTime|null |
||
44 | */ |
||
45 | protected $birthday; |
||
46 | |||
47 | /** |
||
48 | * @var VCardAddress[][]|null |
||
49 | */ |
||
50 | protected $address; |
||
51 | |||
52 | /** |
||
53 | * @var string[][]|null |
||
54 | */ |
||
55 | protected $phone; |
||
56 | |||
57 | /** |
||
58 | * @var string[][]|null |
||
59 | */ |
||
60 | protected $email; |
||
61 | |||
62 | /** |
||
63 | * @var string|null |
||
64 | */ |
||
65 | protected $revision; |
||
66 | |||
67 | /** |
||
68 | * @var string|null |
||
69 | */ |
||
70 | protected $version; |
||
71 | |||
72 | /** |
||
73 | * @var string|null |
||
74 | */ |
||
75 | protected $organization; |
||
76 | |||
77 | /** |
||
78 | * @var string[][]|null |
||
79 | */ |
||
80 | protected $url; |
||
81 | |||
82 | /** |
||
83 | * @var string|null |
||
84 | */ |
||
85 | protected $title; |
||
86 | |||
87 | /** |
||
88 | * @var VCardMedia|null |
||
89 | */ |
||
90 | protected $photo; |
||
91 | |||
92 | /** |
||
93 | * @var VCardMedia|null |
||
94 | */ |
||
95 | protected $logo; |
||
96 | |||
97 | /** |
||
98 | * @var string|null |
||
99 | */ |
||
100 | protected $note; |
||
101 | |||
102 | /** |
||
103 | * @var array|null |
||
104 | */ |
||
105 | protected $categories; |
||
106 | |||
107 | /** |
||
108 | * @var string|null |
||
109 | */ |
||
110 | protected $geo; |
||
111 | |||
112 | /** |
||
113 | * @var string|null |
||
114 | */ |
||
115 | protected $gender; |
||
116 | |||
117 | /** |
||
118 | * @var string[][]|null |
||
119 | */ |
||
120 | protected $nickname; |
||
121 | |||
122 | /** |
||
123 | * @var string[][]|null |
||
124 | */ |
||
125 | protected $skype; |
||
126 | |||
127 | /** |
||
128 | * @var array|null |
||
129 | */ |
||
130 | protected $item; |
||
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getFullName(): string |
||
139 | |||
140 | /** |
||
141 | * @param string $fullName |
||
142 | */ |
||
143 | public function setFullName(string $fullName): void |
||
147 | |||
148 | /** |
||
149 | * @return null|string |
||
150 | */ |
||
151 | public function getLastName(): ?string |
||
155 | |||
156 | /** |
||
157 | * @param null|string $lastName |
||
158 | */ |
||
159 | public function setLastName(?string $lastName): void |
||
163 | |||
164 | /** |
||
165 | * @return null|string |
||
166 | */ |
||
167 | public function getFirstName(): ?string |
||
171 | |||
172 | /** |
||
173 | * @param null|string $firstName |
||
174 | */ |
||
175 | public function setFirstName(?string $firstName): void |
||
179 | |||
180 | /** |
||
181 | * @return null|string |
||
182 | */ |
||
183 | public function getAdditional(): ?string |
||
187 | |||
188 | /** |
||
189 | * @param null|string $additional |
||
190 | */ |
||
191 | public function setAdditional(?string $additional): void |
||
195 | |||
196 | /** |
||
197 | * @return null|string |
||
198 | */ |
||
199 | public function getPrefix(): ?string |
||
203 | |||
204 | /** |
||
205 | * @param null|string $prefix |
||
206 | */ |
||
207 | public function setPrefix(?string $prefix): void |
||
211 | |||
212 | /** |
||
213 | * @return null|string |
||
214 | */ |
||
215 | public function getSuffix(): ?string |
||
219 | |||
220 | /** |
||
221 | * @param null|string $suffix |
||
222 | */ |
||
223 | public function setSuffix(?string $suffix): void |
||
227 | |||
228 | /** |
||
229 | * @return \DateTime|null |
||
230 | */ |
||
231 | public function getBirthday(): ?\DateTime |
||
235 | |||
236 | /** |
||
237 | * @param \DateTime|null $birthday |
||
238 | */ |
||
239 | public function setBirthday(?\DateTime $birthday): void |
||
243 | |||
244 | /** |
||
245 | * @return VCardAddress[][]|null |
||
246 | */ |
||
247 | public function getAddresses(): ?array |
||
251 | |||
252 | /** |
||
253 | * @param VCardAddress[][]|null $address |
||
254 | */ |
||
255 | public function setAddresses(?array $address): void |
||
259 | |||
260 | /** |
||
261 | * @param string $key |
||
262 | * |
||
263 | * @return VCardAddress[]|null |
||
264 | */ |
||
265 | public function getAddress(string $key): ?array |
||
269 | |||
270 | /** |
||
271 | * @param VCardAddress $address |
||
272 | * @param string $key |
||
273 | */ |
||
274 | public function addAddress(VCardAddress $address, string $key = ''): void |
||
278 | |||
279 | /** |
||
280 | * @return null|string[][] |
||
281 | */ |
||
282 | public function getPhones(): ?array |
||
286 | |||
287 | /** |
||
288 | * @param null|string[][] $phone |
||
289 | */ |
||
290 | public function setPhones(?array $phone): void |
||
294 | |||
295 | /** |
||
296 | * @param string $key |
||
297 | * |
||
298 | * @return string[]|null |
||
299 | */ |
||
300 | public function getPhone(string $key): ?array |
||
304 | |||
305 | /** |
||
306 | * @param string $phone |
||
307 | * @param string $key |
||
308 | */ |
||
309 | public function addPhone(string $phone, string $key = ''): void |
||
313 | |||
314 | /** |
||
315 | * @return null|string[][] |
||
316 | */ |
||
317 | public function getEmails(): ?array |
||
321 | |||
322 | /** |
||
323 | * @param null|string[][] $email |
||
324 | */ |
||
325 | public function setEmails(?array $email): void |
||
329 | |||
330 | /** |
||
331 | * @param string $key |
||
332 | * |
||
333 | * @return string[]|null |
||
334 | */ |
||
335 | public function getEmail(string $key): ?array |
||
339 | |||
340 | /** |
||
341 | * @param string $email |
||
342 | * @param string $key |
||
343 | */ |
||
344 | public function addEmail(string $email, string $key = ''): void |
||
348 | |||
349 | /** |
||
350 | * @return null|string |
||
351 | */ |
||
352 | public function getRevision(): ?string |
||
356 | |||
357 | /** |
||
358 | * @param null|string $revision |
||
359 | */ |
||
360 | public function setRevision(?string $revision): void |
||
364 | |||
365 | /** |
||
366 | * @return null|string |
||
367 | */ |
||
368 | public function getVersion(): ?string |
||
372 | |||
373 | /** |
||
374 | * @param null|string $version |
||
375 | */ |
||
376 | public function setVersion(?string $version): void |
||
380 | |||
381 | /** |
||
382 | * @return null|string |
||
383 | */ |
||
384 | public function getOrganization(): ?string |
||
388 | |||
389 | /** |
||
390 | * @param null|string $organization |
||
391 | */ |
||
392 | public function setOrganization(?string $organization): void |
||
396 | |||
397 | /** |
||
398 | * @return null|string[][] |
||
399 | */ |
||
400 | public function getUrls(): ?array |
||
404 | |||
405 | /** |
||
406 | * @param null|string[][] $url |
||
407 | */ |
||
408 | public function setUrls(?array $url): void |
||
412 | |||
413 | /** |
||
414 | * @param string $key |
||
415 | * |
||
416 | * @return string[]|null |
||
417 | */ |
||
418 | public function getUrl(string $key): ?array |
||
422 | |||
423 | /** |
||
424 | * @param string $url |
||
425 | * @param string $key |
||
426 | */ |
||
427 | public function addUrl(string $url, string $key = ''): void |
||
431 | |||
432 | /** |
||
433 | * @return null|string |
||
434 | */ |
||
435 | public function getTitle(): ?string |
||
439 | |||
440 | /** |
||
441 | * @param null|string $title |
||
442 | */ |
||
443 | public function setTitle(?string $title): void |
||
447 | |||
448 | /** |
||
449 | * @return null|VCardMedia |
||
450 | */ |
||
451 | public function getPhoto(): ?VCardMedia |
||
455 | |||
456 | /** |
||
457 | * @param null|VCardMedia $photo |
||
458 | */ |
||
459 | public function setPhoto(?VCardMedia $photo): void |
||
463 | |||
464 | /** |
||
465 | * @return null|VCardMedia |
||
466 | */ |
||
467 | public function getLogo(): ?VCardMedia |
||
471 | |||
472 | /** |
||
473 | * @param null|VCardMedia $logo |
||
474 | */ |
||
475 | public function setLogo(?VCardMedia $logo): void |
||
479 | |||
480 | /** |
||
481 | * @return null|string |
||
482 | */ |
||
483 | public function getNote(): ?string |
||
487 | |||
488 | /** |
||
489 | * @param null|string $note |
||
490 | */ |
||
491 | public function setNote(?string $note): void |
||
495 | |||
496 | /** |
||
497 | * @return array|null |
||
498 | */ |
||
499 | public function getCategories(): ?array |
||
503 | |||
504 | /** |
||
505 | * @param array|null $categories |
||
506 | */ |
||
507 | public function setCategories(?array $categories): void |
||
511 | |||
512 | // /** |
||
|
|||
513 | // * TODO: Add functions |
||
514 | // */ |
||
515 | // /** |
||
516 | // * @return null|string |
||
517 | // */ |
||
518 | // public function getGeo(): ?string |
||
519 | // { |
||
520 | // return $this->geo; |
||
521 | // } |
||
522 | // |
||
523 | // /** |
||
524 | // * @param null|string $geo |
||
525 | // */ |
||
526 | // public function setGeo(?string $geo): void |
||
527 | // { |
||
528 | // $this->geo = $geo; |
||
529 | // } |
||
530 | // |
||
531 | // /** |
||
532 | // * @return null|string |
||
533 | // */ |
||
534 | // public function getGender(): ?string |
||
535 | // { |
||
536 | // return $this->gender; |
||
537 | // } |
||
538 | // |
||
539 | // /** |
||
540 | // * @param null|string $gender |
||
541 | // */ |
||
542 | // public function setGender(?string $gender): void |
||
543 | // { |
||
544 | // $this->gender = $gender; |
||
545 | // } |
||
546 | // |
||
547 | // /** |
||
548 | // * @return null|string[][] |
||
549 | // */ |
||
550 | // public function getNickname(): ?array |
||
551 | // { |
||
552 | // return $this->nickname; |
||
553 | // } |
||
554 | // |
||
555 | // /** |
||
556 | // * @param null|string[][] $nickname |
||
557 | // */ |
||
558 | // public function setNickname(?array $nickname): void |
||
559 | // { |
||
560 | // $this->nickname = $nickname; |
||
561 | // } |
||
562 | // |
||
563 | // /** |
||
564 | // * @return null|string[][] |
||
565 | // */ |
||
566 | // public function getSkype(): ?array |
||
567 | // { |
||
568 | // return $this->skype; |
||
569 | // } |
||
570 | // |
||
571 | // /** |
||
572 | // * @param null|string[][] $skype |
||
573 | // */ |
||
574 | // public function setSkype(?array $skype): void |
||
575 | // { |
||
576 | // $this->skype = $skype; |
||
577 | // } |
||
578 | // |
||
579 | // /** |
||
580 | // * @return array|null |
||
581 | // */ |
||
582 | // public function getItem(): ?array |
||
583 | // { |
||
584 | // return $this->item; |
||
585 | // } |
||
586 | // |
||
587 | // /** |
||
588 | // * @param array|null $item |
||
589 | // */ |
||
590 | // public function setItem(?array $item): void |
||
591 | // { |
||
592 | // $this->item = $item; |
||
593 | // } |
||
594 | } |
||
595 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.