Complex classes like Pass 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 Pass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Pass implements PassInterface |
||
31 | { |
||
32 | /** |
||
33 | * Serial number that uniquely identifies the pass. |
||
34 | * No two passes with the same pass type identifier |
||
35 | * may have the same serial number. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $serialNumber; |
||
40 | |||
41 | /** |
||
42 | * Brief description of the pass, |
||
43 | * used by the iOS accessibility technologies. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $description; |
||
48 | |||
49 | /** |
||
50 | * Version of the file format. |
||
51 | * The value must be 1. |
||
52 | * |
||
53 | * @var int |
||
54 | */ |
||
55 | protected $formatVersion = 1; |
||
56 | |||
57 | /** |
||
58 | * Pass type |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $type; |
||
63 | |||
64 | /** |
||
65 | * Pass structure |
||
66 | * |
||
67 | * @var Structure |
||
68 | */ |
||
69 | protected $structure; |
||
70 | |||
71 | /** |
||
72 | * Pass images |
||
73 | * |
||
74 | * @var ImageInterface[] |
||
75 | */ |
||
76 | protected $images = array(); |
||
77 | |||
78 | /** |
||
79 | * Beacons where the pass is relevant. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $beacons = array(); |
||
84 | |||
85 | /** |
||
86 | * A list of iTunes Store item identifiers (also known as Adam IDs) for the |
||
87 | * associated apps. |
||
88 | * |
||
89 | * Only one item in the list is used—the first item identifier for an app |
||
90 | * compatible with the current device. If the app is not installed, the |
||
91 | * link opens the App Store and shows the app. If the app is already |
||
92 | * installed, the link launches the app. |
||
93 | * |
||
94 | * @var int[] |
||
95 | */ |
||
96 | protected $associatedStoreIdentifiers = array(); |
||
97 | |||
98 | /** |
||
99 | * Locations where the pass is relevant. |
||
100 | * For example, the location of your store. |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $locations = array(); |
||
105 | |||
106 | /** |
||
107 | * List of localizations |
||
108 | * |
||
109 | * @var LocalizationInterface[] |
||
110 | */ |
||
111 | protected $localizations = array(); |
||
112 | |||
113 | /** |
||
114 | * Date and time when the pass becomes relevant. |
||
115 | * For example, the start time of a movie. |
||
116 | * |
||
117 | * @var DateTime |
||
118 | */ |
||
119 | protected $relevantDate; |
||
120 | |||
121 | /** |
||
122 | * Maximum distance in meters from a relevant latitude and longitude that |
||
123 | * the pass is relevant. This number is compared to the pass’s default |
||
124 | * distance and the smaller value is used. |
||
125 | * Available in iOS 7.0. |
||
126 | * @var int |
||
127 | */ |
||
128 | protected $maxDistance; |
||
129 | |||
130 | /** |
||
131 | * Barcodes available to be displayed of iOS 9 and later. The system uses |
||
132 | * the first valid barcode in the array. |
||
133 | * @var BarcodeInterface[] |
||
134 | */ |
||
135 | protected $barcodes = array(); |
||
136 | |||
137 | /** |
||
138 | * Barcode to be displayed for iOS 8 and earlier. |
||
139 | * @var BarcodeInterface |
||
140 | */ |
||
141 | protected $barcode; |
||
142 | |||
143 | /** |
||
144 | * Background color of the pass, specified as an CSS-style RGB triple. |
||
145 | * |
||
146 | * @var string rgb(23, 187, 82) |
||
147 | */ |
||
148 | protected $backgroundColor; |
||
149 | |||
150 | /** |
||
151 | * Foreground color of the pass, specified as a CSS-style RGB triple. |
||
152 | * |
||
153 | * @var string rgb(100, 10, 110) |
||
154 | */ |
||
155 | protected $foregroundColor; |
||
156 | |||
157 | /** |
||
158 | * Identifier used to group related passes. |
||
159 | * If a grouping identifier is specified, passes with the same style, pass type identifier, |
||
160 | * and grouping identifier are displayed as a group. Otherwise, passes are grouped automatically. |
||
161 | * |
||
162 | * @var string |
||
163 | */ |
||
164 | protected $groupingIdentifier; |
||
165 | |||
166 | /** |
||
167 | * Color of the label text, specified as a CSS-style RGB triple. |
||
168 | * |
||
169 | * @var string rgb(255, 255, 255) |
||
170 | */ |
||
171 | protected $labelColor; |
||
172 | |||
173 | /** |
||
174 | * Text displayed next to the logo on the pass. |
||
175 | * |
||
176 | * @var string |
||
177 | */ |
||
178 | protected $logoText; |
||
179 | |||
180 | /** |
||
181 | * If true, the strip image is displayed without a shine effect. |
||
182 | * |
||
183 | * @var string The default value is false |
||
184 | */ |
||
185 | protected $suppressStripShine; |
||
186 | |||
187 | /** |
||
188 | * The authentication token to use with the web service. |
||
189 | * The token must be 16 characters or longer. |
||
190 | * |
||
191 | * @var string |
||
192 | */ |
||
193 | protected $authenticationToken; |
||
194 | |||
195 | /** |
||
196 | * The URL of a web service that conforms to the API described in Passbook Web Service Reference. |
||
197 | * http://developer.apple.com/library/ios/documentation/PassKit/Reference/PassKit_WebService/WebService.html#//apple_ref/doc/uid/TP40011988 |
||
198 | * |
||
199 | * @var string |
||
200 | */ |
||
201 | protected $webServiceURL; |
||
202 | |||
203 | /** |
||
204 | * Pass type identifier |
||
205 | * |
||
206 | * @var string |
||
207 | */ |
||
208 | protected $passTypeIdentifier; |
||
209 | |||
210 | /** |
||
211 | * Team identifier |
||
212 | * |
||
213 | * @var string |
||
214 | */ |
||
215 | protected $teamIdentifier; |
||
216 | |||
217 | /** |
||
218 | * Organization name |
||
219 | * |
||
220 | * @var string |
||
221 | */ |
||
222 | protected $organizationName; |
||
223 | |||
224 | /** |
||
225 | * Date and time when the pass expires. |
||
226 | * |
||
227 | * @var DateTime |
||
228 | */ |
||
229 | protected $expirationDate; |
||
230 | |||
231 | /** |
||
232 | * Indicates that the pass is void—for example, a one time use coupon that has been redeemed. The default value is |
||
233 | * false. |
||
234 | * |
||
235 | * @var boolean |
||
236 | */ |
||
237 | protected $voided; |
||
238 | |||
239 | /** |
||
240 | * |
||
241 | * A URL to be passed to the associated app when launching it. |
||
242 | * The app receives this URL in the application:didFinishLaunchingWithOptions: and application:handleOpenURL: |
||
243 | * methods of its app delegate. If this key is present, the associatedStoreIdentifiers key must also be present. |
||
244 | * |
||
245 | * @var string |
||
246 | */ |
||
247 | protected $appLaunchURL; |
||
248 | |||
249 | /** |
||
250 | * Pass userInfo |
||
251 | * |
||
252 | * @var mixed |
||
253 | */ |
||
254 | protected $userInfo; |
||
255 | |||
256 | public function __construct($serialNumber, $description) |
||
262 | |||
263 | public function toArray() |
||
333 | |||
334 | /** |
||
335 | * {@inheritdoc} |
||
336 | */ |
||
337 | public function setSerialNumber($serialNumber) |
||
343 | |||
344 | /** |
||
345 | * {@inheritdoc} |
||
346 | */ |
||
347 | public function getSerialNumber() |
||
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | */ |
||
355 | public function setDescription($description) |
||
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | */ |
||
365 | public function getDescription() |
||
369 | |||
370 | /** |
||
371 | * {@inheritdoc} |
||
372 | */ |
||
373 | public function getFormatVersion() |
||
377 | |||
378 | /** |
||
379 | * {@inheritdoc} |
||
380 | */ |
||
381 | public function setFormatVersion($formatVersion) |
||
387 | |||
388 | /** |
||
389 | * {@inheritdoc} |
||
390 | */ |
||
391 | public function getType() |
||
395 | |||
396 | /** |
||
397 | * {@inheritdoc} |
||
398 | */ |
||
399 | public function setType($type) |
||
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | */ |
||
409 | public function setStructure(StructureInterface $structure) |
||
415 | |||
416 | /** |
||
417 | * {@inheritdoc} |
||
418 | */ |
||
419 | public function getStructure() |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | public function addImage(ImageInterface $image) |
||
433 | |||
434 | /** |
||
435 | * {@inheritdoc} |
||
436 | */ |
||
437 | public function getImages() |
||
441 | |||
442 | /** |
||
443 | * {@inheritdoc} |
||
444 | */ |
||
445 | public function addLocalization(LocalizationInterface $localization) |
||
451 | |||
452 | /** |
||
453 | * {@inheritdoc} |
||
454 | */ |
||
455 | public function getLocalizations() |
||
459 | |||
460 | /** |
||
461 | * {@inheritdoc} |
||
462 | */ |
||
463 | public function addAssociatedStoreIdentifier($associatedStoreIdentifier) |
||
469 | |||
470 | /** |
||
471 | * {@inheritdoc} |
||
472 | */ |
||
473 | public function getAssociatedStoreIdentifiers() |
||
477 | |||
478 | /** |
||
479 | * {@inheritdoc} |
||
480 | */ |
||
481 | public function addLocation(LocationInterface $location) |
||
487 | |||
488 | /** |
||
489 | * {@inheritdoc} |
||
490 | */ |
||
491 | public function getLocations() |
||
495 | |||
496 | /** |
||
497 | * {@inheritdoc} |
||
498 | */ |
||
499 | public function addBeacon(BeaconInterface $beacon) |
||
505 | |||
506 | /** |
||
507 | * {@inheritdoc} |
||
508 | */ |
||
509 | public function getBeacons() |
||
513 | |||
514 | /** |
||
515 | * {@inheritdoc} |
||
516 | */ |
||
517 | public function setRelevantDate(\DateTime $relevantDate) |
||
523 | |||
524 | /** |
||
525 | * {@inheritdoc} |
||
526 | */ |
||
527 | public function getRelevantDate() |
||
531 | |||
532 | /** |
||
533 | * {@inheritdoc} |
||
534 | */ |
||
535 | public function setMaxDistance($maxDistance) |
||
541 | |||
542 | /** |
||
543 | * {@inheritdoc} |
||
544 | */ |
||
545 | public function getMaxDistance() |
||
549 | |||
550 | /** |
||
551 | * {@inheritdoc} |
||
552 | */ |
||
553 | public function setBarcode(BarcodeInterface $barcode) |
||
560 | |||
561 | /** |
||
562 | * {@inheritdoc} |
||
563 | */ |
||
564 | public function getBarcode() |
||
568 | |||
569 | /** |
||
570 | * {@inheritdoc} |
||
571 | */ |
||
572 | public function addBarcode(BarcodeInterface $barcode) |
||
582 | |||
583 | /** |
||
584 | * {@inheritdoc} |
||
585 | */ |
||
586 | public function getBarcodes() |
||
590 | |||
591 | /** |
||
592 | * {@inheritdoc} |
||
593 | */ |
||
594 | public function setBackgroundColor($backgroundColor) |
||
600 | |||
601 | /** |
||
602 | * {@inheritdoc} |
||
603 | */ |
||
604 | public function getBackgroundColor() |
||
608 | |||
609 | /** |
||
610 | * {@inheritdoc} |
||
611 | */ |
||
612 | public function setForegroundColor($foregroundColor) |
||
618 | |||
619 | /** |
||
620 | * {@inheritdoc} |
||
621 | */ |
||
622 | public function getForegroundColor() |
||
626 | |||
627 | /** |
||
628 | * {@inheritdoc} |
||
629 | */ |
||
630 | public function setGroupingIdentifier($groupingIdentifier) |
||
636 | |||
637 | /** |
||
638 | * {@inheritdoc} |
||
639 | */ |
||
640 | public function getGroupingIdentifier() |
||
644 | |||
645 | /** |
||
646 | * {@inheritdoc} |
||
647 | */ |
||
648 | public function setLabelColor($labelColor) |
||
654 | |||
655 | /** |
||
656 | * {@inheritdoc} |
||
657 | */ |
||
658 | public function getLabelColor() |
||
662 | |||
663 | /** |
||
664 | * {@inheritdoc} |
||
665 | */ |
||
666 | public function setLogoText($logoText) |
||
672 | |||
673 | /** |
||
674 | * {@inheritdoc} |
||
675 | */ |
||
676 | public function getLogoText() |
||
680 | |||
681 | /** |
||
682 | * {@inheritdoc} |
||
683 | */ |
||
684 | public function setSuppressStripShine($suppressStripShine) |
||
690 | |||
691 | /** |
||
692 | * {@inheritdoc} |
||
693 | */ |
||
694 | public function getSuppressStripShine() |
||
698 | |||
699 | /** |
||
700 | * {@inheritdoc} |
||
701 | */ |
||
702 | public function setAuthenticationToken($authenticationToken) |
||
708 | |||
709 | /** |
||
710 | * {@inheritdoc} |
||
711 | */ |
||
712 | public function getAuthenticationToken() |
||
716 | |||
717 | /** |
||
718 | * {@inheritdoc} |
||
719 | */ |
||
720 | public function setWebServiceURL($webServiceURL) |
||
726 | |||
727 | /** |
||
728 | * {@inheritdoc} |
||
729 | */ |
||
730 | public function getWebServiceURL() |
||
734 | |||
735 | /** |
||
736 | * {@inheritdoc} |
||
737 | */ |
||
738 | public function setPassTypeIdentifier($passTypeIdentifier) |
||
744 | |||
745 | /** |
||
746 | * {@inheritdoc} |
||
747 | */ |
||
748 | public function getPassTypeIdentifier() |
||
752 | |||
753 | /** |
||
754 | * {@inheritdoc} |
||
755 | */ |
||
756 | public function setTeamIdentifier($teamIdentifier) |
||
762 | |||
763 | /** |
||
764 | * {@inheritdoc} |
||
765 | */ |
||
766 | public function getTeamIdentifier() |
||
770 | |||
771 | /** |
||
772 | * {@inheritdoc} |
||
773 | */ |
||
774 | public function setOrganizationName($organizationName) |
||
780 | |||
781 | /** |
||
782 | * {@inheritdoc} |
||
783 | */ |
||
784 | public function getOrganizationName() |
||
788 | |||
789 | /** |
||
790 | * {@inheritdoc} |
||
791 | */ |
||
792 | public function setExpirationDate(\DateTime $expirationDate) |
||
798 | |||
799 | /** |
||
800 | * {@inheritdoc} |
||
801 | */ |
||
802 | public function getExpirationDate() |
||
806 | |||
807 | /** |
||
808 | * {@inheritdoc} |
||
809 | */ |
||
810 | public function setVoided($voided) |
||
816 | |||
817 | /** |
||
818 | * {@inheritdoc} |
||
819 | */ |
||
820 | public function getVoided() |
||
824 | |||
825 | /** |
||
826 | * {@inheritdoc} |
||
827 | */ |
||
828 | public function setAppLaunchURL($appLaunchURL) |
||
834 | |||
835 | /** |
||
836 | * {@inheritdoc} |
||
837 | */ |
||
838 | public function getAppLaunchURL() |
||
842 | |||
843 | /** |
||
844 | * {@inheritdoc} |
||
845 | */ |
||
846 | public function setUserInfo($userInfo) { |
||
851 | |||
852 | /** |
||
853 | * {@inheritdoc} |
||
854 | */ |
||
855 | public function getUserInfo() { |
||
858 | |||
859 | } |
||
860 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.