Complex classes like StripeResource 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 StripeResource, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Arcanedev\Stripe; |
||
15 | abstract class StripeResource extends StripeObject implements StripeResourceContract |
||
16 | { |
||
17 | /* ----------------------------------------------------------------- |
||
18 | | Properties |
||
19 | | ----------------------------------------------------------------- |
||
20 | */ |
||
21 | |||
22 | /** @var array */ |
||
23 | private static $persistedHeaders = [ |
||
24 | 'Stripe-Account' => true, |
||
25 | 'Stripe-Version' => true, |
||
26 | ]; |
||
27 | |||
28 | /* ----------------------------------------------------------------- |
||
29 | | Main Methods |
||
30 | | ----------------------------------------------------------------- |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * Get the base url. |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | 396 | public static function baseUrl() |
|
39 | { |
||
40 | 396 | return Stripe::getApiBaseUrl(); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * Get the refreshed resource. |
||
45 | * |
||
46 | * @return static |
||
47 | */ |
||
48 | 176 | public function refresh() |
|
49 | { |
||
50 | 176 | list($response, $this->opts->apiKey) = Requestor::make($this->opts->apiKey, self::baseUrl()) |
|
51 | 176 | ->get($this->instanceUrl(), $this->retrieveParameters, $this->opts->headers); |
|
52 | |||
53 | /** @var \Arcanedev\Stripe\Http\Response $response */ |
||
54 | 150 | $this->setLastResponse($response); |
|
55 | 150 | $this->refreshFrom($response->getJson(), $this->opts); |
|
56 | |||
57 | 150 | return $this; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get The name of the class, with namespacing and underscores stripped. |
||
62 | * |
||
63 | * @param string $class |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | 388 | public static function className($class = '') |
|
68 | { |
||
69 | 388 | $name = self::getShortNameClass($class); |
|
70 | 388 | $name = str_split_camelcase($name, '_'); |
|
71 | |||
72 | 388 | return strtolower(urlencode($name)); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get Class short name. |
||
77 | * |
||
78 | * @param string $class |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | 388 | protected static function getShortNameClass($class = '') |
|
83 | { |
||
84 | 388 | if (empty($class)) |
|
85 | 380 | $class = get_called_class(); |
|
86 | |||
87 | 388 | return (new ReflectionClass($class))->getShortName(); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get the endpoint URL for the given class. |
||
92 | * |
||
93 | * @param string $class |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | 382 | public static function classUrl($class = '') |
|
98 | { |
||
99 | 382 | $base = self::className($class); |
|
100 | |||
101 | 382 | return "/v1/${base}s"; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get Instance URL. |
||
106 | * |
||
107 | * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function instanceUrl() |
||
112 | { |
||
113 | 234 | return static::resourceUrl($this['id']); |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Get the instance endpoint URL for the given class. |
||
118 | * |
||
119 | * @param string $id |
||
120 | * |
||
121 | * @return string |
||
122 | * |
||
123 | * @throws Exceptions\InvalidRequestException |
||
124 | */ |
||
125 | public static function resourceUrl($id) |
||
126 | { |
||
127 | 284 | if ($id === null) { |
|
128 | 6 | $class = get_called_class(); |
|
129 | 6 | throw new Exceptions\InvalidRequestException( |
|
130 | 6 | "Could not determine which URL to request: $class instance has invalid ID: $id", null |
|
131 | ); |
||
132 | } |
||
133 | |||
134 | 278 | return static::classUrl().'/'.urlencode(str_utf8($id)); |
|
135 | } |
||
136 | |||
137 | /* ------------------------------------------------------------------------------------------------ |
||
138 | | Request Functions |
||
139 | | ------------------------------------------------------------------------------------------------ |
||
140 | */ |
||
141 | /** |
||
142 | * Make a request. |
||
143 | * |
||
144 | * @param string $method |
||
145 | * @param string $url |
||
146 | * @param array|null $params |
||
147 | * @param array|string|null $options |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | protected function request($method, $url, $params = [], $options = null) |
||
152 | { |
||
153 | 144 | $opts = $this->opts->merge($options); |
|
154 | |||
155 | /** @var \Arcanedev\Stripe\Http\Response $response */ |
||
156 | 144 | list($response, $options) = static::staticRequest($method, $url, $params, $opts); |
|
157 | 144 | $this->setLastResponse($response); |
|
158 | |||
159 | 144 | return [$response->getJson(), $options]; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * Make a request (static). |
||
164 | * |
||
165 | * @param string $method |
||
166 | * @param string $url |
||
167 | * @param array|null $params |
||
168 | * @param array|string|null $options |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | protected static function staticRequest($method, $url, $params, $options) |
||
173 | { |
||
174 | 374 | $opts = RequestOptions::parse($options); |
|
175 | 374 | $requestor = Requestor::make($opts->apiKey, static::baseUrl()); |
|
176 | |||
177 | 362 | list($response, $opts->apiKey) = |
|
178 | 374 | $requestor->request($method, $url, $params, $opts->headers); |
|
179 | |||
180 | 362 | foreach ($opts->headers as $k => $v) { |
|
181 | 26 | if ( ! array_key_exists($k, self::$persistedHeaders)) |
|
182 | 26 | unset($opts->headers[$k]); |
|
183 | } |
||
184 | |||
185 | 362 | return [$response, $opts]; |
|
186 | } |
||
187 | |||
188 | /* ----------------------------------------------------------------- |
||
189 | | CRUD Scope Methods |
||
190 | | ----------------------------------------------------------------- |
||
191 | */ |
||
192 | |||
193 | /** |
||
194 | * Retrieve scope. |
||
195 | * |
||
196 | * @param string $id |
||
197 | * @param array|string|null $options |
||
198 | * |
||
199 | * @return static |
||
200 | */ |
||
201 | protected static function scopedRetrieve($id, $options = null) |
||
211 | |||
212 | /** |
||
213 | * List scope. |
||
214 | * |
||
215 | * @param array|null $params |
||
216 | * @param array|string|null $options |
||
217 | * |
||
218 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
219 | * |
||
220 | * @return \Arcanedev\Stripe\Collection|array |
||
221 | */ |
||
222 | protected static function scopedAll($params = [], $options = null) |
||
240 | |||
241 | /** |
||
242 | * Create scope. |
||
243 | * |
||
244 | * @param array|null $params |
||
245 | * @param array|string|null $options |
||
246 | * |
||
247 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
248 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
249 | * |
||
250 | * @return static |
||
251 | */ |
||
252 | protected static function scopedCreate($params = [], $options = null) |
||
264 | |||
265 | /** |
||
266 | * Update scope. |
||
267 | * |
||
268 | * @param string $id |
||
269 | * @param array|null $params |
||
270 | * @param array|string|null $options |
||
271 | * |
||
272 | * @return static |
||
273 | * |
||
274 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
275 | */ |
||
276 | protected static function scopedUpdate($id, $params = null, $options = null) |
||
288 | |||
289 | /** |
||
290 | * Save scope. |
||
291 | * |
||
292 | * @param array|string|null $options |
||
293 | * |
||
294 | * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException |
||
295 | * |
||
296 | * @return static |
||
297 | */ |
||
298 | protected function scopedSave($options = null) |
||
299 | { |
||
300 | 88 | if (count($params = $this->serializeParameters()) > 0) { |
|
301 | 72 | self::checkArguments(null, $options); |
|
302 | 72 | list($response, $opts) = $this->request('post', $this->instanceUrl(), $params, $options); |
|
303 | 72 | $this->refreshFrom($response, $opts); |
|
304 | } |
||
305 | |||
306 | 88 | return $this; |
|
307 | } |
||
308 | |||
309 | /** |
||
310 | * Delete Scope. |
||
311 | * |
||
312 | * @param array|null $params |
||
313 | * @param array|string|null $options |
||
314 | * |
||
315 | * @throws \Arcanedev\Stripe\Exceptions\InvalidRequestException |
||
316 | * |
||
317 | * @return static |
||
318 | */ |
||
319 | protected function scopedDelete($params = [], $options = null) |
||
328 | |||
329 | /* ----------------------------------------------------------------- |
||
330 | | Nested Resources Methods |
||
331 | | ----------------------------------------------------------------- |
||
332 | */ |
||
333 | |||
334 | /** |
||
335 | * Create a nested resource. |
||
336 | * |
||
337 | * @param string $id |
||
338 | * @param string $nestedPath |
||
339 | * @param array|null $params |
||
340 | * @param array|string|null $options |
||
341 | * |
||
342 | * @return mixed |
||
343 | */ |
||
344 | protected static function createNestedResource($id, $nestedPath, $params = null, $options = null) |
||
350 | |||
351 | /** |
||
352 | * Retrieve a nested resource. |
||
353 | * |
||
354 | * @param string $id |
||
355 | * @param string $nestedPath |
||
356 | * @param string $nestedId |
||
357 | * @param array|null $params |
||
358 | * @param array|string|null $options |
||
359 | * |
||
360 | * @return mixed |
||
361 | */ |
||
362 | protected static function retrieveNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null) |
||
368 | |||
369 | /** |
||
370 | * Update a nested resource. |
||
371 | * |
||
372 | * @param string $id |
||
373 | * @param string $nestedPath |
||
374 | * @param array|null $params |
||
375 | * @param array|string|null $options |
||
376 | * |
||
377 | * @return mixed |
||
378 | */ |
||
379 | protected static function updateNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null) |
||
385 | |||
386 | /** |
||
387 | * Delete a nested resource. |
||
388 | * |
||
389 | * @param string $id |
||
390 | * @param string $nestedPath |
||
391 | * @param string $nestedId |
||
392 | * @param array|null $params |
||
393 | * @param array|string|null $options |
||
394 | * |
||
395 | * @return mixed |
||
396 | */ |
||
397 | protected static function deleteNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null) |
||
403 | |||
404 | /** |
||
405 | * Get all the nested resources. |
||
406 | * |
||
407 | * @param string $id |
||
408 | * @param string $nestedPath |
||
409 | * @param array|null $params |
||
410 | * @param array|string|null $options |
||
411 | * |
||
412 | * @return \Arcanedev\Stripe\Collection |
||
413 | */ |
||
414 | protected static function allNestedResources($id, $nestedPath, $params = null, $options = null) |
||
420 | |||
421 | /** |
||
422 | * Make a request call to a nested nested resource. |
||
423 | * |
||
424 | * @param string $method |
||
425 | * @param string $url |
||
426 | * @param array|null $params |
||
427 | * @param array|string|null $options |
||
428 | * |
||
429 | * @return mixed |
||
430 | */ |
||
431 | protected static function nestedResourceRequest($method, $url, $params = null, $options = null) |
||
440 | |||
441 | /** |
||
442 | * Prepare the nested resource URL. |
||
443 | * |
||
444 | * @param string $id |
||
445 | * @param string $nestedPath |
||
446 | * @param string|null $nestedId |
||
447 | * |
||
448 | * @return string |
||
449 | */ |
||
450 | protected static function nestedResourceUrl($id, $nestedPath, $nestedId = null) |
||
456 | |||
457 | /* ----------------------------------------------------------------- |
||
458 | | Custom Scope Methods |
||
459 | | ----------------------------------------------------------------- |
||
460 | */ |
||
461 | |||
462 | /** |
||
463 | * Custom Post Call. |
||
464 | * |
||
465 | * @param string $url |
||
466 | * @param array|null $params |
||
467 | * @param array|string|null $options |
||
468 | * |
||
469 | * @return static |
||
470 | */ |
||
471 | protected function scopedPostCall($url, $params = [], $options = null) |
||
482 | |||
483 | /* ----------------------------------------------------------------- |
||
484 | | Check Methods |
||
485 | | ----------------------------------------------------------------- |
||
486 | */ |
||
487 | |||
488 | /** |
||
489 | * Check Arguments. |
||
490 | * |
||
491 | * @param array|null $params |
||
492 | * @param array|string|null $options |
||
493 | */ |
||
494 | protected static function checkArguments($params = [], $options = null) |
||
499 | |||
500 | /** |
||
501 | * Check parameters. |
||
502 | * |
||
503 | * @param array|null $params |
||
504 | * |
||
505 | * @throws \Arcanedev\Stripe\Exceptions\InvalidArgumentException |
||
506 | */ |
||
507 | private static function checkParameters($params) |
||
517 | |||
518 | /** |
||
519 | * Check Options. |
||
520 | * |
||
521 | * @param array|string|null $options |
||
522 | * |
||
523 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
524 | */ |
||
525 | private static function checkOptions($options) |
||
541 | |||
542 | /** |
||
543 | * Check the object is a Collection class. |
||
544 | * |
||
545 | * @param mixed $object |
||
546 | * |
||
547 | * @throws \Arcanedev\Stripe\Exceptions\ApiException |
||
548 | */ |
||
549 | private static function checkIsCollectionObject($object) |
||
557 | } |
||
558 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: