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 |
||
| 17 | class VCard |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * definedElements |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private $definedElements; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Filename |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $filename; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Save Path |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $savePath = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Multiple properties for element allowed |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $multiplePropertiesForElementAllowed = [ |
||
| 46 | 'email', |
||
| 47 | 'address', |
||
| 48 | 'phoneNumber', |
||
| 49 | 'url' |
||
| 50 | ]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Properties |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $properties; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Index |
||
| 61 | * |
||
| 62 | * @var integer |
||
| 63 | */ |
||
| 64 | private $index = 0; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Default Charset |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $charset = 'utf-8'; |
||
| 72 | |||
| 73 | public function new() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Add address |
||
| 79 | * |
||
| 80 | * @param string [optional] $name |
||
| 81 | * @param string [optional] $extended |
||
| 82 | * @param string [optional] $street |
||
| 83 | * @param string [optional] $city |
||
| 84 | * @param string [optional] $region |
||
| 85 | * @param string [optional] $zip |
||
| 86 | * @param string [optional] $country |
||
| 87 | * @param string [optional] $type |
||
| 88 | * $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK |
||
| 89 | * or any combination of these: e.g. "WORK;PARCEL;POSTAL" |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | public function addAddress( |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Add birthday |
||
| 117 | * |
||
| 118 | * @param string $date Format is YYYY-MM-DD |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function addBirthday($date) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Add company |
||
| 134 | * |
||
| 135 | * @param string $company |
||
| 136 | * @param string $department |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function addCompany($company, $department = '') |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Add email |
||
| 158 | * |
||
| 159 | * @param string $address The e-mail address |
||
| 160 | * @param string [optional] $type The type of the email address |
||
| 161 | * $type may be PREF | WORK | HOME |
||
| 162 | * or any combination of these: e.g. "PREF;WORK" |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | public function addEmail($address, $type = '') |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Add jobtitle |
||
| 178 | * |
||
| 179 | * @param string $jobtitle The jobtitle for the person. |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function addJobtitle($jobtitle) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Add role |
||
| 195 | * |
||
| 196 | * @param string $role The role for the person. |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function addRole($role) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Add a photo or logo (depending on property name) |
||
| 212 | * |
||
| 213 | * @param string $property LOGO|PHOTO |
||
| 214 | * @param string $url image url or filename |
||
| 215 | * @param bool $include Do we include the image in our vcard or not? |
||
| 216 | * @param string $element The name of the element to set |
||
| 217 | */ |
||
| 218 | private function addMedia($property, $url, $include = true, $element) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Add name |
||
| 271 | * |
||
| 272 | * @param string [optional] $lastName |
||
| 273 | * @param string [optional] $firstName |
||
| 274 | * @param string [optional] $additional |
||
| 275 | * @param string [optional] $prefix |
||
| 276 | * @param string [optional] $suffix |
||
| 277 | * @return $this |
||
| 278 | */ |
||
| 279 | public function addName( |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Add note |
||
| 321 | * |
||
| 322 | * @param string $note |
||
| 323 | * @return $this |
||
| 324 | */ |
||
| 325 | public function addNote($note) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Add categories |
||
| 338 | * |
||
| 339 | * @param array $categories |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | public function addCategories($categories) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Add phone number |
||
| 355 | * |
||
| 356 | * @param string $number |
||
| 357 | * @param string [optional] $type |
||
| 358 | * Type may be PREF | WORK | HOME | VOICE | FAX | MSG | |
||
| 359 | * CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO |
||
| 360 | * or any senseful combination, e.g. "PREF;WORK;VOICE" |
||
| 361 | * @return $this |
||
| 362 | */ |
||
| 363 | public function addPhoneNumber($number, $type = '') |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Add Logo |
||
| 376 | * |
||
| 377 | * @param string $url image url or filename |
||
| 378 | * @param bool $include Include the image in our vcard? |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | public function addLogo($url, $include = true) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Add Photo |
||
| 395 | * |
||
| 396 | * @param string $url image url or filename |
||
| 397 | * @param bool $include Include the image in our vcard? |
||
| 398 | * @return $this |
||
| 399 | */ |
||
| 400 | public function addPhoto($url, $include = true) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Add URL |
||
| 414 | * |
||
| 415 | * @param string $url |
||
| 416 | * @param string [optional] $type Type may be WORK | HOME |
||
| 417 | * @return $this |
||
| 418 | */ |
||
| 419 | public function addURL($url, $type = '') |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Build VCard (.vcf) |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | public function buildVCard() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Build VCalender (.ics) - Safari (< iOS 8) can not open .vcf files, so we have build a workaround. |
||
| 463 | * |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | public function buildVCalendar() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Returns the browser user agent string. |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | protected function getUserAgent() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Decode |
||
| 520 | * |
||
| 521 | * @param string $value The value to decode |
||
| 522 | * @return string decoded |
||
| 523 | */ |
||
| 524 | private function decode($value) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Download a vcard or vcal file to the browser. |
||
| 532 | */ |
||
| 533 | public function download() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Fold a line according to RFC2425 section 5.8.1. |
||
| 548 | * |
||
| 549 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.1 |
||
| 550 | * @param string $text |
||
| 551 | * @return mixed |
||
| 552 | */ |
||
| 553 | protected function fold($text) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Escape newline characters according to RFC2425 section 5.8.4. |
||
| 565 | * |
||
| 566 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.4 |
||
| 567 | * @param string $text |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | protected function escape($text) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Get output as string |
||
| 580 | * @deprecated in the future |
||
| 581 | * |
||
| 582 | * @return string |
||
| 583 | */ |
||
| 584 | public function get() |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Get charset |
||
| 591 | * |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | public function getCharset() |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Get charset string |
||
| 601 | * |
||
| 602 | * @return string |
||
| 603 | */ |
||
| 604 | public function getCharsetString() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Get content type |
||
| 616 | * |
||
| 617 | * @return string |
||
| 618 | */ |
||
| 619 | public function getContentType() |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Get filename |
||
| 627 | * |
||
| 628 | * @return string |
||
| 629 | */ |
||
| 630 | public function getFilename() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Get file extension |
||
| 641 | * |
||
| 642 | * @return string |
||
| 643 | */ |
||
| 644 | public function getFileExtension() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Get headers |
||
| 652 | * |
||
| 653 | * @param bool $asAssociative |
||
| 654 | * @return array |
||
| 655 | */ |
||
| 656 | public function getHeaders($asAssociative) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Get output as string |
||
| 682 | * iOS devices (and safari < iOS 8 in particular) can not read .vcf (= vcard) files. |
||
| 683 | * So I build a workaround to build a .ics (= vcalender) file. |
||
| 684 | * |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | public function getOutput() |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Get propertie |
||
| 697 | * |
||
| 698 | * @return array |
||
| 699 | */ |
||
| 700 | public function getProperty($index = -1) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Get properties |
||
| 709 | * |
||
| 710 | * @return array |
||
| 711 | */ |
||
| 712 | public function getProperties() |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Has property |
||
| 719 | * |
||
| 720 | * @param string $key |
||
| 721 | * @return bool |
||
| 722 | */ |
||
| 723 | public function hasProperty($key) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Is iOS - Check if the user is using an iOS-device |
||
| 738 | * |
||
| 739 | * @return bool |
||
| 740 | */ |
||
| 741 | public function isIOS() |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Is iOS less than 7 (should cal wrapper be returned) |
||
| 751 | * |
||
| 752 | * @return bool |
||
| 753 | */ |
||
| 754 | public function isIOS7() |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Save to a file |
||
| 761 | * |
||
| 762 | * @return void |
||
| 763 | */ |
||
| 764 | public function save() |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Set charset |
||
| 781 | * |
||
| 782 | * @param mixed $charset |
||
| 783 | * @return void |
||
| 784 | */ |
||
| 785 | public function setCharset($charset) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Set filename |
||
| 792 | * |
||
| 793 | * @param mixed $value |
||
| 794 | * @param bool $overwrite [optional] Default overwrite is true |
||
| 795 | * @param string $separator [optional] Default separator is an underscore '_' |
||
| 796 | * @return void |
||
| 797 | */ |
||
| 798 | public function setFilename($value, $overwrite = true, $separator = '_') |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Set the save path directory |
||
| 829 | * |
||
| 830 | * @param string $savePath Save Path |
||
| 831 | * @throws VCardException |
||
| 832 | */ |
||
| 833 | public function setSavePath($savePath) |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Set property |
||
| 849 | * |
||
| 850 | * @param string $element The element name you want to set, f.e.: name, email, phoneNumber, ... |
||
| 851 | * @param string $key |
||
| 852 | * @param string $value |
||
| 853 | * @throws VCardException |
||
| 854 | */ |
||
| 855 | private function setProperty($element, $key, $value) |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Checks if we should return vcard in cal wrapper |
||
| 875 | * |
||
| 876 | * @return bool |
||
| 877 | */ |
||
| 878 | protected function shouldAttachmentBeCal() |
||
| 888 | } |
||
| 889 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: