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 | * Multiple properties for element allowed |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private $multiplePropertiesForElementAllowed = array( |
||
| 41 | 'email', |
||
| 42 | 'address', |
||
| 43 | 'phoneNumber', |
||
| 44 | 'url' |
||
| 45 | ); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Properties |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $properties; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Default Charset |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | public $charset = 'utf-8'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Add address |
||
| 63 | * |
||
| 64 | * @param string [optional] $name |
||
| 65 | * @param string [optional] $extended |
||
| 66 | * @param string [optional] $street |
||
| 67 | * @param string [optional] $city |
||
| 68 | * @param string [optional] $region |
||
| 69 | * @param string [optional] $zip |
||
| 70 | * @param string [optional] $country |
||
| 71 | * @param string [optional] $type |
||
| 72 | * $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK |
||
| 73 | * or any combination of these: e.g. "WORK;PARCEL;POSTAL" |
||
| 74 | * @return $this |
||
| 75 | */ |
||
| 76 | public function addAddress( |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Add birthday |
||
| 101 | * |
||
| 102 | * @param string $date Format is YYYY-MM-DD |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function addBirthday($date) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Add company |
||
| 118 | * |
||
| 119 | * @param string $company |
||
| 120 | * @return $this |
||
| 121 | */ |
||
| 122 | public function addCompany($company) |
||
| 123 | { |
||
| 124 | $this->setProperty( |
||
| 125 | 'company', |
||
| 126 | 'ORG' . $this->getCharsetString(), |
||
| 127 | $company |
||
| 128 | ); |
||
| 129 | |||
| 130 | // if filename is empty, add to filename |
||
| 131 | if ($this->filename === null) { |
||
| 132 | $this->setFilename($company); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $this; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Add email |
||
| 140 | * |
||
| 141 | * @param string $address The e-mail address |
||
| 142 | * @param string [optional] $type The type of the email address |
||
| 143 | * $type may be PREF | WORK | HOME |
||
| 144 | * or any combination of these: e.g. "PREF;WORK" |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function addEmail($address, $type = '') |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Add jobtitle |
||
| 160 | * |
||
| 161 | * @param string $jobtitle The jobtitle for the person. |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | public function addJobtitle($jobtitle) |
||
| 165 | { |
||
| 166 | $this->setProperty( |
||
| 167 | 'jobtitle', |
||
| 168 | 'TITLE' . $this->getCharsetString(), |
||
| 169 | $jobtitle |
||
| 170 | ); |
||
| 171 | |||
| 172 | return $this; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Add role |
||
| 177 | * |
||
| 178 | * @param string $role The role for the person. |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function addRole($role) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Add a photo or logo (depending on property name) |
||
| 194 | * |
||
| 195 | * @param string $property LOGO|PHOTO |
||
| 196 | * @param string $url image url or filename |
||
| 197 | * @param bool $include Do we include the image in our vcard or not? |
||
| 198 | * @throws VCardMediaException if file is empty or not an image file |
||
| 199 | */ |
||
| 200 | private function addMedia($property, $url, $include = true, $element) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Add name |
||
| 260 | * |
||
| 261 | * @param string [optional] $lastName |
||
| 262 | * @param string [optional] $firstName |
||
| 263 | * @param string [optional] $additional |
||
| 264 | * @param string [optional] $prefix |
||
| 265 | * @param string [optional] $suffix |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function addName( |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Add note |
||
| 310 | * |
||
| 311 | * @param string $note |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function addNote($note) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Add categories |
||
| 327 | * |
||
| 328 | * @param array $categories |
||
| 329 | * @return $this |
||
| 330 | */ |
||
| 331 | public function addCategories($categories) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Add phone number |
||
| 344 | * |
||
| 345 | * @param string $number |
||
| 346 | * @param string [optional] $type |
||
| 347 | * Type may be PREF | WORK | HOME | VOICE | FAX | MSG | |
||
| 348 | * CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO |
||
| 349 | * or any senseful combination, e.g. "PREF;WORK;VOICE" |
||
| 350 | * @return $this |
||
| 351 | */ |
||
| 352 | public function addPhoneNumber($number, $type = '') |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Add Logo |
||
| 365 | * |
||
| 366 | * @param string $url image url or filename |
||
| 367 | * @param bool $include Include the image in our vcard? |
||
| 368 | * @return $this |
||
| 369 | */ |
||
| 370 | public function addLogo($url, $include = true) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Add Photo |
||
| 384 | * |
||
| 385 | * @param string $url image url or filename |
||
| 386 | * @param bool $include Include the image in our vcard? |
||
| 387 | * @return $this |
||
| 388 | */ |
||
| 389 | public function addPhoto($url, $include = true) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Add URL |
||
| 403 | * |
||
| 404 | * @param string $url |
||
| 405 | * @param string [optional] $type Type may be WORK | HOME |
||
| 406 | * @return $this |
||
| 407 | */ |
||
| 408 | public function addURL($url, $type = '') |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Build VCard (.vcf) |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function buildVCard() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Build VCalender (.ics) - Safari (< iOS 8) can not open .vcf files, so we have build a workaround. |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | public function buildVCalendar() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Returns the browser user agent string. |
||
| 488 | * |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | protected function getUserAgent() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Decode |
||
| 504 | * |
||
| 505 | * @param string $value The value to decode |
||
| 506 | * @return string decoded |
||
| 507 | */ |
||
| 508 | private function decode($value) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Download a vcard or vcal file to the browser. |
||
| 516 | */ |
||
| 517 | public function download() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Fold a line according to RFC2425 section 5.8.1. |
||
| 532 | * |
||
| 533 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.1 |
||
| 534 | * @param string $text |
||
| 535 | * @return mixed |
||
| 536 | */ |
||
| 537 | protected function fold($text) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Escape newline characters according to RFC2425 section 5.8.4. |
||
| 549 | * |
||
| 550 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.4 |
||
| 551 | * @param string $text |
||
| 552 | * @return string |
||
| 553 | */ |
||
| 554 | protected function escape($text) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get output as string |
||
| 564 | * @deprecated in the future |
||
| 565 | * |
||
| 566 | * @return string |
||
| 567 | */ |
||
| 568 | public function get() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Get charset |
||
| 575 | * |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public function getCharset() |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Get charset string |
||
| 585 | * |
||
| 586 | * @return string |
||
| 587 | */ |
||
| 588 | public function getCharsetString() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Get content type |
||
| 599 | * |
||
| 600 | * @return string |
||
| 601 | */ |
||
| 602 | public function getContentType() |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Get filename |
||
| 610 | * |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | public function getFilename() |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Get file extension |
||
| 623 | * |
||
| 624 | * @return string |
||
| 625 | */ |
||
| 626 | public function getFileExtension() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Get headers |
||
| 634 | * |
||
| 635 | * @param bool $asAssociative |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | public function getHeaders($asAssociative) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Get output as string |
||
| 664 | * iOS devices (and safari < iOS 8 in particular) can not read .vcf (= vcard) files. |
||
| 665 | * So I build a workaround to build a .ics (= vcalender) file. |
||
| 666 | * |
||
| 667 | * @return string |
||
| 668 | */ |
||
| 669 | public function getOutput() |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Get properties |
||
| 679 | * |
||
| 680 | * @return array |
||
| 681 | */ |
||
| 682 | public function getProperties() |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Has property |
||
| 689 | * |
||
| 690 | * @param string $key |
||
| 691 | * @return bool |
||
| 692 | */ |
||
| 693 | public function hasProperty($key) |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Is iOS - Check if the user is using an iOS-device |
||
| 708 | * |
||
| 709 | * @return bool |
||
| 710 | */ |
||
| 711 | public function isIOS() |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Is iOS less than 7 (should cal wrapper be returned) |
||
| 721 | * |
||
| 722 | * @return bool |
||
| 723 | */ |
||
| 724 | public function isIOS7() |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Save to a file |
||
| 731 | * |
||
| 732 | * @return void |
||
| 733 | */ |
||
| 734 | public function save() |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Set charset |
||
| 746 | * |
||
| 747 | * @param mixed $charset |
||
| 748 | * @return void |
||
| 749 | */ |
||
| 750 | public function setCharset($charset) |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Set filename |
||
| 757 | * |
||
| 758 | * @param mixed $value |
||
| 759 | * @param bool $overwrite [optional] Default overwrite is true |
||
| 760 | * @param string $separator [optional] Default separator is an underscore '_' |
||
| 761 | * @return void |
||
| 762 | */ |
||
| 763 | public function setFilename($value, $overwrite = true, $separator = '_') |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Set property |
||
| 794 | * |
||
| 795 | * @param string $element The element name you want to set, f.e.: name, email, phoneNumber, ... |
||
| 796 | * @param string $key |
||
| 797 | * @param string $value |
||
| 798 | * @return void |
||
| 799 | */ |
||
| 800 | private function setProperty($element, $key, $value) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Checks if we should return vcard in cal wrapper |
||
| 820 | * |
||
| 821 | * @return bool |
||
| 822 | */ |
||
| 823 | protected function shouldAttachmentBeCal() |
||
| 833 | } |
||
| 834 |
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: