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->getFilename() === 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) |
||
| 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 phone number |
||
| 327 | * |
||
| 328 | * @param string $number |
||
| 329 | * @param string [optional] $type |
||
| 330 | * Type may be PREF | WORK | HOME | VOICE | FAX | MSG | |
||
| 331 | * CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO |
||
| 332 | * or any senseful combination, e.g. "PREF;WORK;VOICE" |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | public function addPhoneNumber($number, $type = '') |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Add Logo |
||
| 348 | * |
||
| 349 | * @param string $url image url or filename |
||
| 350 | * @param bool $include Include the image in our vcard? |
||
| 351 | * @return $this |
||
| 352 | */ |
||
| 353 | public function addLogo($url, $include = true) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Add Photo |
||
| 367 | * |
||
| 368 | * @param string $url image url or filename |
||
| 369 | * @param bool $include Include the image in our vcard? |
||
| 370 | * @return $this |
||
| 371 | */ |
||
| 372 | public function addPhoto($url, $include = true) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Add URL |
||
| 386 | * |
||
| 387 | * @param string $url |
||
| 388 | * @param string [optional] $type Type may be WORK | HOME |
||
| 389 | * @return $this |
||
| 390 | */ |
||
| 391 | public function addURL($url, $type = '') |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Build VCard (.vcf) |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public function buildVCard() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Build VCalender (.ics) - Safari (< iOS 8) can not open .vcf files, so we have build a workaround. |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | public function buildVCalendar() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Returns the browser user agent string. |
||
| 471 | * |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | protected function getUserAgent() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Decode |
||
| 487 | * |
||
| 488 | * @param string $value The value to decode |
||
| 489 | * @return string decoded |
||
| 490 | */ |
||
| 491 | private function decode($value) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Download a vcard or vcal file to the browser. |
||
| 499 | */ |
||
| 500 | public function download() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Fold a line according to RFC2425 section 5.8.1. |
||
| 515 | * |
||
| 516 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.1 |
||
| 517 | * @param string $text |
||
| 518 | * @return mixed |
||
| 519 | */ |
||
| 520 | protected function fold($text) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Escape newline characters according to RFC2425 section 5.8.4. |
||
| 532 | * |
||
| 533 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.4 |
||
| 534 | * @param string $text |
||
| 535 | * @return string |
||
| 536 | */ |
||
| 537 | protected function escape($text) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get output as string |
||
| 547 | * @deprecated in the future |
||
| 548 | * |
||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | public function get() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Get charset |
||
| 558 | * |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public function getCharset() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get charset string |
||
| 568 | * |
||
| 569 | * @return string |
||
| 570 | */ |
||
| 571 | public function getCharsetString() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Get content type |
||
| 582 | * |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | public function getContentType() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Get filename |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | public function getFilename() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Get file extension |
||
| 603 | * |
||
| 604 | * @return string |
||
| 605 | */ |
||
| 606 | public function getFileExtension() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Get headers |
||
| 614 | * |
||
| 615 | * @param bool $asAssociative |
||
| 616 | * @return array |
||
| 617 | */ |
||
| 618 | public function getHeaders($asAssociative) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Get output as string |
||
| 644 | * iOS devices (and safari < iOS 8 in particular) can not read .vcf (= vcard) files. |
||
| 645 | * So I build a workaround to build a .ics (= vcalender) file. |
||
| 646 | * |
||
| 647 | * @return string |
||
| 648 | */ |
||
| 649 | public function getOutput() |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Get properties |
||
| 659 | * |
||
| 660 | * @return array |
||
| 661 | */ |
||
| 662 | public function getProperties() |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Has property |
||
| 669 | * |
||
| 670 | * @param string $key |
||
| 671 | * @return bool |
||
| 672 | */ |
||
| 673 | public function hasProperty($key) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Is iOS - Check if the user is using an iOS-device |
||
| 688 | * |
||
| 689 | * @return bool |
||
| 690 | */ |
||
| 691 | public function isIOS() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Is iOS less than 7 (should cal wrapper be returned) |
||
| 701 | * |
||
| 702 | * @return bool |
||
| 703 | */ |
||
| 704 | public function isIOS7() |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Save to a file |
||
| 711 | * |
||
| 712 | * @return void |
||
| 713 | */ |
||
| 714 | public function save() |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Set charset |
||
| 726 | * |
||
| 727 | * @param mixed $charset |
||
| 728 | * @return void |
||
| 729 | */ |
||
| 730 | public function setCharset($charset) |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Set filename |
||
| 737 | * |
||
| 738 | * @param mixed $value |
||
| 739 | * @param bool $overwrite [optional] Default overwrite is true |
||
| 740 | * @param string $separator [optional] Default separator is an underscore '_' |
||
| 741 | * @return void |
||
| 742 | */ |
||
| 743 | public function setFilename($value, $overwrite = true, $separator = '_') |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Set property |
||
| 774 | * |
||
| 775 | * @param string $element The element name you want to set, f.e.: name, email, phoneNumber, ... |
||
| 776 | * @param string $key |
||
| 777 | * @param string $value |
||
| 778 | * @return void |
||
| 779 | */ |
||
| 780 | private function setProperty($element, $key, $value) |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Checks if we should return vcard in cal wrapper |
||
| 800 | * |
||
| 801 | * @return bool |
||
| 802 | */ |
||
| 803 | protected function shouldAttachmentBeCal() |
||
| 813 | } |
||
| 814 |
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: