Complex classes like StripeObject 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 StripeObject, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Arcanedev\Stripe; |
||
24 | class StripeObject implements StripObjectContract, ArrayAccess, JsonSerializable, Arrayable, Jsonable |
||
25 | { |
||
26 | /* ----------------------------------------------------------------- |
||
27 | | Properties |
||
28 | | ----------------------------------------------------------------- |
||
29 | */ |
||
30 | |||
31 | /** |
||
32 | * @var \Arcanedev\Stripe\Http\RequestOptions|string|array |
||
33 | */ |
||
34 | protected $opts; |
||
35 | |||
36 | /** @var array */ |
||
37 | protected $values; |
||
38 | |||
39 | /** |
||
40 | * Unsaved Values. |
||
41 | * |
||
42 | * @var \Arcanedev\Stripe\Utilities\UtilSet |
||
43 | */ |
||
44 | protected $unsavedValues; |
||
45 | |||
46 | /** |
||
47 | * Transient (Deleted) Values. |
||
48 | * |
||
49 | * @var \Arcanedev\Stripe\Utilities\UtilSet |
||
50 | */ |
||
51 | protected $transientValues; |
||
52 | |||
53 | /** |
||
54 | * Retrieve parameters used to query the object. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $retrieveParameters; |
||
59 | |||
60 | /** |
||
61 | * Attributes that should not be sent to the API because |
||
62 | * they're not updatable (e.g. API key, ID). |
||
63 | * |
||
64 | * @var \Arcanedev\Stripe\Utilities\UtilSet |
||
65 | */ |
||
66 | public static $permanentAttributes; |
||
67 | |||
68 | /** |
||
69 | * Attributes that are nested but still updatable from the |
||
70 | * parent class's URL (e.g. metadata). |
||
71 | * |
||
72 | * @var \Arcanedev\Stripe\Utilities\UtilSet |
||
73 | */ |
||
74 | public static $nestedUpdatableAttributes; |
||
75 | |||
76 | /** |
||
77 | * Allow to check attributes while setting. |
||
78 | * |
||
79 | * @var bool |
||
80 | */ |
||
81 | protected $checkUnsavedAttributes = false; |
||
82 | |||
83 | /** |
||
84 | * The last response. |
||
85 | * |
||
86 | * @var \Arcanedev\Stripe\Http\Response |
||
87 | */ |
||
88 | protected $lastResponse; |
||
89 | |||
90 | /* ----------------------------------------------------------------- |
||
91 | | Constructor |
||
92 | | ----------------------------------------------------------------- |
||
93 | */ |
||
94 | |||
95 | /** |
||
96 | * Make a Stripe object instance. |
||
97 | * |
||
98 | * @param string|null $id |
||
99 | * @param string|array|null $options |
||
100 | */ |
||
101 | 690 | public function __construct($id = null, $options = null) |
|
107 | |||
108 | /** |
||
109 | * Init the stripe object. |
||
110 | */ |
||
111 | 690 | private function init() |
|
128 | |||
129 | /* ----------------------------------------------------------------- |
||
130 | | Getters & Setters (+Magics) |
||
131 | | ----------------------------------------------------------------- |
||
132 | */ |
||
133 | |||
134 | /** |
||
135 | * Set the Id. |
||
136 | * |
||
137 | * @param array|string|null $id |
||
138 | * |
||
139 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
140 | * |
||
141 | * @return self |
||
142 | */ |
||
143 | 690 | private function setId($id) |
|
151 | |||
152 | /** |
||
153 | * Set the Id from Array. |
||
154 | * |
||
155 | * @param array|string|null $id |
||
156 | * |
||
157 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
158 | */ |
||
159 | 690 | private function setIdIfArray(&$id) |
|
168 | |||
169 | /** |
||
170 | * Get Retrieve Parameters. |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | 3 | protected function getRetrieveParams() |
|
178 | |||
179 | /** |
||
180 | * Standard get accessor. |
||
181 | * |
||
182 | * @param string|int $key |
||
183 | * |
||
184 | * @return mixed|null |
||
185 | */ |
||
186 | 507 | public function &__get($key) |
|
197 | |||
198 | /** |
||
199 | * Standard set accessor. |
||
200 | * |
||
201 | * @param string $key |
||
202 | * @param mixed $value |
||
203 | */ |
||
204 | 603 | public function __set($key, $value) |
|
211 | |||
212 | /** |
||
213 | * Set value. |
||
214 | * |
||
215 | * @param string $key |
||
216 | * @param mixed $value |
||
217 | * |
||
218 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
219 | */ |
||
220 | 603 | private function setValue($key, $value) |
|
240 | |||
241 | /** |
||
242 | * Get the last response from the Stripe API. |
||
243 | * |
||
244 | * @return \Arcanedev\Stripe\Http\Response |
||
245 | */ |
||
246 | 9 | public function getLastResponse() |
|
250 | |||
251 | /** |
||
252 | * Set the last response from the Stripe API. |
||
253 | * |
||
254 | * @param \Arcanedev\Stripe\Http\Response $response |
||
255 | * |
||
256 | * @return self |
||
257 | */ |
||
258 | 507 | public function setLastResponse(Response $response) |
|
264 | |||
265 | /** |
||
266 | * Check has a value by key. |
||
267 | * |
||
268 | * @param string $key |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | 156 | public function __isset($key) |
|
276 | |||
277 | /** |
||
278 | * Unset element from values. |
||
279 | * |
||
280 | * @param string $key |
||
281 | */ |
||
282 | 57 | public function __unset($key) |
|
289 | |||
290 | /** |
||
291 | * Convert StripeObject to string. |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | 3 | public function __toString() |
|
299 | |||
300 | /** |
||
301 | * Json serialize. |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | 3 | public function jsonSerialize() |
|
309 | |||
310 | /** |
||
311 | * Convert StripeObject to array. |
||
312 | * |
||
313 | * @param bool $recursive |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | 15 | public function toArray($recursive = false) |
|
323 | |||
324 | /** |
||
325 | * Convert StripeObject to JSON. |
||
326 | * |
||
327 | * @param int $options |
||
328 | * |
||
329 | * @return string |
||
330 | */ |
||
331 | 3 | public function toJson($options = 0) |
|
338 | |||
339 | /** |
||
340 | * Get only value keys. |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | 618 | public function keys() |
|
348 | |||
349 | /* ----------------------------------------------------------------- |
||
350 | | ArrayAccess methods |
||
351 | | ----------------------------------------------------------------- |
||
352 | */ |
||
353 | |||
354 | 51 | public function offsetSet($key, $value) |
|
358 | |||
359 | 507 | public function offsetExists($key) |
|
363 | |||
364 | 3 | public function offsetUnset($key) |
|
368 | |||
369 | 393 | public function offsetGet($key) |
|
375 | |||
376 | /* ----------------------------------------------------------------- |
||
377 | | Main Methods |
||
378 | | ----------------------------------------------------------------- |
||
379 | */ |
||
380 | |||
381 | /** |
||
382 | * This unfortunately needs to be public to be used in Util.php |
||
383 | * Return The object constructed from the given values. |
||
384 | * |
||
385 | * @param string $class |
||
386 | * @param array $values |
||
387 | * @param \Arcanedev\Stripe\Http\RequestOptions|array|string|null $options |
||
388 | * |
||
389 | * @return self |
||
390 | */ |
||
391 | 498 | public static function scopedConstructFrom($class, $values, $options) |
|
399 | |||
400 | /** |
||
401 | * Refreshes this object using the provided values. |
||
402 | * |
||
403 | * @param array $values |
||
404 | * @param \Arcanedev\Stripe\Http\RequestOptions|array|string|null $options |
||
405 | * @param bool $partial |
||
406 | */ |
||
407 | 516 | public function refreshFrom($values, $options, $partial = false) |
|
423 | |||
424 | /** |
||
425 | * Clean refreshed StripeObject. |
||
426 | * |
||
427 | * @param array $values |
||
428 | * @param bool|false $partial |
||
429 | */ |
||
430 | 516 | private function cleanObject($values, $partial) |
|
446 | |||
447 | /** |
||
448 | * Construct Value. |
||
449 | * |
||
450 | * @param string $key |
||
451 | * @param mixed $value |
||
452 | * @param \Arcanedev\Stripe\Http\RequestOptions|array|string|null $options |
||
453 | * |
||
454 | * @return self|\Arcanedev\Stripe\StripeResource|\Arcanedev\Stripe\Collection|array |
||
455 | */ |
||
456 | 516 | private function constructValue($key, $value, $options) |
|
462 | |||
463 | /** |
||
464 | * Pretend to have late static bindings. |
||
465 | * |
||
466 | * @param string $method |
||
467 | * |
||
468 | * @return mixed |
||
469 | */ |
||
470 | protected function lsb($method) |
||
477 | |||
478 | /** |
||
479 | * Scoped Late Static Bindings. |
||
480 | * |
||
481 | * @param string $class |
||
482 | * @param string $method |
||
483 | * |
||
484 | * @return mixed |
||
485 | */ |
||
486 | protected static function scopedLsb($class, $method) |
||
493 | |||
494 | /* ----------------------------------------------------------------- |
||
495 | | Check Methods |
||
496 | | ----------------------------------------------------------------- |
||
497 | */ |
||
498 | |||
499 | /** |
||
500 | * Check if array has id. |
||
501 | * |
||
502 | * @param array $array |
||
503 | * |
||
504 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
505 | */ |
||
506 | 12 | private function checkIdIsInArray($array) |
|
511 | |||
512 | /** |
||
513 | * Check if object has retrieve parameters. |
||
514 | * |
||
515 | * @return bool |
||
516 | */ |
||
517 | 3 | public function hasRetrieveParams() |
|
521 | |||
522 | /** |
||
523 | * Check if attribute deletion. |
||
524 | * |
||
525 | * @param string $key |
||
526 | * @param mixed|null $value |
||
527 | * |
||
528 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
529 | */ |
||
530 | 603 | private function checkIfAttributeDeletion($key, $value) |
|
540 | |||
541 | /** |
||
542 | * Check metadata attribute. |
||
543 | * |
||
544 | * @param string $key |
||
545 | * @param mixed|null $value |
||
546 | * |
||
547 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
548 | */ |
||
549 | 603 | private function checkMetadataAttribute($key, $value) |
|
559 | |||
560 | /** |
||
561 | * Check permanent attributes. |
||
562 | * |
||
563 | * @param string $key |
||
564 | */ |
||
565 | 603 | private function checkPermanentAttributes($key) |
|
570 | |||
571 | /** |
||
572 | * Check unsaved attributes. |
||
573 | * |
||
574 | * @param array $supported |
||
575 | * |
||
576 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
577 | */ |
||
578 | 603 | private function checkUnsavedAttributes($supported) |
|
587 | |||
588 | /** |
||
589 | * Check not found attributes exception. |
||
590 | * |
||
591 | * @param array $notFound |
||
592 | * |
||
593 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
594 | */ |
||
595 | 24 | private function checkNotFoundAttributesException($notFound) |
|
602 | |||
603 | /* ----------------------------------------------------------------- |
||
604 | | Other Methods |
||
605 | | ----------------------------------------------------------------- |
||
606 | */ |
||
607 | |||
608 | /** |
||
609 | * A recursive mapping of attributes to values for this object, |
||
610 | * including the proper value for deleted attributes. |
||
611 | * |
||
612 | * @return array |
||
613 | */ |
||
614 | 132 | protected function serializeParameters() |
|
623 | |||
624 | /** |
||
625 | * Serialize unsaved values. |
||
626 | * |
||
627 | * @param array $params |
||
628 | */ |
||
629 | 132 | private function serializeUnsavedValues(&$params) |
|
635 | |||
636 | /** |
||
637 | * Serialize nested updatable attributes. |
||
638 | * |
||
639 | * @param array $params |
||
640 | */ |
||
641 | 132 | private function serializeNestedUpdatableAttributes(&$params) |
|
653 | |||
654 | /** |
||
655 | * Show undefined property warning message. |
||
656 | * |
||
657 | * @param string $class |
||
658 | * @param string $key |
||
659 | */ |
||
660 | 9 | private function showUndefinedPropertyMsg($class, $key) |
|
673 | |||
674 | /** |
||
675 | * Show available attributes for undefined property warning message. |
||
676 | * |
||
677 | * @return string |
||
678 | */ |
||
679 | 6 | private function showUndefinedPropertyMsgAttributes() |
|
685 | } |
||
686 |