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 | 448 | public function __construct($id = null, $options = null) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Init the stripe object. |
||
| 110 | */ |
||
| 111 | 448 | private function init() |
|
| 112 | { |
||
| 113 | 448 | $this->values = []; |
|
| 114 | 448 | self::$permanentAttributes = new UtilSet(['opts', 'id']); |
|
| 115 | 448 | self::$nestedUpdatableAttributes = new UtilSet([ |
|
| 116 | 448 | 'metadata', 'legal_entity', 'address', 'dob', 'payout_schedule', 'transfer_schedule', |
|
| 117 | 'verification', 'tos_acceptance', 'personal_address', 'address_kana', 'address_kanji', |
||
| 118 | // will make the array into an AttachedObject: weird, but works for now |
||
| 119 | 'additional_owners', 0, 1, 2, 3, 4, // Max 3, but leave the 4th so errors work properly |
||
| 120 | 'shipping', 'inventory', |
||
| 121 | 'owner', |
||
| 122 | ]); |
||
| 123 | |||
| 124 | 448 | $this->unsavedValues = new UtilSet; |
|
| 125 | 448 | $this->transientValues = new UtilSet; |
|
| 126 | 448 | $this->retrieveParameters = []; |
|
| 127 | 448 | } |
|
| 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 | 448 | 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 | 448 | private function setIdIfArray(&$id) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Get Retrieve Parameters. |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 2 | protected function getRetrieveParams() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Standard get accessor. |
||
| 181 | * |
||
| 182 | * @param string|int $key |
||
| 183 | * |
||
| 184 | * @return mixed|null |
||
| 185 | */ |
||
| 186 | 338 | public function &__get($key) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Standard set accessor. |
||
| 200 | * |
||
| 201 | * @param string $key |
||
| 202 | * @param mixed $value |
||
| 203 | */ |
||
| 204 | 396 | 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 | 396 | private function setValue($key, $value) |
|
| 221 | { |
||
| 222 | 396 | $this->checkIfAttributeDeletion($key, $value); |
|
| 223 | 396 | $this->checkMetadataAttribute($key, $value); |
|
| 224 | |||
| 225 | if ( |
||
| 226 | 396 | self::$nestedUpdatableAttributes->includes($key) && |
|
| 227 | 396 | isset($this->$key) && |
|
| 228 | 396 | $this->$key instanceof AttachedObject && |
|
| 229 | 396 | is_array($value) |
|
| 230 | ) { |
||
| 231 | 10 | $this->$key->replaceWith($value); |
|
| 232 | } |
||
| 233 | else { |
||
| 234 | // TODO: may want to clear from $transientValues (Won't be user-visible). |
||
| 235 | 396 | $this->values[$key] = $value; |
|
| 236 | } |
||
| 237 | |||
| 238 | 396 | $this->checkPermanentAttributes($key); |
|
| 239 | 396 | } |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Get the last response from the Stripe API. |
||
| 243 | * |
||
| 244 | * @return \Arcanedev\Stripe\Http\Response |
||
| 245 | */ |
||
| 246 | 6 | 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 | 338 | 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 | 104 | public function __isset($key) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Unset element from values. |
||
| 279 | * |
||
| 280 | * @param string $key |
||
| 281 | */ |
||
| 282 | 38 | public function __unset($key) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Convert StripeObject to string. |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | 2 | public function __toString() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Json serialize. |
||
| 302 | * |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | 2 | public function jsonSerialize() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Convert StripeObject to array. |
||
| 312 | * |
||
| 313 | * @param bool $recursive |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | 10 | public function toArray($recursive = false) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Convert StripeObject to JSON. |
||
| 326 | * |
||
| 327 | * @param int $options |
||
| 328 | * |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | 2 | public function toJson($options = 0) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Get only value keys. |
||
| 341 | * |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | 406 | public function keys() |
|
| 348 | |||
| 349 | /* ----------------------------------------------------------------- |
||
| 350 | | ArrayAccess methods |
||
| 351 | | ----------------------------------------------------------------- |
||
| 352 | */ |
||
| 353 | |||
| 354 | 34 | public function offsetSet($key, $value) |
|
| 358 | |||
| 359 | 338 | public function offsetExists($key) |
|
| 363 | |||
| 364 | 2 | public function offsetUnset($key) |
|
| 368 | |||
| 369 | 262 | 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 | 332 | 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 | 344 | public function refreshFrom($values, $options, $partial = false) |
|
| 408 | { |
||
| 409 | 344 | $this->opts = is_array($options) ? RequestOptions::parse($options) : $options; |
|
| 410 | |||
| 411 | 344 | $this->cleanObject($values, $partial); |
|
| 412 | |||
| 413 | 344 | foreach ($values as $key => $value) { |
|
| 414 | 344 | if (self::$permanentAttributes->includes($key) && isset($this[$key])) |
|
| 415 | 334 | continue; |
|
| 416 | |||
| 417 | 344 | $this->values[$key] = $this->constructValue($key, $value, $this->opts); |
|
| 418 | |||
| 419 | 344 | $this->transientValues->discard($key); |
|
| 420 | 344 | $this->unsavedValues->discard($key); |
|
| 421 | } |
||
| 422 | 344 | } |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Clean refreshed StripeObject. |
||
| 426 | * |
||
| 427 | * @param array $values |
||
| 428 | * @param bool|false $partial |
||
| 429 | */ |
||
| 430 | 344 | private function cleanObject($values, $partial) |
|
| 431 | { |
||
| 432 | // Wipe old state before setting new. |
||
| 433 | // This is useful for e.g. updating a customer, where there is no persistent card parameter. |
||
| 434 | // Mark those values which don't persist as transient |
||
| 435 | 344 | $removed = ! $partial |
|
| 436 | 344 | ? array_diff($this->keys(), array_keys($values)) |
|
| 437 | 344 | : new UtilSet; |
|
| 438 | |||
| 439 | 344 | foreach ($removed as $key) { |
|
| 440 | 30 | if (self::$permanentAttributes->includes($key)) |
|
| 441 | 2 | continue; |
|
| 442 | |||
| 443 | 30 | unset($this->$key); |
|
| 444 | } |
||
| 445 | 344 | } |
|
| 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 | 344 | 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) |
||
| 471 | { |
||
| 472 | return call_user_func_array( |
||
| 473 | [get_class($this), $method], |
||
| 474 | array_slice(func_get_args(), 1) |
||
| 475 | ); |
||
| 476 | } |
||
| 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) |
||
| 487 | { |
||
| 488 | return call_user_func_array( |
||
| 489 | [$class, $method], |
||
| 490 | array_slice(func_get_args(), 2) |
||
| 491 | ); |
||
| 492 | } |
||
| 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 | 8 | private function checkIdIsInArray($array) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Check if object has retrieve parameters. |
||
| 514 | * |
||
| 515 | * @return bool |
||
| 516 | */ |
||
| 517 | 2 | 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 | 396 | private function checkIfAttributeDeletion($key, $value) |
|
| 531 | { |
||
| 532 | // Don't use empty($value) instead of ($value === '') |
||
| 533 | 396 | if ( ! is_null($value) && $value === '') |
|
| 534 | 2 | throw new InvalidArgumentException( |
|
| 535 | 2 | "You cannot set '{$key}' to an empty string. " |
|
| 536 | 2 | . 'We interpret empty strings as \'null\' in requests. ' |
|
| 537 | 2 | . "You may set obj->{$key} = null to delete the property" |
|
| 538 | ); |
||
| 539 | 396 | } |
|
| 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 | 396 | private function checkMetadataAttribute($key, $value) |
|
| 550 | { |
||
| 551 | if ( |
||
| 552 | 396 | $key === 'metadata' && |
|
| 553 | 396 | ( ! is_array($value) && ! is_null($value)) |
|
| 554 | ) |
||
| 555 | 2 | throw new InvalidArgumentException( |
|
| 556 | 2 | 'The metadata value must be an array or null, '.gettype($value).' is given' |
|
| 557 | ); |
||
| 558 | 396 | } |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Check permanent attributes. |
||
| 562 | * |
||
| 563 | * @param string $key |
||
| 564 | */ |
||
| 565 | 396 | private function checkPermanentAttributes($key) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Check unsaved attributes. |
||
| 573 | * |
||
| 574 | * @param array $supported |
||
| 575 | * |
||
| 576 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
| 577 | */ |
||
| 578 | 396 | private function checkUnsavedAttributes($supported) |
|
| 579 | { |
||
| 580 | 396 | if ($this->checkUnsavedAttributes === false || count($supported) === 0) |
|
| 581 | 396 | return; |
|
| 582 | |||
| 583 | 16 | $this->checkNotFoundAttributesException( |
|
| 584 | 16 | $this->unsavedValues->diffKeys($supported) |
|
| 585 | ); |
||
| 586 | 12 | } |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Check not found attributes exception. |
||
| 590 | * |
||
| 591 | * @param array $notFound |
||
| 592 | * |
||
| 593 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
| 594 | */ |
||
| 595 | 16 | private function checkNotFoundAttributesException($notFound) |
|
| 596 | { |
||
| 597 | 16 | if (count($notFound)) |
|
| 598 | 4 | throw new InvalidArgumentException( |
|
| 599 | 4 | 'The attributes ['.implode(', ', $notFound).'] are not supported.' |
|
| 600 | ); |
||
| 601 | 12 | } |
|
| 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 | 88 | protected function serializeParameters() |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Serialize unsaved values. |
||
| 626 | * |
||
| 627 | * @param array $params |
||
| 628 | */ |
||
| 629 | 88 | private function serializeUnsavedValues(&$params) |
|
| 630 | { |
||
| 631 | 88 | foreach ($this->unsavedValues->toArray() as $key) { |
|
| 632 | 74 | $params[$key] = ! is_null($value = $this->$key) ? $value : ''; |
|
| 633 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Serialize nested updatable attributes. |
||
| 638 | * |
||
| 639 | * @param array $params |
||
| 640 | */ |
||
| 641 | 88 | private function serializeNestedUpdatableAttributes(&$params) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Show undefined property warning message. |
||
| 656 | * |
||
| 657 | * @param string $class |
||
| 658 | * @param string $key |
||
| 659 | */ |
||
| 660 | 6 | private function showUndefinedPropertyMsg($class, $key) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Show available attributes for undefined property warning message. |
||
| 676 | * |
||
| 677 | * @return string |
||
| 678 | */ |
||
| 679 | 4 | private function showUndefinedPropertyMsgAttributes() |
|
| 685 | } |
||
| 686 |