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',  | 
            ||
| 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 a photo or logo (depending on property name)  | 
            ||
| 177 | *  | 
            ||
| 178 | * @param string $property LOGO|PHOTO  | 
            ||
| 179 | * @param string $url image url or filename  | 
            ||
| 180 | * @param bool $include Do we include the image in our vcard or not?  | 
            ||
| 181 | * @throws VCardMediaException if file is empty or not an image file  | 
            ||
| 182 | */  | 
            ||
| 183 | private function addMedia($property, $url, $include = true, $element)  | 
            ||
| 228 | |||
| 229 | /**  | 
            ||
| 230 | * Add name  | 
            ||
| 231 | *  | 
            ||
| 232 | * @param string [optional] $lastName  | 
            ||
| 233 | * @param string [optional] $firstName  | 
            ||
| 234 | * @param string [optional] $additional  | 
            ||
| 235 | * @param string [optional] $prefix  | 
            ||
| 236 | * @param string [optional] $suffix  | 
            ||
| 237 | * @return $this  | 
            ||
| 238 | */  | 
            ||
| 239 | public function addName(  | 
            ||
| 278 | |||
| 279 | /**  | 
            ||
| 280 | * Add note  | 
            ||
| 281 | *  | 
            ||
| 282 | * @param string $note  | 
            ||
| 283 | * @return $this  | 
            ||
| 284 | */  | 
            ||
| 285 | public function addNote($note)  | 
            ||
| 295 | |||
| 296 | /**  | 
            ||
| 297 | * Add phone number  | 
            ||
| 298 | *  | 
            ||
| 299 | * @param string $number  | 
            ||
| 300 | * @param string [optional] $type  | 
            ||
| 301 | * Type may be PREF | WORK | HOME | VOICE | FAX | MSG |  | 
            ||
| 302 | * CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO  | 
            ||
| 303 | * or any senseful combination, e.g. "PREF;WORK;VOICE"  | 
            ||
| 304 | * @return $this  | 
            ||
| 305 | */  | 
            ||
| 306 | public function addPhoneNumber($number, $type = '')  | 
            ||
| 316 | |||
| 317 | /**  | 
            ||
| 318 | * Add Photo  | 
            ||
| 319 | *  | 
            ||
| 320 | * @param string $url image url or filename  | 
            ||
| 321 | * @param bool $include Include the image in our vcard?  | 
            ||
| 322 | * @return $this  | 
            ||
| 323 | */  | 
            ||
| 324 | public function addPhoto($url, $include = true)  | 
            ||
| 335 | |||
| 336 | /**  | 
            ||
| 337 | * Add URL  | 
            ||
| 338 | *  | 
            ||
| 339 | * @param string $url  | 
            ||
| 340 | * @param string [optional] $type Type may be WORK | HOME  | 
            ||
| 341 | * @return $this  | 
            ||
| 342 | */  | 
            ||
| 343 | public function addURL($url, $type = '')  | 
            ||
| 353 | |||
| 354 | /**  | 
            ||
| 355 | * Build VCard (.vcf)  | 
            ||
| 356 | *  | 
            ||
| 357 | * @return string  | 
            ||
| 358 | */  | 
            ||
| 359 | public function buildVCard()  | 
            ||
| 379 | |||
| 380 | /**  | 
            ||
| 381 | * Build VCalender (.ics) - Safari (< iOS 8) can not open .vcf files, so we have build a workaround.  | 
            ||
| 382 | *  | 
            ||
| 383 | * @return string  | 
            ||
| 384 | */  | 
            ||
| 385 | public function buildVCalendar()  | 
            ||
| 420 | |||
| 421 | /**  | 
            ||
| 422 | * Returns the browser user agent string.  | 
            ||
| 423 | *  | 
            ||
| 424 | * @return string  | 
            ||
| 425 | */  | 
            ||
| 426 | protected function getUserAgent()  | 
            ||
| 436 | |||
| 437 | /**  | 
            ||
| 438 | * Decode  | 
            ||
| 439 | *  | 
            ||
| 440 | * @param string $value The value to decode  | 
            ||
| 441 | * @return string decoded  | 
            ||
| 442 | */  | 
            ||
| 443 | private function decode($value)  | 
            ||
| 448 | |||
| 449 | /**  | 
            ||
| 450 | * Download a vcard or vcal file to the browser.  | 
            ||
| 451 | */  | 
            ||
| 452 | public function download()  | 
            ||
| 464 | |||
| 465 | /**  | 
            ||
| 466 | * Fold a line according to RFC2425 section 5.8.1.  | 
            ||
| 467 | *  | 
            ||
| 468 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.1  | 
            ||
| 469 | * @param string $text  | 
            ||
| 470 | * @return mixed  | 
            ||
| 471 | */  | 
            ||
| 472 | protected function fold($text)  | 
            ||
| 481 | |||
| 482 | /**  | 
            ||
| 483 | * Get output as string  | 
            ||
| 484 | * @deprecated in the future  | 
            ||
| 485 | *  | 
            ||
| 486 | * @return string  | 
            ||
| 487 | */  | 
            ||
| 488 | public function get()  | 
            ||
| 492 | |||
| 493 | /**  | 
            ||
| 494 | * Get charset  | 
            ||
| 495 | *  | 
            ||
| 496 | * @return string  | 
            ||
| 497 | */  | 
            ||
| 498 | public function getCharset()  | 
            ||
| 502 | |||
| 503 | /**  | 
            ||
| 504 | * Get content type  | 
            ||
| 505 | *  | 
            ||
| 506 | * @return string  | 
            ||
| 507 | */  | 
            ||
| 508 | public function getContentType()  | 
            ||
| 513 | |||
| 514 | /**  | 
            ||
| 515 | * Get filename  | 
            ||
| 516 | *  | 
            ||
| 517 | * @return string  | 
            ||
| 518 | */  | 
            ||
| 519 | public function getFilename()  | 
            ||
| 523 | |||
| 524 | /**  | 
            ||
| 525 | * Get file extension  | 
            ||
| 526 | *  | 
            ||
| 527 | * @return string  | 
            ||
| 528 | */  | 
            ||
| 529 | public function getFileExtension()  | 
            ||
| 534 | |||
| 535 | /**  | 
            ||
| 536 | * Get headers  | 
            ||
| 537 | *  | 
            ||
| 538 | * @param bool $asAssociative  | 
            ||
| 539 | * @return array  | 
            ||
| 540 | */  | 
            ||
| 541 | public function getHeaders($asAssociative)  | 
            ||
| 564 | |||
| 565 | /**  | 
            ||
| 566 | * Get output as string  | 
            ||
| 567 | * iOS devices (and safari < iOS 8 in particular) can not read .vcf (= vcard) files.  | 
            ||
| 568 | * So I build a workaround to build a .ics (= vcalender) file.  | 
            ||
| 569 | *  | 
            ||
| 570 | * @return string  | 
            ||
| 571 | */  | 
            ||
| 572 | public function getOutput()  | 
            ||
| 584 | |||
| 585 | /**  | 
            ||
| 586 | * Get properties  | 
            ||
| 587 | *  | 
            ||
| 588 | * @return array  | 
            ||
| 589 | */  | 
            ||
| 590 | public function getProperties()  | 
            ||
| 594 | |||
| 595 | /**  | 
            ||
| 596 | * Has property  | 
            ||
| 597 | *  | 
            ||
| 598 | * @param string $key  | 
            ||
| 599 | * @return bool  | 
            ||
| 600 | */  | 
            ||
| 601 | public function hasProperty($key)  | 
            ||
| 613 | |||
| 614 | /**  | 
            ||
| 615 | * Is iOS - Check if the user is using an iOS-device  | 
            ||
| 616 | *  | 
            ||
| 617 | * @return bool  | 
            ||
| 618 | */  | 
            ||
| 619 | public function isIOS()  | 
            ||
| 626 | |||
| 627 | /**  | 
            ||
| 628 | * Is iOS less than 7 (should cal wrapper be returned)  | 
            ||
| 629 | *  | 
            ||
| 630 | * @return bool  | 
            ||
| 631 | */  | 
            ||
| 632 | public function isIOS7()  | 
            ||
| 636 | |||
| 637 | /**  | 
            ||
| 638 | * Save to a file  | 
            ||
| 639 | *  | 
            ||
| 640 | * @return void  | 
            ||
| 641 | */  | 
            ||
| 642 | public function save()  | 
            ||
| 651 | |||
| 652 | /**  | 
            ||
| 653 | * Set charset  | 
            ||
| 654 | *  | 
            ||
| 655 | * @param mixed $charset  | 
            ||
| 656 | * @return void  | 
            ||
| 657 | */  | 
            ||
| 658 | public function setCharset($charset)  | 
            ||
| 662 | |||
| 663 | /**  | 
            ||
| 664 | * Set filename  | 
            ||
| 665 | *  | 
            ||
| 666 | * @param mixed $value  | 
            ||
| 667 | * @param bool $overwrite [optional] Default overwrite is true  | 
            ||
| 668 | * @param string $separator [optional] Default separator is an underscore '_'  | 
            ||
| 669 | * @return void  | 
            ||
| 670 | */  | 
            ||
| 671 | public function setFilename($value, $overwrite = true, $separator = '_')  | 
            ||
| 699 | |||
| 700 | /**  | 
            ||
| 701 | * Set property  | 
            ||
| 702 | *  | 
            ||
| 703 | * @param string $element The element name you want to set, f.e.: name, email, phoneNumber, ...  | 
            ||
| 704 | * @param string $key  | 
            ||
| 705 | * @param string $value  | 
            ||
| 706 | * @return void  | 
            ||
| 707 | */  | 
            ||
| 708 | private function setProperty($element, $key, $value)  | 
            ||
| 725 | |||
| 726 | /**  | 
            ||
| 727 | * Checks if we should return vcard in cal wrapper  | 
            ||
| 728 | *  | 
            ||
| 729 | * @return bool  | 
            ||
| 730 | */  | 
            ||
| 731 | protected function shouldAttachmentBeCal()  | 
            ||
| 741 | }  | 
            ||
| 742 | 
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: