Complex classes like QRCode 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 QRCode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class QRCode |
||
11 | { |
||
12 | /** |
||
13 | * @var IBAN |
||
14 | */ |
||
15 | protected $creditorAccount; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $creditorName; |
||
21 | |||
22 | /** |
||
23 | * @var StructuredPostalAddress |
||
24 | */ |
||
25 | protected $creditorAddress; |
||
26 | |||
27 | /** |
||
28 | * @var string|null |
||
29 | */ |
||
30 | protected $ultimateCreditorName; |
||
31 | |||
32 | /** |
||
33 | * @var StructuredPostalAddress|null |
||
34 | */ |
||
35 | protected $ultimateCreditorAddress; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $currency; |
||
41 | |||
42 | /** |
||
43 | * @var Money\Money|null |
||
44 | */ |
||
45 | protected $amount; |
||
46 | |||
47 | /** |
||
48 | * @var string|null |
||
49 | */ |
||
50 | protected $dueDate; |
||
51 | |||
52 | /** |
||
53 | * @var string|null |
||
54 | */ |
||
55 | protected $ultimateDebtorName; |
||
56 | |||
57 | /** |
||
58 | * @var StructuredPostalAddress|null |
||
59 | */ |
||
60 | protected $ultimateDebtorAddress; |
||
61 | |||
62 | /** |
||
63 | * @var QRReference|CreditorReference|null |
||
64 | */ |
||
65 | protected $reference; |
||
66 | |||
67 | /** |
||
68 | * @var string|null |
||
69 | */ |
||
70 | protected $unstructuredMessage; |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $alternativeSchemes; |
||
76 | |||
77 | /** |
||
78 | * Constructor |
||
79 | * |
||
80 | * @param string $code |
||
81 | * |
||
82 | * @throws \InvalidArgumentException When the QR code is malformed or otherwise invalid. |
||
83 | */ |
||
84 | 5 | public function __construct($code) |
|
156 | |||
157 | /** |
||
158 | * Gets the IBAN of the creditor |
||
159 | * |
||
160 | * @return IBAN |
||
161 | */ |
||
162 | 2 | public function getCreditorAccount() |
|
166 | |||
167 | /** |
||
168 | * Gets the name of the creditor |
||
169 | * |
||
170 | * @return string A ISO 3166-1 alpha-2 country code |
||
171 | */ |
||
172 | 2 | public function getCreditorName() |
|
176 | |||
177 | /** |
||
178 | * Gets the address of the creditor |
||
179 | * |
||
180 | * @return StructuredPostalAddress |
||
181 | */ |
||
182 | 2 | public function getCreditorAddress() |
|
186 | |||
187 | /** |
||
188 | * Gets the name of the ultimate creditor |
||
189 | * |
||
190 | * @return string|null |
||
191 | */ |
||
192 | 2 | public function getUltimateCreditorName() |
|
196 | |||
197 | /** |
||
198 | * Gets the address of the ultimate creditor |
||
199 | * |
||
200 | * @return StructuredPostalAddress|null |
||
201 | */ |
||
202 | 2 | public function getUltimateCreditorAddress() |
|
206 | |||
207 | /** |
||
208 | * Gets the currency of the creditor's account |
||
209 | * |
||
210 | * @return string A ISO 4217 currency code |
||
211 | */ |
||
212 | 2 | public function getCurrency() |
|
216 | |||
217 | /** |
||
218 | * Gets the amount |
||
219 | * |
||
220 | * @return Money\Money|null |
||
221 | */ |
||
222 | 2 | public function getAmount() |
|
226 | |||
227 | /** |
||
228 | * Gets the due date |
||
229 | * |
||
230 | * @return string|null |
||
231 | */ |
||
232 | public function getDueDate() |
||
236 | |||
237 | /** |
||
238 | * Gets the name of the ultimate debtor |
||
239 | * |
||
240 | * @return string|null |
||
241 | */ |
||
242 | 2 | public function getUltimateDebtorName() |
|
246 | |||
247 | /** |
||
248 | * Gets the address of the ultimate debtor |
||
249 | * |
||
250 | * @return StructuredPostalAddress|null |
||
251 | */ |
||
252 | 2 | public function getUltimateDebtorAddress() |
|
256 | |||
257 | /** |
||
258 | * Gets the reference number |
||
259 | * |
||
260 | * @return CreditorReference|QRReference|null |
||
261 | */ |
||
262 | 2 | public function getReference() |
|
266 | |||
267 | /** |
||
268 | * Gets the additional information |
||
269 | * |
||
270 | * @return string|null |
||
271 | */ |
||
272 | 2 | public function getUnstructuredMessage() |
|
276 | |||
277 | /** |
||
278 | * Gets a list of subelements for a given identifier. |
||
279 | * |
||
280 | //* @param string $id The 2-char identifier |
||
281 | * @param string $id The 3-char identifier |
||
282 | * |
||
283 | * @return array|null |
||
284 | */ |
||
285 | 1 | public function getAlternativeScheme($id) |
|
297 | |||
298 | /** |
||
299 | * Gets a list of all alternative schemes |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | 2 | public function getAlternativeSchemes() |
|
307 | |||
308 | /** |
||
309 | * Parses an address |
||
310 | * |
||
311 | * @param array $elements |
||
312 | * |
||
313 | * @returns StructuredPostalAddress|null |
||
314 | * |
||
315 | * @throws \InvalidArgumentException When the address is malformed. |
||
316 | */ |
||
317 | 2 | protected function parseAddress(array $elements) |
|
339 | } |
||
340 |