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) |
||
144 | |||
145 | /** |
||
146 | * Add email |
||
147 | * |
||
148 | * @param string $address The e-mail address |
||
149 | * @param string [optional] $type The type of the email address |
||
150 | * $type may be PREF | WORK | HOME |
||
151 | * or any combination of these: e.g. "PREF;WORK" |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function addEmail($address, $type = '') |
||
164 | |||
165 | /** |
||
166 | * Add jobtitle |
||
167 | * |
||
168 | * @param string $jobtitle The jobtitle for the person. |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function addJobtitle($jobtitle) |
||
181 | |||
182 | /** |
||
183 | * Add role |
||
184 | * |
||
185 | * @param string $role The role for the person. |
||
186 | * @return $this |
||
187 | */ |
||
188 | public function addRole($role) |
||
198 | |||
199 | /** |
||
200 | * Add a photo or logo (depending on property name) |
||
201 | * |
||
202 | * @param string $property LOGO|PHOTO |
||
203 | * @param string $url image url or filename |
||
204 | * @param bool $include Do we include the image in our vcard or not? |
||
205 | * @throws VCardMediaException if file is empty or not an image file |
||
206 | */ |
||
207 | private function addMedia($property, $url, $include = true, $element) |
||
264 | |||
265 | /** |
||
266 | * Add name |
||
267 | * |
||
268 | * @param string [optional] $lastName |
||
269 | * @param string [optional] $firstName |
||
270 | * @param string [optional] $additional |
||
271 | * @param string [optional] $prefix |
||
272 | * @param string [optional] $suffix |
||
273 | * @return $this |
||
274 | */ |
||
275 | public function addName( |
||
314 | |||
315 | /** |
||
316 | * Add note |
||
317 | * |
||
318 | * @param string $note |
||
319 | * @return $this |
||
320 | */ |
||
321 | public function addNote($note) |
||
331 | |||
332 | /** |
||
333 | * Add phone number |
||
334 | * |
||
335 | * @param string $number |
||
336 | * @param string [optional] $type |
||
337 | * Type may be PREF | WORK | HOME | VOICE | FAX | MSG | |
||
338 | * CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO |
||
339 | * or any senseful combination, e.g. "PREF;WORK;VOICE" |
||
340 | * @return $this |
||
341 | */ |
||
342 | public function addPhoneNumber($number, $type = '') |
||
352 | |||
353 | /** |
||
354 | * Add Logo |
||
355 | * |
||
356 | * @param string $url image url or filename |
||
357 | * @param bool $include Include the image in our vcard? |
||
358 | * @return $this |
||
359 | */ |
||
360 | public function addLogo($url, $include = true) |
||
371 | |||
372 | /** |
||
373 | * Add Photo |
||
374 | * |
||
375 | * @param string $url image url or filename |
||
376 | * @param bool $include Include the image in our vcard? |
||
377 | * @return $this |
||
378 | */ |
||
379 | public function addPhoto($url, $include = true) |
||
390 | |||
391 | /** |
||
392 | * Add URL |
||
393 | * |
||
394 | * @param string $url |
||
395 | * @param string [optional] $type Type may be WORK | HOME |
||
396 | * @return $this |
||
397 | */ |
||
398 | public function addURL($url, $type = '') |
||
408 | |||
409 | /** |
||
410 | * Build VCard (.vcf) |
||
411 | * |
||
412 | * @return string |
||
413 | */ |
||
414 | public function buildVCard() |
||
434 | |||
435 | /** |
||
436 | * Build VCalender (.ics) - Safari (< iOS 8) can not open .vcf files, so we have build a workaround. |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | public function buildVCalendar() |
||
475 | |||
476 | /** |
||
477 | * Returns the browser user agent string. |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | protected function getUserAgent() |
||
491 | |||
492 | /** |
||
493 | * Decode |
||
494 | * |
||
495 | * @param string $value The value to decode |
||
496 | * @return string decoded |
||
497 | */ |
||
498 | private function decode($value) |
||
503 | |||
504 | /** |
||
505 | * Download a vcard or vcal file to the browser. |
||
506 | */ |
||
507 | public function download() |
||
519 | |||
520 | /** |
||
521 | * Fold a line according to RFC2425 section 5.8.1. |
||
522 | * |
||
523 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.1 |
||
524 | * @param string $text |
||
525 | * @return mixed |
||
526 | */ |
||
527 | protected function fold($text) |
||
536 | |||
537 | /** |
||
538 | * Escape newline characters according to RFC2425 section 5.8.4. |
||
539 | * |
||
540 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.4 |
||
541 | * @param string $text |
||
542 | * @return string |
||
543 | */ |
||
544 | protected function escape($text) |
||
551 | |||
552 | /** |
||
553 | * Get output as string |
||
554 | * @deprecated in the future |
||
555 | * |
||
556 | * @return string |
||
557 | */ |
||
558 | public function get() |
||
562 | |||
563 | /** |
||
564 | * Get charset |
||
565 | * |
||
566 | * @return string |
||
567 | */ |
||
568 | public function getCharset() |
||
572 | |||
573 | /** |
||
574 | * Get charset string |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | public function getCharsetString() |
||
586 | |||
587 | /** |
||
588 | * Get content type |
||
589 | * |
||
590 | * @return string |
||
591 | */ |
||
592 | public function getContentType() |
||
597 | |||
598 | /** |
||
599 | * Get filename |
||
600 | * |
||
601 | * @return string |
||
602 | */ |
||
603 | public function getFilename() |
||
610 | |||
611 | /** |
||
612 | * Get file extension |
||
613 | * |
||
614 | * @return string |
||
615 | */ |
||
616 | public function getFileExtension() |
||
621 | |||
622 | /** |
||
623 | * Get headers |
||
624 | * |
||
625 | * @param bool $asAssociative |
||
626 | * @return array |
||
627 | */ |
||
628 | public function getHeaders($asAssociative) |
||
651 | |||
652 | /** |
||
653 | * Get output as string |
||
654 | * iOS devices (and safari < iOS 8 in particular) can not read .vcf (= vcard) files. |
||
655 | * So I build a workaround to build a .ics (= vcalender) file. |
||
656 | * |
||
657 | * @return string |
||
658 | */ |
||
659 | public function getOutput() |
||
666 | |||
667 | /** |
||
668 | * Get properties |
||
669 | * |
||
670 | * @return array |
||
671 | */ |
||
672 | public function getProperties() |
||
676 | |||
677 | /** |
||
678 | * Has property |
||
679 | * |
||
680 | * @param string $key |
||
681 | * @return bool |
||
682 | */ |
||
683 | public function hasProperty($key) |
||
695 | |||
696 | /** |
||
697 | * Is iOS - Check if the user is using an iOS-device |
||
698 | * |
||
699 | * @return bool |
||
700 | */ |
||
701 | public function isIOS() |
||
708 | |||
709 | /** |
||
710 | * Is iOS less than 7 (should cal wrapper be returned) |
||
711 | * |
||
712 | * @return bool |
||
713 | */ |
||
714 | public function isIOS7() |
||
718 | |||
719 | /** |
||
720 | * Save to a file |
||
721 | * |
||
722 | * @return void |
||
723 | */ |
||
724 | public function save() |
||
738 | |||
739 | /** |
||
740 | * Set charset |
||
741 | * |
||
742 | * @param mixed $charset |
||
743 | * @return void |
||
744 | */ |
||
745 | public function setCharset($charset) |
||
749 | |||
750 | /** |
||
751 | * Set filename |
||
752 | * |
||
753 | * @param mixed $value |
||
754 | * @param bool $overwrite [optional] Default overwrite is true |
||
755 | * @param string $separator [optional] Default separator is an underscore '_' |
||
756 | * @return void |
||
757 | */ |
||
758 | public function setFilename($value, $overwrite = true, $separator = '_') |
||
786 | |||
787 | /** |
||
788 | * Set the save path directory |
||
789 | * |
||
790 | * @param string $savePath Save Path |
||
791 | * @throws Exception |
||
792 | */ |
||
793 | public function setSavePath($savePath) |
||
806 | |||
807 | /** |
||
808 | * Set property |
||
809 | * |
||
810 | * @param string $element The element name you want to set, f.e.: name, email, phoneNumber, ... |
||
811 | * @param string $key |
||
812 | * @param string $value |
||
813 | * @return void |
||
814 | */ |
||
815 | private function setProperty($element, $key, $value) |
||
832 | |||
833 | /** |
||
834 | * Checks if we should return vcard in cal wrapper |
||
835 | * |
||
836 | * @return bool |
||
837 | */ |
||
838 | protected function shouldAttachmentBeCal() |
||
848 | } |
||
849 |
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: