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 |
||
19 | class VCard |
||
20 | { |
||
21 | /** |
||
22 | * definedElements |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | private $definedElements; |
||
27 | |||
28 | /** |
||
29 | * Filename |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $filename; |
||
34 | |||
35 | /** |
||
36 | * Save Path |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $savePath = null; |
||
41 | |||
42 | /** |
||
43 | * Multiple properties for element allowed |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $multiplePropertiesForElementAllowed = array( |
||
48 | 'email', |
||
49 | 'address', |
||
50 | 'phoneNumber', |
||
51 | 'url' |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Properties |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | private $properties; |
||
60 | |||
61 | /** |
||
62 | * Default Charset |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | public $charset = 'utf-8'; |
||
67 | |||
68 | /** |
||
69 | * Add address |
||
70 | * |
||
71 | * @param string [optional] $name |
||
72 | * @param string [optional] $extended |
||
73 | * @param string [optional] $street |
||
74 | * @param string [optional] $city |
||
75 | * @param string [optional] $region |
||
76 | * @param string [optional] $zip |
||
77 | * @param string [optional] $country |
||
78 | * @param string [optional] $type |
||
79 | * $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK |
||
80 | * or any combination of these: e.g. "WORK;PARCEL;POSTAL" |
||
81 | * @return $this |
||
82 | */ |
||
83 | public function addAddress( |
||
105 | |||
106 | /** |
||
107 | * Add birthday |
||
108 | * |
||
109 | * @param string $date Format is YYYY-MM-DD |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function addBirthday($date) |
||
122 | |||
123 | /** |
||
124 | * Add company |
||
125 | * |
||
126 | * @param string $company |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function addCompany($company, $department = '') |
||
145 | |||
146 | /** |
||
147 | * Add email |
||
148 | * |
||
149 | * @param string $address The e-mail address |
||
150 | * @param string [optional] $type The type of the email address |
||
151 | * $type may be PREF | WORK | HOME |
||
152 | * or any combination of these: e.g. "PREF;WORK" |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function addEmail($address, $type = '') |
||
165 | |||
166 | /** |
||
167 | * Add jobtitle |
||
168 | * |
||
169 | * @param string $jobtitle The jobtitle for the person. |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function addJobtitle($jobtitle) |
||
182 | |||
183 | /** |
||
184 | * Add role |
||
185 | * |
||
186 | * @param string $role The role for the person. |
||
187 | * @return $this |
||
188 | */ |
||
189 | public function addRole($role) |
||
199 | |||
200 | /** |
||
201 | * Add a photo or logo (depending on property name) |
||
202 | * |
||
203 | * @param string $property LOGO|PHOTO |
||
204 | * @param string $url image url or filename |
||
205 | * @param bool $include Do we include the image in our vcard or not? |
||
206 | * @throws VCardMediaException if file is empty or not an image file |
||
207 | */ |
||
208 | private function addMedia($property, $url, $include = true, $element) |
||
265 | |||
266 | /** |
||
267 | * Add name |
||
268 | * |
||
269 | * @param string [optional] $lastName |
||
270 | * @param string [optional] $firstName |
||
271 | * @param string [optional] $additional |
||
272 | * @param string [optional] $prefix |
||
273 | * @param string [optional] $suffix |
||
274 | * @return $this |
||
275 | */ |
||
276 | public function addName( |
||
315 | |||
316 | /** |
||
317 | * Add note |
||
318 | * |
||
319 | * @param string $note |
||
320 | * @return $this |
||
321 | */ |
||
322 | public function addNote($note) |
||
332 | |||
333 | /** |
||
334 | * Add categories |
||
335 | * |
||
336 | * @param array $categories |
||
337 | * @return $this |
||
338 | */ |
||
339 | public function addCategories($categories) |
||
349 | |||
350 | /** |
||
351 | * Add phone number |
||
352 | * |
||
353 | * @param string $number |
||
354 | * @param string [optional] $type |
||
355 | * Type may be PREF | WORK | HOME | VOICE | FAX | MSG | |
||
356 | * CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO |
||
357 | * or any senseful combination, e.g. "PREF;WORK;VOICE" |
||
358 | * @return $this |
||
359 | */ |
||
360 | public function addPhoneNumber($number, $type = '') |
||
370 | |||
371 | /** |
||
372 | * Add Logo |
||
373 | * |
||
374 | * @param string $url image url or filename |
||
375 | * @param bool $include Include the image in our vcard? |
||
376 | * @return $this |
||
377 | */ |
||
378 | public function addLogo($url, $include = true) |
||
389 | |||
390 | /** |
||
391 | * Add Photo |
||
392 | * |
||
393 | * @param string $url image url or filename |
||
394 | * @param bool $include Include the image in our vcard? |
||
395 | * @return $this |
||
396 | */ |
||
397 | public function addPhoto($url, $include = true) |
||
408 | |||
409 | /** |
||
410 | * Add URL |
||
411 | * |
||
412 | * @param string $url |
||
413 | * @param string [optional] $type Type may be WORK | HOME |
||
414 | * @return $this |
||
415 | */ |
||
416 | public function addURL($url, $type = '') |
||
426 | |||
427 | /** |
||
428 | * Build VCard (.vcf) |
||
429 | * |
||
430 | * @return string |
||
431 | */ |
||
432 | public function buildVCard() |
||
452 | |||
453 | /** |
||
454 | * Build VCalender (.ics) - Safari (< iOS 8) can not open .vcf files, so we have build a workaround. |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | public function buildVCalendar() |
||
493 | |||
494 | /** |
||
495 | * Returns the browser user agent string. |
||
496 | * |
||
497 | * @return string |
||
498 | */ |
||
499 | protected function getUserAgent() |
||
509 | |||
510 | /** |
||
511 | * Decode |
||
512 | * |
||
513 | * @param string $value The value to decode |
||
514 | * @return string decoded |
||
515 | */ |
||
516 | private function decode($value) |
||
521 | |||
522 | /** |
||
523 | * Download a vcard or vcal file to the browser. |
||
524 | */ |
||
525 | public function download() |
||
537 | |||
538 | /** |
||
539 | * Fold a line according to RFC2425 section 5.8.1. |
||
540 | * |
||
541 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.1 |
||
542 | * @param string $text |
||
543 | * @return mixed |
||
544 | */ |
||
545 | protected function fold($text) |
||
554 | |||
555 | /** |
||
556 | * Escape newline characters according to RFC2425 section 5.8.4. |
||
557 | * |
||
558 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.4 |
||
559 | * @param string $text |
||
560 | * @return string |
||
561 | */ |
||
562 | protected function escape($text) |
||
569 | |||
570 | /** |
||
571 | * Get output as string |
||
572 | * @deprecated in the future |
||
573 | * |
||
574 | * @return string |
||
575 | */ |
||
576 | public function get() |
||
580 | |||
581 | /** |
||
582 | * Get charset |
||
583 | * |
||
584 | * @return string |
||
585 | */ |
||
586 | public function getCharset() |
||
590 | |||
591 | /** |
||
592 | * Get charset string |
||
593 | * |
||
594 | * @return string |
||
595 | */ |
||
596 | public function getCharsetString() |
||
605 | |||
606 | /** |
||
607 | * Get content type |
||
608 | * |
||
609 | * @return string |
||
610 | */ |
||
611 | public function getContentType() |
||
616 | |||
617 | /** |
||
618 | * Get filename |
||
619 | * |
||
620 | * @return string |
||
621 | */ |
||
622 | public function getFilename() |
||
630 | |||
631 | /** |
||
632 | * Get file extension |
||
633 | * |
||
634 | * @return string |
||
635 | */ |
||
636 | public function getFileExtension() |
||
641 | |||
642 | /** |
||
643 | * Get headers |
||
644 | * |
||
645 | * @param bool $asAssociative |
||
646 | * @return array |
||
647 | */ |
||
648 | public function getHeaders($asAssociative) |
||
671 | |||
672 | /** |
||
673 | * Get output as string |
||
674 | * iOS devices (and safari < iOS 8 in particular) can not read .vcf (= vcard) files. |
||
675 | * So I build a workaround to build a .ics (= vcalender) file. |
||
676 | * |
||
677 | * @return string |
||
678 | */ |
||
679 | public function getOutput() |
||
686 | |||
687 | /** |
||
688 | * Get properties |
||
689 | * |
||
690 | * @return array |
||
691 | */ |
||
692 | public function getProperties() |
||
696 | |||
697 | /** |
||
698 | * Has property |
||
699 | * |
||
700 | * @param string $key |
||
701 | * @return bool |
||
702 | */ |
||
703 | public function hasProperty($key) |
||
715 | |||
716 | /** |
||
717 | * Is iOS - Check if the user is using an iOS-device |
||
718 | * |
||
719 | * @return bool |
||
720 | */ |
||
721 | public function isIOS() |
||
728 | |||
729 | /** |
||
730 | * Is iOS less than 7 (should cal wrapper be returned) |
||
731 | * |
||
732 | * @return bool |
||
733 | */ |
||
734 | public function isIOS7() |
||
738 | |||
739 | /** |
||
740 | * Save to a file |
||
741 | * |
||
742 | * @return void |
||
743 | */ |
||
744 | public function save() |
||
758 | |||
759 | /** |
||
760 | * Set charset |
||
761 | * |
||
762 | * @param mixed $charset |
||
763 | * @return void |
||
764 | */ |
||
765 | public function setCharset($charset) |
||
769 | |||
770 | /** |
||
771 | * Set filename |
||
772 | * |
||
773 | * @param mixed $value |
||
774 | * @param bool $overwrite [optional] Default overwrite is true |
||
775 | * @param string $separator [optional] Default separator is an underscore '_' |
||
776 | * @return void |
||
777 | */ |
||
778 | public function setFilename($value, $overwrite = true, $separator = '_') |
||
806 | |||
807 | /** |
||
808 | * Set the save path directory |
||
809 | * |
||
810 | * @param string $savePath Save Path |
||
811 | * @throws Exception |
||
812 | */ |
||
813 | public function setSavePath($savePath) |
||
826 | |||
827 | /** |
||
828 | * Set property |
||
829 | * |
||
830 | * @param string $element The element name you want to set, f.e.: name, email, phoneNumber, ... |
||
831 | * @param string $key |
||
832 | * @param string $value |
||
833 | * @throws Exception |
||
834 | */ |
||
835 | private function setProperty($element, $key, $value) |
||
852 | |||
853 | /** |
||
854 | * Checks if we should return vcard in cal wrapper |
||
855 | * |
||
856 | * @return bool |
||
857 | */ |
||
858 | protected function shouldAttachmentBeCal() |
||
868 | } |
||
869 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: