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, $department = '') |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Add email |
||
| 148 | * |
||
| 149 | * @param string $address The e-mail address |
||
| 150 | * @param string [optional] $type The type of the email address |
||
| 151 | * $type may be PREF | WORK | HOME |
||
| 152 | * or any combination of these: e.g. "PREF;WORK" |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | public function addEmail($address, $type = '') |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Add jobtitle |
||
| 168 | * |
||
| 169 | * @param string $jobtitle The jobtitle for the person. |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function addJobtitle($jobtitle) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Add role |
||
| 185 | * |
||
| 186 | * @param string $role The role for the person. |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function addRole($role) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Add a photo or logo (depending on property name) |
||
| 202 | * |
||
| 203 | * @param string $property LOGO|PHOTO |
||
| 204 | * @param string $url image url or filename |
||
| 205 | * @param bool $include Do we include the image in our vcard or not? |
||
| 206 | * @throws VCardMediaException if file is empty or not an image file |
||
| 207 | */ |
||
| 208 | private function addMedia($property, $url, $include = true, $element) |
||
| 209 | { |
||
| 210 | if ($include) { |
||
| 211 | $value = file_get_contents($url); |
||
| 212 | |||
| 213 | if (!$value) { |
||
| 214 | throw new VCardMediaException('Nothing returned from URL.'); |
||
| 215 | } |
||
| 216 | |||
| 217 | $value = base64_encode($value); |
||
| 218 | $mimetype = mime_content_type($url); |
||
| 219 | |||
| 220 | if (preg_match('/^image\//', $mimetype) !== 1) { |
||
| 221 | throw new VCardMediaException('Returned data aren\'t an image.'); |
||
| 222 | } |
||
| 223 | |||
| 224 | $type = strtoupper(str_replace('image/', '', $mimetype)); |
||
| 225 | |||
| 226 | $property .= ";ENCODING=b;TYPE=" . $type; |
||
| 227 | } else { |
||
| 228 | if (filter_var($url, FILTER_VALIDATE_URL) !== false) { |
||
| 229 | $propertySuffix = ';VALUE=URL'; |
||
| 230 | |||
| 231 | $headers = get_headers($url); |
||
| 232 | |||
| 233 | $imageTypeMatched = false; |
||
| 234 | $fileType = null; |
||
| 235 | |||
| 236 | foreach ($headers as $header) { |
||
| 237 | if (preg_match('/Content-Type:\simage\/([a-z]+)/i', $header, $m)) { |
||
| 238 | $fileType = $m[1]; |
||
| 239 | $imageTypeMatched = true; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | if (!$imageTypeMatched) { |
||
| 244 | throw new VCardMediaException('Returned data isn\'t an image.'); |
||
| 245 | } |
||
| 246 | |||
| 247 | $propertySuffix .= ';TYPE=' . strtoupper($fileType); |
||
| 248 | |||
| 249 | $property = $property . $propertySuffix; |
||
| 250 | $value = $url; |
||
| 251 | } else { |
||
| 252 | $value = $url; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | $this->setProperty( |
||
| 257 | $element, |
||
| 258 | $property, |
||
| 259 | $value |
||
| 260 | ); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Add name |
||
| 265 | * |
||
| 266 | * @param string [optional] $lastName |
||
| 267 | * @param string [optional] $firstName |
||
| 268 | * @param string [optional] $additional |
||
| 269 | * @param string [optional] $prefix |
||
| 270 | * @param string [optional] $suffix |
||
| 271 | * @return $this |
||
| 272 | */ |
||
| 273 | public function addName( |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Add note |
||
| 315 | * |
||
| 316 | * @param string $note |
||
| 317 | * @return $this |
||
| 318 | */ |
||
| 319 | public function addNote($note) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Add categories |
||
| 332 | * |
||
| 333 | * @param array $categories |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | public function addCategories($categories) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Add phone number |
||
| 349 | * |
||
| 350 | * @param string $number |
||
| 351 | * @param string [optional] $type |
||
| 352 | * Type may be PREF | WORK | HOME | VOICE | FAX | MSG | |
||
| 353 | * CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO |
||
| 354 | * or any senseful combination, e.g. "PREF;WORK;VOICE" |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | public function addPhoneNumber($number, $type = '') |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Add Logo |
||
| 370 | * |
||
| 371 | * @param string $url image url or filename |
||
| 372 | * @param bool $include Include the image in our vcard? |
||
| 373 | * @return $this |
||
| 374 | */ |
||
| 375 | public function addLogo($url, $include = true) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Add Photo |
||
| 389 | * |
||
| 390 | * @param string $url image url or filename |
||
| 391 | * @param bool $include Include the image in our vcard? |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | public function addPhoto($url, $include = true) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Add URL |
||
| 408 | * |
||
| 409 | * @param string $url |
||
| 410 | * @param string [optional] $type Type may be WORK | HOME |
||
| 411 | * @return $this |
||
| 412 | */ |
||
| 413 | public function addURL($url, $type = '') |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Build VCard (.vcf) |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function buildVCard() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Build VCalender (.ics) - Safari (< iOS 8) can not open .vcf files, so we have build a workaround. |
||
| 452 | * |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public function buildVCalendar() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Returns the browser user agent string. |
||
| 493 | * |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | protected function getUserAgent() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Decode |
||
| 509 | * |
||
| 510 | * @param string $value The value to decode |
||
| 511 | * @return string decoded |
||
| 512 | */ |
||
| 513 | private function decode($value) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Download a vcard or vcal file to the browser. |
||
| 521 | */ |
||
| 522 | public function download() |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Fold a line according to RFC2425 section 5.8.1. |
||
| 537 | * |
||
| 538 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.1 |
||
| 539 | * @param string $text |
||
| 540 | * @return mixed |
||
| 541 | */ |
||
| 542 | protected function fold($text) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * multibyte word chunk split |
||
| 554 | * @link http://php.net/manual/en/function.chunk-split.php#107711 |
||
| 555 | * |
||
| 556 | * @param string $body The string to be chunked. |
||
| 557 | * @param integer $chunklen The chunk length. |
||
| 558 | * @param string $end The line ending sequence. |
||
| 559 | * @return string Chunked string |
||
| 560 | */ |
||
| 561 | protected function chunk_split_unicode($body, $chunklen = 76, $end = "\r\n") |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Escape newline characters according to RFC2425 section 5.8.4. |
||
| 574 | * |
||
| 575 | * @link http://tools.ietf.org/html/rfc2425#section-5.8.4 |
||
| 576 | * @param string $text |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | protected function escape($text) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Get output as string |
||
| 589 | * @deprecated in the future |
||
| 590 | * |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | public function get() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Get charset |
||
| 600 | * |
||
| 601 | * @return string |
||
| 602 | */ |
||
| 603 | public function getCharset() |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Get charset string |
||
| 610 | * |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | public function getCharsetString() |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Get content type |
||
| 625 | * |
||
| 626 | * @return string |
||
| 627 | */ |
||
| 628 | public function getContentType() |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Get filename |
||
| 636 | * |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | public function getFilename() |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Get file extension |
||
| 650 | * |
||
| 651 | * @return string |
||
| 652 | */ |
||
| 653 | public function getFileExtension() |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Get headers |
||
| 661 | * |
||
| 662 | * @param bool $asAssociative |
||
| 663 | * @return array |
||
| 664 | */ |
||
| 665 | public function getHeaders($asAssociative) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Get output as string |
||
| 691 | * iOS devices (and safari < iOS 8 in particular) can not read .vcf (= vcard) files. |
||
| 692 | * So I build a workaround to build a .ics (= vcalender) file. |
||
| 693 | * |
||
| 694 | * @return string |
||
| 695 | */ |
||
| 696 | public function getOutput() |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Get properties |
||
| 706 | * |
||
| 707 | * @return array |
||
| 708 | */ |
||
| 709 | public function getProperties() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Has property |
||
| 716 | * |
||
| 717 | * @param string $key |
||
| 718 | * @return bool |
||
| 719 | */ |
||
| 720 | public function hasProperty($key) |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Is iOS - Check if the user is using an iOS-device |
||
| 735 | * |
||
| 736 | * @return bool |
||
| 737 | */ |
||
| 738 | public function isIOS() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Is iOS less than 7 (should cal wrapper be returned) |
||
| 748 | * |
||
| 749 | * @return bool |
||
| 750 | */ |
||
| 751 | public function isIOS7() |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Save to a file |
||
| 758 | * |
||
| 759 | * @return void |
||
| 760 | */ |
||
| 761 | public function save() |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Set charset |
||
| 778 | * |
||
| 779 | * @param mixed $charset |
||
| 780 | * @return void |
||
| 781 | */ |
||
| 782 | public function setCharset($charset) |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Set filename |
||
| 789 | * |
||
| 790 | * @param mixed $value |
||
| 791 | * @param bool $overwrite [optional] Default overwrite is true |
||
| 792 | * @param string $separator [optional] Default separator is an underscore '_' |
||
| 793 | * @return void |
||
| 794 | */ |
||
| 795 | public function setFilename($value, $overwrite = true, $separator = '_') |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Set the save path directory |
||
| 826 | * |
||
| 827 | * @param string $savePath Save Path |
||
| 828 | * @throws Exception |
||
| 829 | */ |
||
| 830 | public function setSavePath($savePath) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Set property |
||
| 846 | * |
||
| 847 | * @param string $element The element name you want to set, f.e.: name, email, phoneNumber, ... |
||
| 848 | * @param string $key |
||
| 849 | * @param string $value |
||
| 850 | * @throws Exception |
||
| 851 | */ |
||
| 852 | private function setProperty($element, $key, $value) |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Checks if we should return vcard in cal wrapper |
||
| 872 | * |
||
| 873 | * @return bool |
||
| 874 | */ |
||
| 875 | protected function shouldAttachmentBeCal() |
||
| 885 | } |
||
| 886 |
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: