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 | 498 | public function __construct($id = null, $options = null) |
|
107 | |||
108 | /** |
||
109 | * Init the stripe object. |
||
110 | */ |
||
111 | 498 | private function init() |
|
142 | |||
143 | 498 | /* ----------------------------------------------------------------- |
|
144 | | Getters & Setters (+Magics) |
||
145 | 498 | | ----------------------------------------------------------------- |
|
146 | */ |
||
147 | 496 | ||
148 | /** |
||
149 | 496 | * Set the Id. |
|
150 | * |
||
151 | * @param array|string|null $id |
||
152 | * |
||
153 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
154 | * |
||
155 | * @return self |
||
156 | */ |
||
157 | private function setId($id) |
||
165 | |||
166 | 6 | /** |
|
167 | 6 | * Set the Id from Array. |
|
168 | * |
||
169 | * @param array|string|null $id |
||
170 | * |
||
171 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
172 | */ |
||
173 | private function setIdIfArray(&$id) |
||
182 | |||
183 | /** |
||
184 | * Get Retrieve Parameters. |
||
185 | * |
||
186 | 384 | * @return array |
|
187 | */ |
||
188 | 384 | protected function getRetrieveParams() |
|
192 | |||
193 | 6 | /** |
|
194 | * Standard get accessor. |
||
195 | 6 | * |
|
196 | * @param string|int $key |
||
197 | * |
||
198 | * @return mixed|null |
||
199 | */ |
||
200 | public function &__get($key) |
||
211 | |||
212 | /** |
||
213 | * Standard set accessor. |
||
214 | * |
||
215 | * @param string $key |
||
216 | * @param mixed $value |
||
217 | */ |
||
218 | public function __set($key, $value) |
||
225 | |||
226 | 430 | /** |
|
227 | 430 | * Set value. |
|
228 | 430 | * |
|
229 | 430 | * @param string $key |
|
230 | * @param mixed $value |
||
231 | 10 | * |
|
232 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
233 | */ |
||
234 | private function setValue($key, $value) |
||
254 | |||
255 | /** |
||
256 | * Get the last response from the Stripe API. |
||
257 | * |
||
258 | 378 | * @return \Arcanedev\Stripe\Http\Response |
|
259 | */ |
||
260 | 378 | public function getLastResponse() |
|
264 | |||
265 | /** |
||
266 | * Set the last response from the Stripe API. |
||
267 | * |
||
268 | * @param \Arcanedev\Stripe\Http\Response $response |
||
269 | * |
||
270 | * @return static |
||
271 | */ |
||
272 | 104 | public function setLastResponse(Response $response) |
|
278 | |||
279 | /** |
||
280 | * Check has a value by key. |
||
281 | * |
||
282 | 38 | * @param string $key |
|
283 | * |
||
284 | 38 | * @return bool |
|
285 | */ |
||
286 | 38 | public function __isset($key) |
|
290 | |||
291 | /** |
||
292 | * Unset element from values. |
||
293 | * |
||
294 | * @param string $key |
||
295 | 2 | */ |
|
296 | public function __unset($key) |
||
303 | |||
304 | /** |
||
305 | 2 | * Convert StripeObject to string. |
|
306 | * |
||
307 | 2 | * @return string |
|
308 | */ |
||
309 | public function __toString() |
||
313 | |||
314 | /** |
||
315 | * Json serialize. |
||
316 | * |
||
317 | 10 | * @return array |
|
318 | */ |
||
319 | 10 | public function jsonSerialize() |
|
323 | |||
324 | /** |
||
325 | * Convert StripeObject to array. |
||
326 | * |
||
327 | * @param bool $recursive |
||
328 | * |
||
329 | * @return array |
||
330 | */ |
||
331 | 2 | public function toArray($recursive = false) |
|
337 | |||
338 | /** |
||
339 | * Convert StripeObject to JSON. |
||
340 | * |
||
341 | * @param int $options |
||
342 | * |
||
343 | * @return string |
||
344 | 452 | */ |
|
345 | public function toJson($options = 0) |
||
352 | |||
353 | /** |
||
354 | 34 | * Get only value keys. |
|
355 | * |
||
356 | 34 | * @return array |
|
357 | 34 | */ |
|
358 | public function keys() |
||
362 | |||
363 | /* ----------------------------------------------------------------- |
||
364 | 2 | | ArrayAccess methods |
|
365 | | ----------------------------------------------------------------- |
||
366 | 2 | */ |
|
367 | 2 | ||
368 | public function offsetSet($key, $value) |
||
372 | 254 | ||
373 | 266 | public function offsetExists($key) |
|
377 | |||
378 | public function offsetUnset($key) |
||
382 | |||
383 | public function offsetGet($key) |
||
389 | |||
390 | 382 | /* ----------------------------------------------------------------- |
|
391 | | Main Methods |
||
392 | 382 | | ----------------------------------------------------------------- |
|
393 | 382 | */ |
|
394 | |||
395 | 382 | /** |
|
396 | * This unfortunately needs to be public to be used in Util.php |
||
397 | * Return The object constructed from the given values. |
||
398 | * |
||
399 | * @param array $values |
||
400 | * @param \Arcanedev\Stripe\Http\RequestOptions|array|string|null $options |
||
401 | * |
||
402 | * @return static |
||
403 | */ |
||
404 | public static function scopedConstructFrom($values, $options) |
||
411 | 390 | ||
412 | 390 | /** |
|
413 | 368 | * Refreshes this object using the provided values. |
|
414 | * |
||
415 | 390 | * @param array $values |
|
416 | * @param \Arcanedev\Stripe\Http\RequestOptions|array|string|null $options |
||
417 | 390 | * @param bool $partial |
|
418 | 390 | */ |
|
419 | public function refreshFrom($values, $options, $partial = false) |
||
435 | 390 | ||
436 | /** |
||
437 | 390 | * Clean refreshed StripeObject. |
|
438 | 30 | * |
|
439 | 2 | * @param array $values |
|
440 | * @param bool|false $partial |
||
441 | 30 | */ |
|
442 | private function cleanObject($values, $partial) |
||
458 | 390 | ||
459 | /** |
||
460 | * Construct Value. |
||
461 | * |
||
462 | * @param string $key |
||
463 | * @param mixed $value |
||
464 | * @param \Arcanedev\Stripe\Http\RequestOptions|array|string|null $options |
||
465 | * |
||
466 | * @return static|\Arcanedev\Stripe\StripeResource|\Arcanedev\Stripe\Collection|array |
||
467 | */ |
||
468 | private function constructValue($key, $value, $options) |
||
474 | |||
475 | /** |
||
476 | * Pretend to have late static bindings. |
||
477 | * |
||
478 | * @param string $method |
||
479 | * |
||
480 | * @return mixed |
||
481 | */ |
||
482 | protected function lsb($method) |
||
489 | |||
490 | /** |
||
491 | * Scoped Late Static Bindings. |
||
492 | * |
||
493 | * @param string $class |
||
494 | * @param string $method |
||
495 | * |
||
496 | * @return mixed |
||
497 | */ |
||
498 | protected static function scopedLsb($class, $method) |
||
505 | |||
506 | 8 | /* ----------------------------------------------------------------- |
|
507 | 2 | | Check Methods |
|
508 | 6 | | ----------------------------------------------------------------- |
|
509 | */ |
||
510 | |||
511 | /** |
||
512 | * Check if array has id. |
||
513 | * |
||
514 | * @param array $array |
||
515 | 2 | * |
|
516 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
517 | 2 | */ |
|
518 | private function checkIdIsInArray($array) |
||
523 | |||
524 | /** |
||
525 | * Check if object has retrieve parameters. |
||
526 | * |
||
527 | * @return bool |
||
528 | 430 | */ |
|
529 | public function hasRetrieveParams() |
||
533 | 2 | ||
534 | 2 | /** |
|
535 | 2 | * Check if attribute deletion. |
|
536 | * |
||
537 | 430 | * @param string $key |
|
538 | * @param mixed|null $value |
||
539 | * |
||
540 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
541 | */ |
||
542 | private function checkIfAttributeDeletion($key, $value) |
||
552 | |||
553 | 2 | /** |
|
554 | 2 | * Check metadata attribute. |
|
555 | * |
||
556 | 430 | * @param string $key |
|
557 | * @param mixed|null $value |
||
558 | * |
||
559 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
560 | */ |
||
561 | private function checkMetadataAttribute($key, $value) |
||
571 | |||
572 | /** |
||
573 | * Check permanent attributes. |
||
574 | * |
||
575 | * @param string $key |
||
576 | 430 | */ |
|
577 | private function checkPermanentAttributes($key) |
||
582 | 16 | ||
583 | /** |
||
584 | 12 | * Check unsaved attributes. |
|
585 | * |
||
586 | * @param array $supported |
||
587 | * |
||
588 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
589 | */ |
||
590 | private function checkUnsavedAttributes($supported) |
||
599 | 12 | ||
600 | /** |
||
601 | * Check not found attributes exception. |
||
602 | * |
||
603 | * @param array $notFound |
||
604 | * |
||
605 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
606 | */ |
||
607 | private function checkNotFoundAttributesException($notFound) |
||
614 | 88 | ||
615 | /* ----------------------------------------------------------------- |
||
616 | 88 | | Other Methods |
|
617 | 88 | | ----------------------------------------------------------------- |
|
618 | */ |
||
619 | 88 | ||
620 | /** |
||
621 | * A recursive mapping of attributes to values for this object, |
||
622 | * including the proper value for deleted attributes. |
||
623 | * |
||
624 | * @return array |
||
625 | */ |
||
626 | protected function serializeParameters() |
||
635 | |||
636 | /** |
||
637 | * Serialize unsaved values. |
||
638 | * |
||
639 | 88 | * @param array $params |
|
640 | */ |
||
641 | 88 | private function serializeUnsavedValues(&$params) |
|
647 | 88 | ||
648 | /** |
||
649 | * Serialize nested updatable attributes. |
||
650 | 88 | * |
|
651 | * @param array $params |
||
652 | */ |
||
653 | private function serializeNestedUpdatableAttributes(&$params) |
||
665 | 4 | ||
666 | /** |
||
667 | * Show undefined property warning message. |
||
668 | 6 | * |
|
669 | * @param string $class |
||
670 | 6 | * @param string $key |
|
671 | */ |
||
672 | private function showUndefinedPropertyMsg($class, $key) |
||
685 | |||
686 | /** |
||
687 | * Show available attributes for undefined property warning message. |
||
688 | * |
||
689 | * @return string |
||
690 | */ |
||
691 | private function showUndefinedPropertyMsgAttributes() |
||
697 | } |
||
698 |