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 ObjectInterface, ArrayAccess, JsonSerializable, Arrayable, Jsonable |
||
| 25 | { |
||
| 26 | /* ------------------------------------------------------------------------------------------------ |
||
| 27 | | Constants |
||
| 28 | | ------------------------------------------------------------------------------------------------ |
||
| 29 | */ |
||
| 30 | const ATTACHED_OBJECT_CLASS = 'Arcanedev\\Stripe\\AttachedObject'; |
||
| 31 | |||
| 32 | /* ------------------------------------------------------------------------------------------------ |
||
| 33 | | Properties |
||
| 34 | | ------------------------------------------------------------------------------------------------ |
||
| 35 | */ |
||
| 36 | /** |
||
| 37 | * @var RequestOptions|string|array |
||
| 38 | */ |
||
| 39 | protected $opts; |
||
| 40 | |||
| 41 | /** @var array */ |
||
| 42 | protected $values; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Unsaved Values. |
||
| 46 | * |
||
| 47 | * @var UtilSet |
||
| 48 | */ |
||
| 49 | protected $unsavedValues; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Transient (Deleted) Values. |
||
| 53 | * |
||
| 54 | * @var UtilSet |
||
| 55 | */ |
||
| 56 | protected $transientValues; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Retrieve parameters used to query the object. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $retrieveParameters; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Attributes that should not be sent to the API because |
||
| 67 | * they're not updatable (e.g. API key, ID). |
||
| 68 | * |
||
| 69 | * @var UtilSet |
||
| 70 | */ |
||
| 71 | public static $permanentAttributes; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Attributes that are nested but still updatable from the |
||
| 75 | * parent class's URL (e.g. metadata). |
||
| 76 | * |
||
| 77 | * @var UtilSet |
||
| 78 | */ |
||
| 79 | public static $nestedUpdatableAttributes; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Allow to check attributes while setting. |
||
| 83 | * |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | protected $checkUnsavedAttributes = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The last response. |
||
| 90 | * |
||
| 91 | * @var \Arcanedev\Stripe\Http\Response |
||
| 92 | */ |
||
| 93 | protected $lastResponse; |
||
| 94 | |||
| 95 | /* ------------------------------------------------------------------------------------------------ |
||
| 96 | | Constructor |
||
| 97 | | ------------------------------------------------------------------------------------------------ |
||
| 98 | */ |
||
| 99 | /** |
||
| 100 | * Make a Stripe object instance. |
||
| 101 | * |
||
| 102 | * @param string|null $id |
||
| 103 | * @param string|array|null $options |
||
| 104 | */ |
||
| 105 | 935 | public function __construct($id = null, $options = null) |
|
| 122 | |||
| 123 | /* ------------------------------------------------------------------------------------------------ |
||
| 124 | | Getters & Setters (+Magics) |
||
| 125 | | ------------------------------------------------------------------------------------------------ |
||
| 126 | */ |
||
| 127 | /** |
||
| 128 | * Set the Id. |
||
| 129 | * |
||
| 130 | * @param array|string|null $id |
||
| 131 | * |
||
| 132 | * @throws ApiException |
||
| 133 | * |
||
| 134 | * @return self |
||
| 135 | */ |
||
| 136 | 935 | private function setId($id) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Set the Id from Array. |
||
| 149 | * |
||
| 150 | * @param array|string|null $id |
||
| 151 | * |
||
| 152 | * @throws ApiException |
||
| 153 | */ |
||
| 154 | 935 | private function setIdIfArray(&$id) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Get Retrieve Parameters. |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 5 | protected function getRetrieveParams() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Standard get accessor. |
||
| 176 | * |
||
| 177 | * @param string|int $key |
||
| 178 | * |
||
| 179 | * @return mixed|null |
||
| 180 | */ |
||
| 181 | 654 | public function &__get($key) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Standard set accessor. |
||
| 196 | * |
||
| 197 | * @param string $key |
||
| 198 | * @param mixed $value |
||
| 199 | * |
||
| 200 | * @throws InvalidArgumentException |
||
| 201 | */ |
||
| 202 | 779 | public function __set($key, $value) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Set value. |
||
| 212 | * |
||
| 213 | * @param string $key |
||
| 214 | * @param mixed $value |
||
| 215 | * |
||
| 216 | * @throws InvalidArgumentException |
||
| 217 | */ |
||
| 218 | 779 | private function setValue($key, $value) |
|
| 219 | { |
||
| 220 | 779 | $this->checkIfAttributeDeletion($key, $value); |
|
| 221 | 779 | $this->checkMetadataAttribute($key, $value); |
|
| 222 | |||
| 223 | if ( |
||
| 224 | 779 | self::$nestedUpdatableAttributes->includes($key) && |
|
| 225 | 779 | isset($this->$key) && |
|
| 226 | 779 | $this->$key instanceof AttachedObject && |
|
| 227 | 172 | is_array($value) |
|
| 228 | 623 | ) { |
|
| 229 | 15 | $this->$key->replaceWith($value); |
|
| 230 | 12 | } |
|
| 231 | else { |
||
| 232 | // TODO: may want to clear from $transientValues (Won't be user-visible). |
||
| 233 | 779 | $this->values[$key] = $value; |
|
| 234 | } |
||
| 235 | |||
| 236 | 779 | $this->checkPermanentAttributes($key); |
|
| 237 | 779 | } |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Get the last response from the Stripe API. |
||
| 241 | * |
||
| 242 | * @return \Arcanedev\Stripe\Http\Response |
||
| 243 | */ |
||
| 244 | 15 | public function getLastResponse() |
|
| 245 | { |
||
| 246 | 15 | return $this->lastResponse; |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set the last response from the Stripe API. |
||
| 251 | * |
||
| 252 | * @param \Arcanedev\Stripe\Http\Response $response |
||
| 253 | * |
||
| 254 | * @return self |
||
| 255 | */ |
||
| 256 | 649 | public function setLastResponse(Response $response) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Check has a value by key. |
||
| 265 | * |
||
| 266 | * @param string $key |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | 175 | public function __isset($key) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Unset element from values. |
||
| 277 | * |
||
| 278 | * @param string $key |
||
| 279 | */ |
||
| 280 | 70 | public function __unset($key) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Convert StripeObject to string. |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | 5 | public function __toString() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Json serialize. |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | 5 | public function jsonSerialize() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Convert StripeObject to array. |
||
| 310 | * |
||
| 311 | * @param bool $recursive |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | 25 | public function toArray($recursive = false) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Convert StripeObject to JSON. |
||
| 324 | * |
||
| 325 | * @param int $options |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 5 | public function toJson($options = 0) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Get only value keys. |
||
| 340 | * |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | 804 | public function keys() |
|
| 347 | |||
| 348 | /* ------------------------------------------------------------------------------------------------ |
||
| 349 | | ArrayAccess methods |
||
| 350 | | ------------------------------------------------------------------------------------------------ |
||
| 351 | */ |
||
| 352 | 60 | public function offsetSet($key, $value) |
|
| 356 | |||
| 357 | 639 | public function offsetExists($key) |
|
| 361 | |||
| 362 | 5 | public function offsetUnset($key) |
|
| 366 | |||
| 367 | 485 | public function offsetGet($key) |
|
| 373 | |||
| 374 | /* ------------------------------------------------------------------------------------------------ |
||
| 375 | | Main Functions |
||
| 376 | | ------------------------------------------------------------------------------------------------ |
||
| 377 | */ |
||
| 378 | /** |
||
| 379 | * This unfortunately needs to be public to be used in Util.php |
||
| 380 | * Return The object constructed from the given values. |
||
| 381 | * |
||
| 382 | * @param string $class |
||
| 383 | * @param array $values |
||
| 384 | * @param string $options |
||
| 385 | * |
||
| 386 | * @return self |
||
| 387 | */ |
||
| 388 | 654 | public static function scopedConstructFrom($class, $values, $options) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Refreshes this object using the provided values. |
||
| 399 | * |
||
| 400 | * @param array $values |
||
| 401 | * @param RequestOptions|array|string|null $opts |
||
| 402 | * @param boolean $partial |
||
| 403 | */ |
||
| 404 | 654 | public function refreshFrom($values, $opts, $partial = false) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Clean refreshed StripeObject. |
||
| 427 | * |
||
| 428 | * @param array $values |
||
| 429 | * @param bool|false $partial |
||
| 430 | */ |
||
| 431 | 654 | private function cleanObject($values, $partial) |
|
| 432 | { |
||
| 433 | // Wipe old state before setting new. |
||
| 434 | // This is useful for e.g. updating a customer, where there is no persistent card parameter. |
||
| 435 | // Mark those values which don't persist as transient |
||
| 436 | 131 | $removed = ! $partial |
|
| 437 | 654 | ? array_diff($this->keys(), array_keys($values)) |
|
| 438 | 654 | : new UtilSet; |
|
| 439 | |||
| 440 | 654 | foreach ($removed as $key) { |
|
| 441 | 50 | if (self::$permanentAttributes->includes($key)) { |
|
| 442 | continue; |
||
| 443 | } |
||
| 444 | 50 | unset($this->$key); |
|
| 445 | 523 | } |
|
| 446 | 654 | } |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Construct Value. |
||
| 450 | * |
||
| 451 | * @param string $key |
||
| 452 | * @param mixed $value |
||
| 453 | * @param array $opts |
||
| 454 | * |
||
| 455 | * @return self|StripeResource|Collection|array |
||
| 456 | */ |
||
| 457 | 654 | private function constructValue($key, $value, $opts) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Pretend to have late static bindings. |
||
| 466 | * |
||
| 467 | * @param string $method |
||
| 468 | * |
||
| 469 | * @return mixed |
||
| 470 | */ |
||
| 471 | 390 | protected function lsb($method) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Scoped Late Static Bindings. |
||
| 481 | * |
||
| 482 | * @param string $class |
||
| 483 | * @param string $method |
||
| 484 | * |
||
| 485 | * @return mixed |
||
| 486 | */ |
||
| 487 | protected static function scopedLsb($class, $method) |
||
| 493 | |||
| 494 | /* ------------------------------------------------------------------------------------------------ |
||
| 495 | | Check Functions |
||
| 496 | | ------------------------------------------------------------------------------------------------ |
||
| 497 | */ |
||
| 498 | /** |
||
| 499 | * Check if array has id. |
||
| 500 | * |
||
| 501 | * @param array $array |
||
| 502 | * |
||
| 503 | * @throws ApiException |
||
| 504 | */ |
||
| 505 | 15 | private function checkIdIsInArray($array) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Check if object has retrieve parameters. |
||
| 514 | * |
||
| 515 | * @return bool |
||
| 516 | */ |
||
| 517 | 5 | public function hasRetrieveParams() |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Check if attribute deletion. |
||
| 524 | * |
||
| 525 | * @param string $key |
||
| 526 | * @param mixed|null $value |
||
| 527 | * |
||
| 528 | * @throws InvalidArgumentException |
||
| 529 | */ |
||
| 530 | 779 | private function checkIfAttributeDeletion($key, $value) |
|
| 531 | { |
||
| 532 | // Don't use empty($value) instead of ($value === '') |
||
| 533 | 779 | if ( ! is_null($value) && $value === '') { |
|
| 534 | 5 | throw new InvalidArgumentException( |
|
| 535 | 5 | "You cannot set '$key' to an empty string. " |
|
| 536 | 1 | . 'We interpret empty strings as \'null\' in requests. ' |
|
| 537 | 5 | . "You may set obj->$key = null to delete the property" |
|
| 538 | 4 | ); |
|
| 539 | } |
||
| 540 | 779 | } |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Check metadata attribute. |
||
| 544 | * |
||
| 545 | * @param string $key |
||
| 546 | * @param mixed|null $value |
||
| 547 | * |
||
| 548 | * @throws InvalidArgumentException |
||
| 549 | */ |
||
| 550 | 779 | private function checkMetadataAttribute($key, $value) |
|
| 551 | { |
||
| 552 | if ( |
||
| 553 | 779 | $key === 'metadata' && |
|
| 554 | 184 | ( ! is_array($value) && ! is_null($value)) |
|
| 555 | 623 | ) { |
|
| 556 | 5 | throw new InvalidArgumentException( |
|
| 557 | 5 | 'The metadata value must be an array or null, ' . gettype($value) . ' is given' |
|
| 558 | 4 | ); |
|
| 559 | } |
||
| 560 | 779 | } |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Check permanent attributes. |
||
| 564 | * |
||
| 565 | * @param string $key |
||
| 566 | */ |
||
| 567 | 779 | private function checkPermanentAttributes($key) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Check unsaved attributes. |
||
| 576 | * |
||
| 577 | * @param array $supported |
||
| 578 | * |
||
| 579 | * @throws InvalidArgumentException |
||
| 580 | */ |
||
| 581 | 779 | private function checkUnsavedAttributes($supported) |
|
| 582 | { |
||
| 583 | if ( |
||
| 584 | 779 | $this->checkUnsavedAttributes === false || |
|
| 585 | 403 | count($supported) == 0 |
|
| 586 | 623 | ) { |
|
| 587 | 779 | return; |
|
| 588 | } |
||
| 589 | |||
| 590 | 40 | $this->checkNotFoundAttributesException( |
|
| 591 | 40 | $this->unsavedValues->diffKeys($supported) |
|
| 592 | 32 | ); |
|
| 593 | 30 | } |
|
| 594 | |||
| 595 | /* ------------------------------------------------------------------------------------------------ |
||
| 596 | | Other Functions |
||
| 597 | | ------------------------------------------------------------------------------------------------ |
||
| 598 | */ |
||
| 599 | /** |
||
| 600 | * A recursive mapping of attributes to values for this object, |
||
| 601 | * including the proper value for deleted attributes. |
||
| 602 | * |
||
| 603 | * @return array |
||
| 604 | */ |
||
| 605 | 155 | protected function serializeParameters() |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Serialize unsaved values. |
||
| 617 | * |
||
| 618 | * @param array $params |
||
| 619 | */ |
||
| 620 | 155 | private function serializeUnsavedValues(&$params) |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Serialize nested updatable attributes. |
||
| 629 | * |
||
| 630 | * @param array $params |
||
| 631 | */ |
||
| 632 | 155 | private function serializeNestedUpdatableAttributes(&$params) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Show undefined property warning message. |
||
| 647 | * |
||
| 648 | * @param string $class |
||
| 649 | * @param string $key |
||
| 650 | */ |
||
| 651 | 10 | private function showUndefinedPropertyMsg($class, $key) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Show available attributes for undefined property warning message. |
||
| 669 | * |
||
| 670 | * @return string |
||
| 671 | */ |
||
| 672 | 5 | private function showUndefinedPropertyMsgAttributes() |
|
| 678 | |||
| 679 | /** |
||
| 680 | * Check not found attributes exception. |
||
| 681 | * |
||
| 682 | * @param array $notFound |
||
| 683 | * |
||
| 684 | * @throws InvalidArgumentException |
||
| 685 | */ |
||
| 686 | 40 | private function checkNotFoundAttributesException($notFound) |
|
| 694 | } |
||
| 695 |