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 | private int $formatVersion = 1; |
||
|
|||
24 | private string $passTypeIdentifier; |
||
25 | private string $serialNumber; |
||
26 | private string $teamIdentifier; |
||
27 | private string $organizationName; |
||
28 | private string$description; |
||
29 | private string $logoText; |
||
30 | /** @var Barcode[] */ |
||
31 | private array $barcodes = []; |
||
32 | private DateTimeImmutable $relevantDate; |
||
33 | private string $appLaunchURL; |
||
34 | /** @var int[] */ |
||
35 | private array $associatedStoreIdentifiers = []; |
||
36 | private string $userInfo; |
||
37 | private DateTimeImmutable $expirationDate; |
||
38 | private bool $voided; |
||
39 | /** @var Location[] */ |
||
40 | private array $locations = []; |
||
41 | private int $maxDistance; |
||
42 | private string $webServiceURL; |
||
43 | private string $authenticationToken; |
||
44 | private Color $foregroundColor; |
||
45 | private Color $backgroundColor; |
||
46 | private Color $labelColor; |
||
47 | /** @var Image[] */ |
||
48 | private array $images = []; |
||
49 | /** @var Field[] */ |
||
50 | private array $headerFields = []; |
||
51 | /** @var Field[]*/ |
||
52 | private array $primaryFields = []; |
||
53 | /** @var Field[] */ |
||
54 | private array $auxiliaryFields = []; |
||
55 | /** @var Field[]*/ |
||
56 | private array $secondaryFields = []; |
||
57 | /** @var Field[] */ |
||
58 | private array $backFields = []; |
||
59 | |||
60 | 145 | public function __construct(string $serialNumber) |
|
61 | { |
||
62 | 145 | $this->serialNumber = $serialNumber; |
|
63 | 145 | } |
|
64 | |||
65 | 114 | public function setOrganizationName(string $organizationName): void |
|
66 | { |
||
67 | 114 | $this->organizationName = $organizationName; |
|
68 | 114 | } |
|
69 | |||
70 | 108 | public function setDescription(string $description): void |
|
71 | { |
||
72 | 108 | $this->description = $description; |
|
73 | 108 | } |
|
74 | |||
75 | 132 | public function setPassTypeIdentifier(string $passTypeIdentifier): void |
|
76 | { |
||
77 | 132 | $this->passTypeIdentifier = $passTypeIdentifier; |
|
78 | 132 | } |
|
79 | |||
80 | 126 | public function setTeamIdentifier(string $teamIdentifier): void |
|
81 | { |
||
82 | 126 | $this->teamIdentifier = $teamIdentifier; |
|
83 | 126 | } |
|
84 | |||
85 | 7 | public function setLogoText(string $logoText): void |
|
86 | { |
||
87 | 7 | $this->logoText = $logoText; |
|
88 | 7 | } |
|
89 | |||
90 | 7 | public function setRelevantDate(DateTimeImmutable $relevantDate): void |
|
91 | { |
||
92 | 7 | $this->relevantDate = $relevantDate; |
|
93 | 7 | } |
|
94 | |||
95 | 7 | public function setBarcode(Barcode $barcode): void |
|
96 | { |
||
97 | 7 | $this->barcodes[] = $barcode; |
|
98 | 7 | } |
|
99 | |||
100 | 7 | public function addLocation(Location $location): void |
|
101 | { |
||
102 | 7 | $this->locations[] = $location; |
|
103 | 7 | } |
|
104 | |||
105 | 6 | public function setMaxDistance(int $maxDistance): void |
|
106 | { |
||
107 | 6 | $this->maxDistance = $maxDistance; |
|
108 | 6 | } |
|
109 | |||
110 | 7 | public function setWebService(string $url, string $authenticationToken): void |
|
111 | { |
||
112 | 7 | $this->webServiceURL = $url; |
|
113 | 7 | $this->authenticationToken = $authenticationToken; |
|
114 | 7 | } |
|
115 | |||
116 | 7 | public function setForegroundColor(Color $foregroundColor): void |
|
117 | { |
||
118 | 7 | $this->foregroundColor = $foregroundColor; |
|
119 | 7 | } |
|
120 | |||
121 | 7 | public function setBackgroundColor(Color $backgroundColor): void |
|
122 | { |
||
123 | 7 | $this->backgroundColor = $backgroundColor; |
|
124 | 7 | } |
|
125 | |||
126 | 6 | public function setLabelColor(Color $labelColor): void |
|
127 | { |
||
128 | 6 | $this->labelColor = $labelColor; |
|
129 | 6 | } |
|
130 | |||
131 | 7 | public function addImage(Image $image): void |
|
132 | { |
||
133 | 7 | $this->images[] = $image; |
|
134 | 7 | } |
|
135 | |||
136 | 7 | public function addHeaderField(Field $field): void |
|
137 | { |
||
138 | 7 | $this->headerFields[] = $field; |
|
139 | 7 | } |
|
140 | |||
141 | 7 | public function addPrimaryField(Field $field): void |
|
142 | { |
||
143 | 7 | $this->primaryFields[] = $field; |
|
144 | 7 | } |
|
145 | |||
146 | 7 | public function addAuxiliaryField(Field $field): void |
|
147 | { |
||
148 | 7 | $this->auxiliaryFields[] = $field; |
|
149 | 7 | } |
|
150 | |||
151 | 7 | public function addSecondaryField(Field $field): void |
|
152 | { |
||
153 | 7 | $this->secondaryFields[] = $field; |
|
154 | 7 | } |
|
155 | |||
156 | 7 | public function addBackField(Field $field): void |
|
157 | { |
||
158 | 7 | $this->backFields[] = $field; |
|
159 | 7 | } |
|
160 | |||
161 | 1 | public function setAppLaunchURL(string $appLaunchURL): void |
|
162 | { |
||
163 | 1 | $this->appLaunchURL = $appLaunchURL; |
|
164 | 1 | } |
|
165 | |||
166 | 1 | public function addAssociatedStoreIdentifiers(int $associatedStoreIdentifiers): void |
|
167 | { |
||
168 | 1 | $this->associatedStoreIdentifiers[] = $associatedStoreIdentifiers; |
|
169 | 1 | } |
|
170 | |||
171 | 1 | public function setUserInfo(string $userInfo): void |
|
172 | { |
||
173 | 1 | $this->userInfo = $userInfo; |
|
174 | 1 | } |
|
175 | |||
176 | 6 | public function voided(): void |
|
177 | { |
||
178 | 6 | $this->voided = true; |
|
179 | 6 | } |
|
180 | |||
181 | public function isVoided(): bool |
||
182 | { |
||
183 | return isset($this->voided); |
||
184 | } |
||
185 | |||
186 | 12 | public function hasPassTypeIdentifier(): bool |
|
187 | { |
||
188 | 12 | return isset($this->passTypeIdentifier); |
|
189 | } |
||
190 | |||
191 | 12 | public function hasTeamIdentifier(): bool |
|
192 | { |
||
193 | 12 | return isset($this->teamIdentifier); |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * @return array<int|string, mixed> |
||
198 | * @throws MissingRequiredDataException |
||
199 | */ |
||
200 | 127 | public function getData(): array |
|
201 | { |
||
202 | 127 | $this->validate(); |
|
203 | |||
204 | 101 | $data = $this->getGenericData(); |
|
205 | 101 | $data[static::TYPE] = $this->getFieldsData(); |
|
206 | |||
207 | 101 | return $data; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * @return array<int|string, mixed> |
||
212 | */ |
||
213 | 101 | private function getGenericData(): array |
|
214 | { |
||
215 | $data = [ |
||
216 | 101 | 'formatVersion' => $this->formatVersion, |
|
217 | 101 | 'passTypeIdentifier' => $this->passTypeIdentifier, |
|
218 | 101 | 'serialNumber' => $this->serialNumber, |
|
219 | 101 | 'teamIdentifier' => $this->teamIdentifier, |
|
220 | 101 | 'organizationName' => $this->organizationName, |
|
221 | 101 | 'description' => $this->description, |
|
222 | ]; |
||
223 | |||
224 | 101 | if (isset($this->logoText)) { |
|
225 | 7 | $data['logoText'] = $this->logoText; |
|
226 | } |
||
227 | |||
228 | 101 | if (count($this->barcodes) > 0) { |
|
229 | 7 | $data['barcode'] = $this->barcodes[0]->toArray(); |
|
230 | |||
231 | 7 | foreach ($this->barcodes as $barcode) { |
|
232 | 7 | $data['barcodes'][] = $barcode->toArray(); |
|
233 | } |
||
234 | } |
||
235 | |||
236 | 101 | if (isset($this->relevantDate)) { |
|
237 | 7 | $data['relevantDate'] = $this->relevantDate->format(DateTimeInterface::W3C); |
|
238 | } |
||
239 | |||
240 | 101 | if (isset($this->expirationDate)) { |
|
241 | $data['expirationDate'] = $this->expirationDate->format(DateTimeInterface::W3C); |
||
242 | } |
||
243 | |||
244 | 101 | if (isset($this->appLaunchURL)) { |
|
245 | 1 | $data['appLaunchURL'] = $this->appLaunchURL; |
|
246 | } |
||
247 | |||
248 | 101 | if (count($this->associatedStoreIdentifiers) > 0) { |
|
249 | 1 | $data['associatedStoreIdentifiers'] = $this->associatedStoreIdentifiers; |
|
250 | } |
||
251 | |||
252 | 101 | if (isset($this->userInfo)) { |
|
253 | 1 | $data['userInfo'] = $this->userInfo; |
|
254 | } |
||
255 | |||
256 | 101 | if (isset($this->voided)) { |
|
257 | 6 | $data['voided'] = true; |
|
258 | } |
||
259 | |||
260 | 101 | if (isset($this->locations)) { |
|
261 | 101 | foreach ($this->locations as $location) { |
|
262 | 7 | $data['locations'][] = $location->toArray(); |
|
263 | } |
||
264 | } |
||
265 | |||
266 | 101 | if (isset($this->maxDistance)) { |
|
267 | 6 | $data['maxDistance'] = $this->maxDistance; |
|
268 | } |
||
269 | |||
270 | 101 | if (isset($this->webServiceURL) && isset($this->authenticationToken)) { |
|
271 | 7 | $data['webServiceURL'] = $this->webServiceURL; |
|
272 | 7 | $data['authenticationToken'] = $this->authenticationToken; |
|
273 | } |
||
274 | |||
275 | 101 | if (isset($this->foregroundColor)) { |
|
276 | 7 | $data['foregroundColor'] = $this->foregroundColor->toString(); |
|
277 | } |
||
278 | |||
279 | 101 | if (isset($this->backgroundColor)) { |
|
280 | 7 | $data['backgroundColor'] = $this->backgroundColor->toString(); |
|
281 | } |
||
282 | |||
283 | 101 | if (isset($this->labelColor)) { |
|
284 | 6 | $data['labelColor'] = $this->labelColor->toString(); |
|
285 | } |
||
286 | |||
287 | 101 | return $data; |
|
288 | } |
||
289 | |||
290 | /** |
||
291 | * @return array<int|string, mixed> |
||
292 | */ |
||
293 | 101 | private function getFieldsData(): array |
|
294 | { |
||
295 | 101 | $data = []; |
|
296 | |||
297 | 101 | foreach ($this->headerFields as $field) { |
|
298 | 7 | $data['headerFields'][] = $field->getMetaData(); |
|
299 | } |
||
300 | 101 | foreach ($this->primaryFields as $field) { |
|
301 | 7 | $data['primaryFields'][] = $field->getMetaData(); |
|
302 | } |
||
303 | 101 | foreach ($this->auxiliaryFields as $field) { |
|
304 | 7 | $data['auxiliaryFields'][] = $field->getMetaData(); |
|
305 | } |
||
306 | 101 | foreach ($this->secondaryFields as $field) { |
|
307 | 7 | $data['secondaryFields'][] = $field->getMetaData(); |
|
308 | } |
||
309 | 101 | foreach ($this->backFields as $field) { |
|
310 | 7 | $data['backFields'][] = $field->getMetaData(); |
|
311 | } |
||
312 | |||
313 | 101 | return $data; |
|
314 | } |
||
315 | |||
316 | /** |
||
317 | * @return Image[] |
||
318 | */ |
||
319 | 7 | public function getImages(): array |
|
320 | { |
||
321 | 7 | return $this->images; |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * @throws LogicException |
||
326 | * @throws MissingRequiredDataException |
||
327 | */ |
||
328 | 127 | public function validate(): void |
|
329 | { |
||
330 | 127 | if (static::TYPE === null) { |
|
331 | 1 | throw new LogicException('Please implement protected const TYPE in class.'); |
|
332 | } |
||
333 | |||
334 | 126 | if (!isset($this->passTypeIdentifier)) { |
|
335 | 6 | throw new MissingRequiredDataException('Please specify the PassTypeIdentifier before requesting the manifest data.'); |
|
336 | } |
||
337 | |||
338 | 120 | if (!isset($this->teamIdentifier)) { |
|
339 | 6 | throw new MissingRequiredDataException('Please specify the TeamIdentifier before requesting the manifest data.'); |
|
340 | } |
||
341 | |||
342 | 114 | if (!isset($this->organizationName)) { |
|
343 | 6 | throw new MissingRequiredDataException('Please specify the OrganizationName before requesting the manifest data.'); |
|
344 | } |
||
345 | |||
346 | 108 | if (!isset($this->description)) { |
|
347 | 6 | throw new MissingRequiredDataException('Please specify the Description before requesting the manifest data.'); |
|
348 | } |
||
349 | 102 | } |
|
350 | } |
||
351 |