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 | 502 | public function __construct($id = null, $options = null) |
|
107 | |||
108 | /** |
||
109 | * Init the stripe object. |
||
110 | */ |
||
111 | 502 | private function init() |
|
142 | |||
143 | /* ----------------------------------------------------------------- |
||
144 | | Getters & Setters (+Magics) |
||
145 | | ----------------------------------------------------------------- |
||
146 | */ |
||
147 | |||
148 | /** |
||
149 | * Set the Id. |
||
150 | * |
||
151 | * @param array|string|null $id |
||
152 | * |
||
153 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
154 | * |
||
155 | * @return self |
||
156 | */ |
||
157 | 502 | private function setId($id) |
|
165 | |||
166 | /** |
||
167 | * Set the Id from Array. |
||
168 | * |
||
169 | * @param array|string|null $id |
||
170 | * |
||
171 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
172 | */ |
||
173 | 502 | private function setIdIfArray(&$id) |
|
174 | { |
||
175 | 502 | if ( ! is_array($id)) return; |
|
176 | |||
177 | 8 | $this->checkIdIsInArray($id); |
|
178 | 6 | $this->retrieveParameters = array_diff_key($id, array_flip(['id'])); |
|
179 | |||
180 | 6 | $id = $id['id']; |
|
181 | 6 | } |
|
182 | |||
183 | /** |
||
184 | * Get Retrieve Parameters. |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | 2 | protected function getRetrieveParams() |
|
189 | { |
||
190 | 2 | return $this->retrieveParameters; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Standard get accessor. |
||
195 | * |
||
196 | * @param string|int $key |
||
197 | * |
||
198 | * @return mixed|null |
||
199 | */ |
||
200 | 388 | public function &__get($key) |
|
201 | { |
||
202 | 388 | $nullVal = null; |
|
203 | |||
204 | 388 | if (in_array($key, $this->keys())) |
|
205 | 384 | return $this->values[$key]; |
|
206 | |||
207 | 6 | $this->showUndefinedPropertyMsg(get_class($this), $key); |
|
208 | |||
209 | 6 | return $nullVal; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Standard set accessor. |
||
214 | * |
||
215 | * @param string $key |
||
216 | * @param mixed $value |
||
217 | */ |
||
218 | 434 | public function __set($key, $value) |
|
219 | { |
||
220 | 434 | $supportedAttributes = $this->keys(); |
|
221 | |||
222 | 434 | $this->setValue($key, $value); |
|
223 | 434 | $this->checkUnsavedAttributes($supportedAttributes); |
|
224 | 434 | } |
|
225 | |||
226 | /** |
||
227 | * Set value. |
||
228 | * |
||
229 | * @param string $key |
||
230 | * @param mixed $value |
||
231 | * |
||
232 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
233 | */ |
||
234 | 434 | private function setValue($key, $value) |
|
235 | { |
||
236 | 434 | $this->checkIfAttributeDeletion($key, $value); |
|
237 | 434 | $this->checkMetadataAttribute($key, $value); |
|
238 | |||
239 | if ( |
||
240 | 434 | self::$nestedUpdatableAttributes->includes($key) && |
|
241 | 434 | isset($this->$key) && |
|
242 | 434 | $this->$key instanceof AttachedObject && |
|
243 | 434 | is_array($value) |
|
244 | ) { |
||
245 | 10 | $this->$key->replaceWith($value); |
|
246 | } |
||
247 | else { |
||
248 | // TODO: may want to clear from $transientValues (Won't be user-visible). |
||
249 | 434 | $this->values[$key] = $value; |
|
250 | } |
||
251 | |||
252 | 434 | $this->checkPermanentAttributes($key); |
|
253 | 434 | } |
|
254 | |||
255 | /** |
||
256 | * Get the last response from the Stripe API. |
||
257 | * |
||
258 | * @return \Arcanedev\Stripe\Http\Response |
||
259 | */ |
||
260 | 6 | 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 | 382 | public function setLastResponse(Response $response) |
|
273 | { |
||
274 | 382 | $this->lastResponse = $response; |
|
275 | |||
276 | 382 | return $this; |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * Check has a value by key. |
||
281 | * |
||
282 | * @param string $key |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | 104 | public function __isset($key) |
|
287 | { |
||
288 | 104 | return isset($this->values[$key]); |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * Unset element from values. |
||
293 | * |
||
294 | * @param string $key |
||
295 | */ |
||
296 | 38 | public function __unset($key) |
|
297 | { |
||
298 | 38 | unset($this->values[$key]); |
|
299 | |||
300 | 38 | $this->transientValues->add($key); |
|
301 | 38 | $this->unsavedValues->discard($key); |
|
302 | 38 | } |
|
303 | |||
304 | /** |
||
305 | * Convert StripeObject to string. |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | 2 | public function __toString() |
|
310 | { |
||
311 | 2 | return get_class($this).' JSON: '.$this->toJson(); |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * Json serialize. |
||
316 | * |
||
317 | * @return array |
||
318 | */ |
||
319 | 2 | public function jsonSerialize() |
|
320 | { |
||
321 | 2 | return $this->toArray(true); |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * Convert StripeObject to array. |
||
326 | * |
||
327 | * @param bool $recursive |
||
328 | * |
||
329 | * @return array |
||
330 | */ |
||
331 | 10 | public function toArray($recursive = false) |
|
337 | |||
338 | /** |
||
339 | * Convert StripeObject to JSON. |
||
340 | * |
||
341 | * @param int $options |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | 2 | public function toJson($options = 0) |
|
346 | { |
||
347 | 2 | if ($options === 0 && defined('JSON_PRETTY_PRINT')) |
|
348 | 2 | $options = JSON_PRETTY_PRINT; |
|
349 | |||
350 | 2 | return json_encode($this->toArray(true), $options); |
|
351 | } |
||
352 | |||
353 | /** |
||
354 | * Get only value keys. |
||
355 | * |
||
356 | * @return array |
||
357 | */ |
||
358 | 456 | public function keys() |
|
362 | |||
363 | /* ----------------------------------------------------------------- |
||
364 | | ArrayAccess methods |
||
365 | | ----------------------------------------------------------------- |
||
366 | */ |
||
367 | |||
368 | 34 | public function offsetSet($key, $value) |
|
369 | { |
||
370 | 34 | $this->$key = $value; |
|
371 | 34 | } |
|
372 | |||
373 | 376 | public function offsetExists($key) |
|
374 | { |
||
375 | 376 | return array_key_exists($key, $this->values); |
|
376 | } |
||
377 | |||
378 | 2 | public function offsetUnset($key) |
|
379 | { |
||
380 | 2 | unset($this->$key); |
|
381 | 2 | } |
|
382 | |||
383 | 268 | public function offsetGet($key) |
|
384 | { |
||
385 | 268 | return array_key_exists($key, $this->values) |
|
386 | 256 | ? $this->values[$key] |
|
387 | 268 | : null; |
|
388 | } |
||
389 | |||
390 | /* ----------------------------------------------------------------- |
||
391 | | Main Methods |
||
392 | | ----------------------------------------------------------------- |
||
393 | */ |
||
394 | |||
395 | /** |
||
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 | 386 | public static function scopedConstructFrom($values, $options) |
|
405 | { |
||
406 | 386 | $obj = new static(isset($values['id']) ? $values['id'] : null); |
|
407 | 386 | $obj->refreshFrom($values, $options); |
|
408 | |||
409 | 386 | return $obj; |
|
410 | } |
||
411 | |||
412 | /** |
||
413 | * Refreshes this object using the provided values. |
||
414 | * |
||
415 | * @param array $values |
||
416 | * @param \Arcanedev\Stripe\Http\RequestOptions|array|string|null $options |
||
417 | * @param bool $partial |
||
418 | */ |
||
419 | 394 | public function refreshFrom($values, $options, $partial = false) |
|
420 | { |
||
421 | 394 | $this->opts = is_array($options) ? RequestOptions::parse($options) : $options; |
|
422 | |||
423 | 394 | $this->cleanObject($values, $partial); |
|
424 | |||
425 | 394 | foreach ($values as $key => $value) { |
|
426 | 394 | if (self::$permanentAttributes->includes($key) && isset($this[$key])) |
|
427 | 372 | continue; |
|
428 | |||
429 | 394 | $this->values[$key] = $this->constructValue($key, $value, $this->opts); |
|
430 | |||
431 | 394 | $this->transientValues->discard($key); |
|
432 | 394 | $this->unsavedValues->discard($key); |
|
433 | } |
||
434 | 394 | } |
|
435 | |||
436 | /** |
||
437 | * Clean refreshed StripeObject. |
||
438 | * |
||
439 | * @param array $values |
||
440 | * @param bool|false $partial |
||
441 | */ |
||
442 | 394 | private function cleanObject($values, $partial) |
|
443 | { |
||
444 | // Wipe old state before setting new. |
||
445 | // This is useful for e.g. updating a customer, where there is no persistent card parameter. |
||
446 | // Mark those values which don't persist as transient |
||
447 | 394 | $removed = ! $partial |
|
448 | 394 | ? array_diff($this->keys(), array_keys($values)) |
|
449 | 394 | : new UtilSet; |
|
450 | |||
451 | 394 | foreach ($removed as $key) { |
|
452 | 30 | if (self::$permanentAttributes->includes($key)) |
|
453 | 2 | continue; |
|
454 | |||
455 | 30 | unset($this->$key); |
|
456 | } |
||
457 | 394 | } |
|
458 | |||
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 | 394 | private function constructValue($key, $value, $options) |
|
469 | { |
||
470 | 394 | return (self::$nestedUpdatableAttributes->includes($key) && is_array($value)) |
|
471 | 284 | ? AttachedObject::scopedConstructFrom($value, $options) |
|
472 | 394 | : Util::convertToStripeObject($value, $options); |
|
473 | } |
||
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) |
||
499 | { |
||
500 | return call_user_func_array( |
||
501 | [$class, $method], |
||
502 | array_slice(func_get_args(), 2) |
||
503 | ); |
||
504 | } |
||
505 | |||
506 | /* ----------------------------------------------------------------- |
||
507 | | Check Methods |
||
508 | | ----------------------------------------------------------------- |
||
509 | */ |
||
510 | |||
511 | /** |
||
512 | * Check if array has id. |
||
513 | * |
||
514 | * @param array $array |
||
515 | * |
||
516 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
517 | */ |
||
518 | 8 | private function checkIdIsInArray($array) |
|
519 | { |
||
520 | 8 | if ( ! array_key_exists('id', $array)) |
|
521 | 2 | throw new ApiException('The attribute id must be included.'); |
|
522 | 6 | } |
|
523 | |||
524 | /** |
||
525 | * Check if object has retrieve parameters. |
||
526 | * |
||
527 | * @return bool |
||
528 | */ |
||
529 | 2 | public function hasRetrieveParams() |
|
533 | |||
534 | /** |
||
535 | * Check if attribute deletion. |
||
536 | * |
||
537 | * @param string $key |
||
538 | * @param mixed|null $value |
||
539 | * |
||
540 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
541 | */ |
||
542 | 434 | private function checkIfAttributeDeletion($key, $value) |
|
543 | { |
||
544 | // Don't use empty($value) instead of ($value === '') |
||
545 | 434 | if ( ! is_null($value) && $value === '') |
|
546 | 2 | throw new InvalidArgumentException( |
|
547 | 2 | "You cannot set '{$key}' to an empty string. " |
|
548 | 2 | . 'We interpret empty strings as \'null\' in requests. ' |
|
549 | 2 | . "You may set obj->{$key} = null to delete the property" |
|
550 | ); |
||
551 | 434 | } |
|
552 | |||
553 | /** |
||
554 | * Check metadata attribute. |
||
555 | * |
||
556 | * @param string $key |
||
557 | * @param mixed|null $value |
||
558 | * |
||
559 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
560 | */ |
||
561 | 434 | private function checkMetadataAttribute($key, $value) |
|
562 | { |
||
563 | if ( |
||
564 | 434 | $key === 'metadata' && |
|
565 | 434 | ( ! is_array($value) && ! is_null($value)) |
|
566 | ) |
||
567 | 2 | throw new InvalidArgumentException( |
|
568 | 2 | 'The metadata value must be an array or null, '.gettype($value).' is given' |
|
569 | ); |
||
570 | 434 | } |
|
571 | |||
572 | /** |
||
573 | * Check permanent attributes. |
||
574 | * |
||
575 | * @param string $key |
||
576 | */ |
||
577 | 434 | private function checkPermanentAttributes($key) |
|
578 | { |
||
579 | 434 | if ( ! self::$permanentAttributes->includes($key)) |
|
580 | 98 | $this->unsavedValues->add($key); |
|
581 | 434 | } |
|
582 | |||
583 | /** |
||
584 | * Check unsaved attributes. |
||
585 | * |
||
586 | * @param array $supported |
||
587 | * |
||
588 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
589 | */ |
||
590 | 434 | private function checkUnsavedAttributes($supported) |
|
591 | { |
||
592 | 434 | if ($this->checkUnsavedAttributes === false || count($supported) === 0) |
|
593 | 434 | return; |
|
594 | |||
595 | 16 | $this->checkNotFoundAttributesException( |
|
596 | 16 | $this->unsavedValues->diffKeys($supported) |
|
597 | ); |
||
598 | 12 | } |
|
599 | |||
600 | /** |
||
601 | * Check not found attributes exception. |
||
602 | * |
||
603 | * @param array $notFound |
||
604 | * |
||
605 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
606 | */ |
||
607 | 16 | private function checkNotFoundAttributesException($notFound) |
|
608 | { |
||
609 | 16 | if (count($notFound)) |
|
610 | 4 | throw new InvalidArgumentException( |
|
611 | 4 | 'The attributes ['.implode(', ', $notFound).'] are not supported.' |
|
612 | ); |
||
613 | 12 | } |
|
614 | |||
615 | /* ----------------------------------------------------------------- |
||
616 | | Other Methods |
||
617 | | ----------------------------------------------------------------- |
||
618 | */ |
||
619 | |||
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 | 88 | protected function serializeParameters() |
|
627 | { |
||
628 | 88 | $params = []; |
|
629 | |||
630 | 88 | $this->serializeUnsavedValues($params); |
|
631 | 88 | $this->serializeNestedUpdatableAttributes($params); |
|
632 | |||
633 | 88 | return $params; |
|
634 | } |
||
635 | |||
636 | /** |
||
637 | * Serialize unsaved values. |
||
638 | * |
||
639 | * @param array $params |
||
640 | */ |
||
641 | 88 | private function serializeUnsavedValues(&$params) |
|
647 | |||
648 | /** |
||
649 | * Serialize nested updatable attributes. |
||
650 | * |
||
651 | * @param array $params |
||
652 | */ |
||
653 | 88 | private function serializeNestedUpdatableAttributes(&$params) |
|
654 | { |
||
655 | 88 | foreach (self::$nestedUpdatableAttributes->toArray() as $property) { |
|
656 | if ( |
||
657 | 88 | isset($this->$property) && |
|
658 | 88 | $this->$property instanceof self && |
|
659 | 88 | $serialized = $this->$property->serializeParameters() |
|
660 | ) { |
||
661 | 88 | $params[$property] = $serialized; |
|
662 | } |
||
663 | } |
||
664 | 88 | } |
|
665 | |||
666 | /** |
||
667 | * Show undefined property warning message. |
||
668 | * |
||
669 | * @param string $class |
||
670 | * @param string $key |
||
671 | */ |
||
672 | 6 | private function showUndefinedPropertyMsg($class, $key) |
|
673 | { |
||
674 | 6 | $message = "Stripe Notice: Undefined property of {$class} instance: {$key}."; |
|
675 | |||
685 | |||
686 | /** |
||
687 | * Show available attributes for undefined property warning message. |
||
688 | * |
||
689 | * @return string |
||
690 | */ |
||
691 | 4 | private function showUndefinedPropertyMsgAttributes() |
|
697 | } |
||
698 |