Complex classes like Passbook 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 Passbook, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | abstract class Passbook |
||
20 | { |
||
21 | protected const TYPE = null; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $formatVersion = 1; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $passTypeIdentifier; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $serialNumber; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $teamIdentifier; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $organizationName; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | private $description; |
||
52 | |||
53 | /** |
||
54 | * @var string|null |
||
55 | */ |
||
56 | private $logoText; |
||
57 | |||
58 | /** |
||
59 | * @var Barcode[] |
||
60 | */ |
||
61 | private $barcodes = []; |
||
62 | |||
63 | /** |
||
64 | * @var DateTimeImmutable|null |
||
65 | */ |
||
66 | private $relevantDate; |
||
67 | |||
68 | /** |
||
69 | * @var string|null |
||
70 | */ |
||
71 | private $appLaunchURL; |
||
72 | |||
73 | /** |
||
74 | * @var array|null |
||
75 | */ |
||
76 | private $associatedStoreIdentifiers; |
||
77 | |||
78 | /** |
||
79 | * @var string|null |
||
80 | */ |
||
81 | private $userInfo; |
||
82 | |||
83 | /** |
||
84 | * @var DateTimeImmutable|null |
||
85 | */ |
||
86 | private $expirationDate; |
||
87 | |||
88 | /** |
||
89 | * @var boolean|null |
||
90 | */ |
||
91 | private $voided; |
||
92 | |||
93 | /** |
||
94 | * @var Location[]|null |
||
95 | */ |
||
96 | private $locations; |
||
97 | |||
98 | /** |
||
99 | * @var int|null |
||
100 | */ |
||
101 | private $maxDistance; |
||
102 | |||
103 | /** |
||
104 | * @var string|null |
||
105 | */ |
||
106 | private $webServiceURL; |
||
107 | |||
108 | /** |
||
109 | * @var string|null |
||
110 | */ |
||
111 | private $authenticationToken; |
||
112 | |||
113 | /** |
||
114 | * @var Color|null |
||
115 | */ |
||
116 | private $foregroundColor; |
||
117 | |||
118 | /** |
||
119 | * @var Color|null |
||
120 | */ |
||
121 | private $backgroundColor; |
||
122 | |||
123 | /** |
||
124 | * @var Color|null |
||
125 | */ |
||
126 | private $labelColor; |
||
127 | |||
128 | /** |
||
129 | * @var LocalImage[] |
||
130 | */ |
||
131 | private $images = []; |
||
132 | |||
133 | /** |
||
134 | * @var Field[] |
||
135 | */ |
||
136 | private $headerFields = []; |
||
137 | |||
138 | /** |
||
139 | * @var Field[] |
||
140 | */ |
||
141 | private $primaryFields = []; |
||
142 | |||
143 | /** |
||
144 | * @var Field[] |
||
145 | */ |
||
146 | private $auxiliaryFields = []; |
||
147 | |||
148 | /** |
||
149 | * @var Field[] |
||
150 | */ |
||
151 | private $secondaryFields = []; |
||
152 | |||
153 | /** |
||
154 | * @var Field[] |
||
155 | */ |
||
156 | private $backFields = []; |
||
157 | |||
158 | 3 | public function __construct(string $serialNumber) |
|
162 | |||
163 | 3 | public function setOrganizationName(string $organizationName): void |
|
167 | |||
168 | 3 | public function setDescription(string $description): void |
|
172 | |||
173 | 3 | public function setPassTypeIdentifier(string $passTypeIdentifier): void |
|
177 | |||
178 | 3 | public function setTeamIdentifier(string $teamIdentifier): void |
|
182 | |||
183 | 7 | public function setLogoText(string $logoText): void |
|
187 | |||
188 | 7 | public function setRelevantDate(DateTimeImmutable $relevantDate): void |
|
192 | |||
193 | 7 | public function setBarcode(Barcode $barcode): void |
|
197 | |||
198 | 7 | public function addLocation(Location $location): void |
|
202 | |||
203 | 6 | public function setMaxDistance(int $maxDistance): void |
|
207 | |||
208 | 7 | public function setWebService($url, $authenticationToken): void |
|
213 | |||
214 | 7 | public function setForegroundColor(Color $foregroundColor): void |
|
218 | |||
219 | 7 | public function setBackgroundColor(Color $backgroundColor): void |
|
223 | |||
224 | 6 | public function setLabelColor(Color $labelColor): void |
|
228 | |||
229 | 6 | public function addImage(Image $image): void |
|
233 | |||
234 | 7 | public function addHeaderField(Field $field): void |
|
238 | |||
239 | 7 | public function addPrimaryField(Field $field): void |
|
243 | |||
244 | 1 | public function addAuxiliaryField(Field $field): void |
|
248 | |||
249 | 7 | public function addSecondaryField(Field $field): void |
|
253 | |||
254 | 7 | public function addBackField(Field $field): void |
|
258 | |||
259 | 1 | public function setAppLaunchURL(string $appLaunchURL): void |
|
263 | |||
264 | 1 | public function addAssociatedStoreIdentifiers(int $associatedStoreIdentifiers): void |
|
268 | |||
269 | 1 | public function setUserInfo(string $userInfo): void |
|
273 | |||
274 | 6 | public function voided(): void |
|
278 | |||
279 | 6 | public function hasPassTypeIdentifier(): bool |
|
283 | |||
284 | 6 | public function hasTeamIdentifier(): bool |
|
288 | |||
289 | 3 | public function getData(): array |
|
298 | |||
299 | 2 | private function getGenericData(): array |
|
375 | |||
376 | 2 | private function getFieldsData(): array |
|
398 | |||
399 | /** |
||
400 | * @return Image[] |
||
401 | */ |
||
402 | 6 | public function getImages(): array |
|
406 | |||
407 | /** |
||
408 | * @throws LogicException |
||
409 | * @throws MissingRequiredDataException |
||
410 | */ |
||
411 | 29 | public function validate(): void |
|
433 | } |
||
434 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: