Complex classes like Droplet 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 Droplet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Droplet extends AbstractApi |
||
26 | { |
||
27 | /** |
||
28 | * @param int $per_page |
||
29 | * @param int $page |
||
30 | * @param string|null $tag |
||
31 | * |
||
32 | * @return DropletEntity[] |
||
33 | */ |
||
34 | public function getAll($per_page = 200, $page = 1, $tag = null) |
||
50 | |||
51 | /** |
||
52 | * @param int $id |
||
53 | * |
||
54 | * @return DropletEntity[] |
||
55 | */ |
||
56 | public function getNeighborsById($id) |
||
66 | |||
67 | /** |
||
68 | * @return DropletEntity[] |
||
69 | */ |
||
70 | public function getAllNeighbors() |
||
80 | |||
81 | /** |
||
82 | * @return UpgradeEntity[] |
||
83 | */ |
||
84 | public function getUpgrades() |
||
94 | |||
95 | /** |
||
96 | * @param int $id |
||
97 | * |
||
98 | * @throws HttpException |
||
99 | * |
||
100 | * @return DropletEntity |
||
101 | */ |
||
102 | public function getById($id) |
||
110 | |||
111 | /** |
||
112 | * @param array|string $names |
||
113 | * @param string $region |
||
114 | * @param string $size |
||
115 | * @param string|int $image |
||
116 | * @param bool $backups |
||
117 | * @param bool $ipv6 |
||
118 | * @param bool $privateNetworking |
||
119 | * @param int[] $sshKeys |
||
120 | * @param string $userData |
||
121 | * @param bool $monitoring |
||
122 | * @param array $volumes |
||
123 | * @param array $tags |
||
124 | * @param bool $wait |
||
125 | * @param int $waitTimeout |
||
126 | * |
||
127 | * @throws HttpException |
||
128 | * |
||
129 | * @return DropletEntity|null |
||
130 | */ |
||
131 | public function create($names, $region, $size, $image, $backups = false, $ipv6 = false, $privateNetworking = false, array $sshKeys = [], $userData = '', $monitoring = true, array $volumes = [], array $tags = [], $wait = false, $waitTimeout = 300) |
||
181 | |||
182 | /** |
||
183 | * @param int $id |
||
184 | * |
||
185 | * @throws HttpException |
||
186 | */ |
||
187 | public function delete($id) |
||
191 | |||
192 | /** |
||
193 | * @param int $id |
||
194 | * |
||
195 | * @throws HttpException |
||
196 | * |
||
197 | * @return KernelEntity[] |
||
198 | */ |
||
199 | public function getAvailableKernels($id) |
||
211 | |||
212 | /** |
||
213 | * @param int $id |
||
214 | * |
||
215 | * @return ImageEntity[] |
||
216 | */ |
||
217 | public function getSnapshots($id) |
||
231 | |||
232 | /** |
||
233 | * @param int $id |
||
234 | * |
||
235 | * @return ImageEntity[] |
||
236 | */ |
||
237 | public function getBackups($id) |
||
249 | |||
250 | /** |
||
251 | * @param int $id |
||
252 | * |
||
253 | * @return ActionEntity[] |
||
254 | */ |
||
255 | public function getActions($id) |
||
267 | |||
268 | /** |
||
269 | * @param int $id |
||
270 | * @param int $actionId |
||
271 | * |
||
272 | * @return ActionEntity |
||
273 | */ |
||
274 | public function getActionById($id, $actionId) |
||
282 | |||
283 | /** |
||
284 | * @param int $id |
||
285 | * |
||
286 | * @throws HttpException |
||
287 | * |
||
288 | * @return ActionEntity |
||
289 | */ |
||
290 | public function reboot($id) |
||
294 | |||
295 | /** |
||
296 | * @param int $id |
||
297 | * |
||
298 | * @throws HttpException |
||
299 | * |
||
300 | * @return ActionEntity |
||
301 | */ |
||
302 | public function powerCycle($id) |
||
306 | |||
307 | /** |
||
308 | * @param int $id |
||
309 | * |
||
310 | * @throws HttpException |
||
311 | * |
||
312 | * @return ActionEntity |
||
313 | */ |
||
314 | public function shutdown($id) |
||
318 | |||
319 | /** |
||
320 | * @param int $id |
||
321 | * |
||
322 | * @throws HttpException |
||
323 | * |
||
324 | * @return ActionEntity |
||
325 | */ |
||
326 | public function powerOff($id) |
||
330 | |||
331 | /** |
||
332 | * @param int $id |
||
333 | * |
||
334 | * @throws HttpException |
||
335 | * |
||
336 | * @return ActionEntity |
||
337 | */ |
||
338 | public function powerOn($id) |
||
342 | |||
343 | /** |
||
344 | * @param int $id |
||
345 | * |
||
346 | * @throws HttpException |
||
347 | * |
||
348 | * @return ActionEntity |
||
349 | */ |
||
350 | public function passwordReset($id) |
||
354 | |||
355 | /** |
||
356 | * @param int $id |
||
357 | * @param string $size |
||
358 | * @param bool $disk |
||
359 | * |
||
360 | * @throws HttpException |
||
361 | * |
||
362 | * @return ActionEntity |
||
363 | */ |
||
364 | public function resize($id, $size, $disk = true) |
||
368 | |||
369 | /** |
||
370 | * @param int $id |
||
371 | * @param int $image |
||
372 | * |
||
373 | * @throws HttpException |
||
374 | * |
||
375 | * @return ActionEntity |
||
376 | */ |
||
377 | public function restore($id, $image) |
||
381 | |||
382 | /** |
||
383 | * @param int $id |
||
384 | * @param int|string $image |
||
385 | * |
||
386 | * @throws HttpException |
||
387 | * |
||
388 | * @return ActionEntity |
||
389 | */ |
||
390 | public function rebuild($id, $image) |
||
394 | |||
395 | /** |
||
396 | * @param int $id |
||
397 | * @param string $name |
||
398 | * |
||
399 | * @throws HttpException |
||
400 | * |
||
401 | * @return ActionEntity |
||
402 | */ |
||
403 | public function rename($id, $name) |
||
407 | |||
408 | /** |
||
409 | * @param int $id |
||
410 | * @param int $kernel |
||
411 | * |
||
412 | * @throws HttpException |
||
413 | * |
||
414 | * @return ActionEntity |
||
415 | */ |
||
416 | public function changeKernel($id, $kernel) |
||
420 | |||
421 | /** |
||
422 | * @param int $id |
||
423 | * |
||
424 | * @throws HttpException |
||
425 | * |
||
426 | * @return ActionEntity |
||
427 | */ |
||
428 | public function enableIpv6($id) |
||
432 | |||
433 | /** |
||
434 | * @param int $id |
||
435 | * |
||
436 | * @throws HttpException |
||
437 | * |
||
438 | * @return ActionEntity |
||
439 | */ |
||
440 | public function enableBackups($id) |
||
444 | |||
445 | /** |
||
446 | * @param int $id |
||
447 | * |
||
448 | * @throws HttpException |
||
449 | * |
||
450 | * @return ActionEntity |
||
451 | */ |
||
452 | public function disableBackups($id) |
||
456 | |||
457 | /** |
||
458 | * @param int $id |
||
459 | * |
||
460 | * @throws HttpException |
||
461 | * |
||
462 | * @return ActionEntity |
||
463 | */ |
||
464 | public function enablePrivateNetworking($id) |
||
468 | |||
469 | /** |
||
470 | * @param int $id |
||
471 | * @param string $name |
||
472 | * |
||
473 | * @throws HttpException |
||
474 | * |
||
475 | * @return ActionEntity |
||
476 | */ |
||
477 | public function snapshot($id, $name) |
||
481 | |||
482 | /** |
||
483 | * @param int $id |
||
484 | * @param array $options |
||
485 | * |
||
486 | * @throws HttpException |
||
487 | * |
||
488 | * @return ActionEntity |
||
489 | */ |
||
490 | private function executeAction($id, array $options) |
||
498 | |||
499 | /** |
||
500 | * @param DropletEntity $droplet |
||
501 | * @param int $waitTimeout |
||
502 | * |
||
503 | * @throws HttpException |
||
504 | * |
||
505 | * @return DropletEntity|null |
||
506 | */ |
||
507 | public function waitForActive($droplet, $waitTimeout) |
||
519 | } |
||
520 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.