Complex classes like AbstractResource 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 AbstractResource, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | abstract class AbstractResource |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var Request |
||
| 13 | */ |
||
| 14 | protected $request; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $actions = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $childResources = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * AbstractResource constructor. |
||
| 28 | * @param Request $request |
||
| 29 | */ |
||
| 30 | 5 | public function __construct(Request $request) |
|
| 34 | |||
| 35 | /** |
||
| 36 | * @param string $action |
||
| 37 | * @return array |
||
| 38 | * @throws ClientException |
||
| 39 | */ |
||
| 40 | 133 | public function getAction(string $action): array |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @return Request |
||
| 51 | */ |
||
| 52 | 1 | public function getRequest() |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @param string $action |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | 133 | public function hasAction(string $action): bool |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | 5 | public function getChildResources(): array |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $action |
||
| 80 | * @param float|null $parentId |
||
| 81 | * @param float|null $childId |
||
| 82 | * @param float|null $childChildId |
||
| 83 | * @param array|null $parameters |
||
| 84 | * @return array|bool |
||
| 85 | */ |
||
| 86 | 133 | protected function request( |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $action |
||
| 104 | * @param float|null $parentId |
||
| 105 | * @param float|null $childId |
||
| 106 | * @param float|null $childChildId |
||
| 107 | * @return string |
||
| 108 | * @throws ClientException |
||
| 109 | */ |
||
| 110 | 131 | protected function getEndpoint(string $action, float $parentId = null, float $childId = null, float $childChildId = null): string |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $action |
||
| 135 | * @return string |
||
| 136 | * @throws ClientException |
||
| 137 | */ |
||
| 138 | 132 | protected function getMethod(string $action): string |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $action |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 52 | protected function getResourceKey(string $action): string |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $action |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | 133 | protected function getResponseKey(string $action): string |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $action |
||
| 181 | * @param array $parameters |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | 130 | private function getRequestOptions(string $action, array $parameters): array |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $method |
||
| 214 | * @param array|null $arguments |
||
| 215 | * @return array|bool |
||
| 216 | */ |
||
| 217 | 133 | public function __call(string $method, array $arguments = []) |
|
| 256 | } |
||
| 257 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.