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; |
||
| 23 | class StripeObject implements ObjectInterface, ArrayAccess, JsonSerializable, Arrayable, Jsonable |
||
| 24 | { |
||
| 25 | /* ------------------------------------------------------------------------------------------------ |
||
| 26 | | Constants |
||
| 27 | | ------------------------------------------------------------------------------------------------ |
||
| 28 | */ |
||
| 29 | const ATTACHED_OBJECT_CLASS = 'Arcanedev\\Stripe\\AttachedObject'; |
||
| 30 | |||
| 31 | /* ------------------------------------------------------------------------------------------------ |
||
| 32 | | Properties |
||
| 33 | | ------------------------------------------------------------------------------------------------ |
||
| 34 | */ |
||
| 35 | /** |
||
| 36 | * @var RequestOptions|string|array |
||
| 37 | */ |
||
| 38 | protected $opts; |
||
| 39 | |||
| 40 | /** @var array */ |
||
| 41 | protected $values; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Unsaved Values. |
||
| 45 | * |
||
| 46 | * @var UtilSet |
||
| 47 | */ |
||
| 48 | protected $unsavedValues; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Transient (Deleted) Values. |
||
| 52 | * |
||
| 53 | * @var UtilSet |
||
| 54 | */ |
||
| 55 | protected $transientValues; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Retrieve parameters used to query the object. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $retrieveParameters; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Attributes that should not be sent to the API because |
||
| 66 | * they're not updatable (e.g. API key, ID). |
||
| 67 | * |
||
| 68 | * @var UtilSet |
||
| 69 | */ |
||
| 70 | public static $permanentAttributes; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Attributes that are nested but still updatable from the |
||
| 74 | * parent class's URL (e.g. metadata). |
||
| 75 | * |
||
| 76 | * @var UtilSet |
||
| 77 | */ |
||
| 78 | public static $nestedUpdatableAttributes; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Allow to check attributes while setting. |
||
| 82 | * |
||
| 83 | * @var bool |
||
| 84 | */ |
||
| 85 | protected $checkUnsavedAttributes = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The last response. |
||
| 89 | * |
||
| 90 | * @var \Arcanedev\Stripe\Http\Response |
||
| 91 | */ |
||
| 92 | protected $lastResponse; |
||
| 93 | |||
| 94 | /* ------------------------------------------------------------------------------------------------ |
||
| 95 | | Constructor |
||
| 96 | | ------------------------------------------------------------------------------------------------ |
||
| 97 | */ |
||
| 98 | /** |
||
| 99 | * Make a Stripe object instance. |
||
| 100 | * |
||
| 101 | * @param string|null $id |
||
| 102 | * @param string|array|null $options |
||
| 103 | */ |
||
| 104 | 895 | public function __construct($id = null, $options = null) |
|
| 121 | |||
| 122 | /* ------------------------------------------------------------------------------------------------ |
||
| 123 | | Getters & Setters (+Magics) |
||
| 124 | | ------------------------------------------------------------------------------------------------ |
||
| 125 | */ |
||
| 126 | /** |
||
| 127 | * Set the Id. |
||
| 128 | * |
||
| 129 | * @param array|string|null $id |
||
| 130 | * |
||
| 131 | * @throws ApiException |
||
| 132 | * |
||
| 133 | * @return self |
||
| 134 | */ |
||
| 135 | 895 | private function setId($id) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Set the Id from Array. |
||
| 148 | * |
||
| 149 | * @param array|string|null $id |
||
| 150 | * |
||
| 151 | * @throws ApiException |
||
| 152 | */ |
||
| 153 | 895 | private function setIdIfArray(&$id) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Get Retrieve Parameters. |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | 5 | protected function getRetrieveParams() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Standard get accessor. |
||
| 175 | * |
||
| 176 | * @param string|int $key |
||
| 177 | * |
||
| 178 | * @return mixed|null |
||
| 179 | */ |
||
| 180 | 614 | public function __get($key) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Standard set accessor. |
||
| 193 | * |
||
| 194 | * @param string $key |
||
| 195 | * @param mixed $value |
||
| 196 | * |
||
| 197 | * @throws InvalidArgumentException |
||
| 198 | */ |
||
| 199 | 744 | public function __set($key, $value) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Set value. |
||
| 209 | * |
||
| 210 | * @param string $key |
||
| 211 | * @param mixed $value |
||
| 212 | * |
||
| 213 | * @throws InvalidArgumentException |
||
| 214 | */ |
||
| 215 | 744 | private function setValue($key, $value) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get the last response from the Stripe API. |
||
| 237 | * |
||
| 238 | * @return \Arcanedev\Stripe\Http\Response |
||
| 239 | */ |
||
| 240 | 15 | public function getLastResponse() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Set the last response from the Stripe API. |
||
| 247 | * |
||
| 248 | * @param \Arcanedev\Stripe\Http\Response $response |
||
| 249 | * |
||
| 250 | * @return self |
||
| 251 | */ |
||
| 252 | 619 | public function setLastResponse(\Arcanedev\Stripe\Http\Response $response) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Check has a value by key. |
||
| 261 | * |
||
| 262 | * @param string $key |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | 180 | public function __isset($key) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Unset element from values. |
||
| 273 | * |
||
| 274 | * @param string $key |
||
| 275 | */ |
||
| 276 | 55 | public function __unset($key) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Convert StripeObject to string. |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | 5 | public function __toString() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Json serialize. |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 5 | public function jsonSerialize() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Convert StripeObject to array. |
||
| 306 | * |
||
| 307 | * @param bool $recursive |
||
| 308 | * |
||
| 309 | * @return array |
||
| 310 | */ |
||
| 311 | 25 | public function toArray($recursive = false) |
|
| 312 | { |
||
| 313 | 5 | return $recursive |
|
| 314 | 24 | ? Util::convertStripeObjectToArray($this->values) |
|
| 315 | 25 | : $this->values; |
|
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Convert StripeObject to JSON. |
||
| 320 | * |
||
| 321 | * @param int $options |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | 5 | public function toJson($options = 0) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Get only value keys. |
||
| 336 | * |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | 764 | public function keys() |
|
| 343 | |||
| 344 | /* ------------------------------------------------------------------------------------------------ |
||
| 345 | | ArrayAccess methods |
||
| 346 | | ------------------------------------------------------------------------------------------------ |
||
| 347 | */ |
||
| 348 | 65 | public function offsetSet($key, $value) |
|
| 352 | |||
| 353 | 609 | public function offsetExists($key) |
|
| 357 | |||
| 358 | 5 | public function offsetUnset($key) |
|
| 362 | |||
| 363 | 480 | public function offsetGet($key) |
|
| 369 | |||
| 370 | /* ------------------------------------------------------------------------------------------------ |
||
| 371 | | Main Functions |
||
| 372 | | ------------------------------------------------------------------------------------------------ |
||
| 373 | */ |
||
| 374 | /** |
||
| 375 | * This unfortunately needs to be public to be used in Util.php |
||
| 376 | * Return The object constructed from the given values. |
||
| 377 | * |
||
| 378 | * @param string $class |
||
| 379 | * @param array $values |
||
| 380 | * @param string $options |
||
| 381 | * |
||
| 382 | * @return self |
||
| 383 | */ |
||
| 384 | 619 | public static function scopedConstructFrom($class, $values, $options) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Refreshes this object using the provided values. |
||
| 395 | * |
||
| 396 | * @param array $values |
||
| 397 | * @param RequestOptions|array|string|null $opts |
||
| 398 | * @param boolean $partial |
||
| 399 | */ |
||
| 400 | 619 | public function refreshFrom($values, $opts, $partial = false) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Clean refreshed StripeObject. |
||
| 423 | * |
||
| 424 | * @param array $values |
||
| 425 | * @param bool|false $partial |
||
| 426 | */ |
||
| 427 | 619 | private function cleanObject($values, $partial) |
|
| 428 | { |
||
| 429 | // Wipe old state before setting new. |
||
| 430 | // This is useful for e.g. updating a customer, where there is no persistent card parameter. |
||
| 431 | // Mark those values which don't persist as transient |
||
| 432 | 124 | $removed = ! $partial |
|
| 433 | 619 | ? array_diff($this->keys(), array_keys($values)) |
|
| 434 | 619 | : new UtilSet; |
|
| 435 | |||
| 436 | 619 | foreach ($removed as $key) { |
|
| 437 | 35 | if (self::$permanentAttributes->includes($key)) { |
|
| 438 | continue; |
||
| 439 | } |
||
| 440 | 35 | unset($this->$key); |
|
| 441 | 495 | } |
|
| 442 | 619 | } |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Construct Value. |
||
| 446 | * |
||
| 447 | * @param string $key |
||
| 448 | * @param mixed $value |
||
| 449 | * @param array $opts |
||
| 450 | * |
||
| 451 | * @return self|StripeResource|Collection|array |
||
| 452 | */ |
||
| 453 | 619 | private function constructValue($key, $value, $opts) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Pretend to have late static bindings. |
||
| 462 | * |
||
| 463 | * @param string $method |
||
| 464 | * |
||
| 465 | * @return mixed |
||
| 466 | */ |
||
| 467 | 390 | protected function lsb($method) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Scoped Late Static Bindings. |
||
| 477 | * |
||
| 478 | * @param string $class |
||
| 479 | * @param string $method |
||
| 480 | * |
||
| 481 | * @return mixed |
||
| 482 | */ |
||
| 483 | protected static function scopedLsb($class, $method) |
||
| 489 | |||
| 490 | /* ------------------------------------------------------------------------------------------------ |
||
| 491 | | Check Functions |
||
| 492 | | ------------------------------------------------------------------------------------------------ |
||
| 493 | */ |
||
| 494 | /** |
||
| 495 | * Check if array has id. |
||
| 496 | * |
||
| 497 | * @param array $array |
||
| 498 | * |
||
| 499 | * @throws ApiException |
||
| 500 | */ |
||
| 501 | 15 | private function checkIdIsInArray($array) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Check if object has retrieve parameters. |
||
| 510 | * |
||
| 511 | * @return bool |
||
| 512 | */ |
||
| 513 | 5 | public function hasRetrieveParams() |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Check if attribute deletion. |
||
| 520 | * |
||
| 521 | * @param string $key |
||
| 522 | * @param mixed|null $value |
||
| 523 | * |
||
| 524 | * @throws InvalidArgumentException |
||
| 525 | */ |
||
| 526 | 744 | private function checkIfAttributeDeletion($key, $value) |
|
| 527 | { |
||
| 528 | // Don't use empty($value) instead of ($value === '') |
||
| 529 | 744 | if ( ! is_null($value) && $value === '') { |
|
| 530 | 5 | throw new InvalidArgumentException( |
|
| 531 | 4 | "You cannot set '$key' to an empty string. " |
|
| 532 | 1 | . 'We interpret empty strings as \'null\' in requests. ' |
|
| 533 | 5 | . "You may set obj->$key = null to delete the property" |
|
| 534 | 4 | ); |
|
| 535 | } |
||
| 536 | 744 | } |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Check metadata attribute. |
||
| 540 | * |
||
| 541 | * @param string $key |
||
| 542 | * @param mixed|null $value |
||
| 543 | * |
||
| 544 | * @throws InvalidArgumentException |
||
| 545 | */ |
||
| 546 | 744 | private function checkMetadataAttribute($key, $value) |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Check permanent attributes. |
||
| 560 | * |
||
| 561 | * @param string $key |
||
| 562 | */ |
||
| 563 | 744 | private function checkPermanentAttributes($key) |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Check unsaved attributes. |
||
| 572 | * |
||
| 573 | * @param array $supported |
||
| 574 | * |
||
| 575 | * @throws InvalidArgumentException |
||
| 576 | */ |
||
| 577 | 744 | private function checkUnsavedAttributes($supported) |
|
| 590 | |||
| 591 | /* ------------------------------------------------------------------------------------------------ |
||
| 592 | | Other Functions |
||
| 593 | | ------------------------------------------------------------------------------------------------ |
||
| 594 | */ |
||
| 595 | /** |
||
| 596 | * A recursive mapping of attributes to values for this object, |
||
| 597 | * including the proper value for deleted attributes. |
||
| 598 | * |
||
| 599 | * @return array |
||
| 600 | */ |
||
| 601 | 165 | protected function serializeParameters() |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Serialize unsaved values. |
||
| 613 | * |
||
| 614 | * @param array $params |
||
| 615 | */ |
||
| 616 | 165 | private function serializeUnsavedValues(&$params) |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Serialize nested updatable attributes. |
||
| 625 | * |
||
| 626 | * @param array $params |
||
| 627 | */ |
||
| 628 | 165 | private function serializeNestedUpdatableAttributes(&$params) |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Show undefined property warning message. |
||
| 643 | * |
||
| 644 | * @param string $class |
||
| 645 | * @param string $key |
||
| 646 | */ |
||
| 647 | 10 | private function showUndefinedPropertyMsg($class, $key) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Show available attributes for undefined property warning message. |
||
| 665 | * |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | 5 | private function showUndefinedPropertyMsgAttributes() |
|
| 674 | |||
| 675 | /** |
||
| 676 | * Check not found attributes exception. |
||
| 677 | * |
||
| 678 | * @param array $notFound |
||
| 679 | * |
||
| 680 | * @throws InvalidArgumentException |
||
| 681 | */ |
||
| 682 | 40 | private function checkNotFoundAttributesException($notFound) |
|
| 690 | } |
||
| 691 |