Complex classes like ApiPlatformClient 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 ApiPlatformClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | final class ApiPlatformClient implements ApiClientInterface |
||
| 22 | { |
||
| 23 | /** @var AbstractBrowser */ |
||
| 24 | private $client; |
||
| 25 | |||
| 26 | /** @var SharedStorageInterface */ |
||
| 27 | private $sharedStorage; |
||
| 28 | |||
| 29 | /** @var array */ |
||
| 30 | private $request = ['url' => null, 'body' => []]; |
||
| 31 | |||
| 32 | /** @var array */ |
||
| 33 | private $filters; |
||
| 34 | |||
| 35 | public function __construct(AbstractBrowser $client, SharedStorageInterface $sharedStorage) |
||
| 40 | |||
| 41 | public function index(string $resource): void |
||
| 45 | |||
| 46 | public function show(string $resource, string $id): void |
||
| 50 | |||
| 51 | public function showRelated(string $resource): void |
||
| 55 | |||
| 56 | public function showByIri(string $iri): void |
||
| 60 | |||
| 61 | public function subResourceIndex(string $resource, string $subResource, string $id): void |
||
| 65 | |||
| 66 | public function buildCreateRequest(string $resource): void |
||
| 70 | |||
| 71 | public function buildUpdateRequest(string $resource, string $id): void |
||
| 78 | |||
| 79 | public function buildFilter(array $filters): void |
||
| 83 | |||
| 84 | /** @param string|int $value */ |
||
| 85 | public function addRequestData(string $key, $value): void |
||
| 89 | |||
| 90 | public function addCompoundRequestData(array $data): void |
||
| 94 | |||
| 95 | public function updateRequestData(array $data): void |
||
| 99 | |||
| 100 | public function create(): void |
||
| 106 | |||
| 107 | public function update(): void |
||
| 113 | |||
| 114 | public function delete(string $resource, string $id): void |
||
| 118 | |||
| 119 | public function filter(string $resource): void |
||
| 126 | |||
| 127 | public function applyTransition(string $resource, string $id, string $transition): void |
||
| 136 | |||
| 137 | public function countCollectionItems(): int |
||
| 141 | |||
| 142 | public function getCollectionItems(): array |
||
| 146 | |||
| 147 | public function getCollectionItemsWithValue(string $key, string $value): array |
||
| 155 | |||
| 156 | public function getError(): string |
||
| 160 | |||
| 161 | public function isCreationSuccessful(): bool |
||
| 165 | |||
| 166 | public function isUpdateSuccessful(): bool |
||
| 170 | |||
| 171 | public function isDeletionSuccessful(): bool |
||
| 175 | |||
| 176 | /** @param string|int $value */ |
||
| 177 | public function responseHasValue(string $key, $value): bool |
||
| 181 | |||
| 182 | /** @param string|int $value */ |
||
| 183 | public function relatedResourceHasValue(string $resource, string $key, $value): bool |
||
| 189 | |||
| 190 | /** @param string|float $value */ |
||
| 191 | public function hasItemWithValue(string $key, $value): bool |
||
| 201 | |||
| 202 | public function hasItemOnPositionWithValue(int $position, string $key, string $value): bool |
||
| 206 | |||
| 207 | public function hasItemWithTranslation(string $locale, string $key, string $translation): bool |
||
| 221 | |||
| 222 | private function request(string $method, string $url, array $headers, string $content = null): void |
||
| 231 | |||
| 232 | private function getResponseContentValue(string $key) |
||
| 240 | |||
| 241 | private function mergeArraysUniquely(array $firstArray, array $secondArray): array |
||
| 251 | } |
||
| 252 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.