Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Client implements ResourceOwner, ContainsRecordedMessages, DomainObject |
||
|
|
|||
| 34 | { |
||
| 35 | use PrivateMessageRecorderCapabilities; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | private $deleted = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var UserAccountId|null |
||
| 44 | */ |
||
| 45 | private $ownerId = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var ClientId|null |
||
| 49 | */ |
||
| 50 | private $clientId = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var DataBag |
||
| 54 | */ |
||
| 55 | protected $parameters; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * ClientCredentials constructor. |
||
| 59 | */ |
||
| 60 | private function __construct() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public static function getSchema(): string |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return Client |
||
| 75 | */ |
||
| 76 | public static function createEmpty(): self |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param ClientId $clientId |
||
| 83 | * @param DataBag $parameters |
||
| 84 | * @param UserAccountId|null $ownerId |
||
| 85 | * |
||
| 86 | * @return Client |
||
| 87 | */ |
||
| 88 | public function create(ClientId $clientId, DataBag $parameters, ? UserAccountId $ownerId): self |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return ClientId |
||
| 103 | */ |
||
| 104 | public function getClientId(): ClientId |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return UserAccountId|null |
||
| 116 | */ |
||
| 117 | public function getOwnerId(): ? UserAccountId |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param UserAccountId $ownerId |
||
| 124 | * |
||
| 125 | * @return Client |
||
| 126 | */ |
||
| 127 | public function withOwnerId(UserAccountId $ownerId): self |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param DataBag $parameters |
||
| 143 | * |
||
| 144 | * @return Client |
||
| 145 | */ |
||
| 146 | public function withParameters(DataBag $parameters): self |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return Client |
||
| 158 | */ |
||
| 159 | public function markAsDeleted(): self |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public function isDeleted(): bool |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $grant_type |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | public function isGrantTypeAllowed(string $grant_type): bool |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $response_type |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | public function isResponseTypeAllowed(string $response_type): bool |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function isPublic(): bool |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public function getTokenEndpointAuthenticationMethod(): string |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return int |
||
| 229 | */ |
||
| 230 | public function getClientCredentialsExpiresAt(): int |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | public function areClientCredentialsExpired(): bool |
||
| 250 | |||
| 251 | /** |
||
| 252 | * {@inheritdoc} |
||
| 253 | */ |
||
| 254 | public function getPublicId(): ResourceOwnerId |
||
| 262 | |||
| 263 | /** |
||
| 264 | * {@inheritdoc} |
||
| 265 | */ |
||
| 266 | public function has(string $key): bool |
||
| 270 | |||
| 271 | /** |
||
| 272 | * {@inheritdoc} |
||
| 273 | */ |
||
| 274 | public function get(string $key) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function all(): array |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function jsonSerialize() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * {@inheritdoc} |
||
| 313 | */ |
||
| 314 | public static function createFromJson(\stdClass $json): DomainObject |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param Event $event |
||
| 332 | * |
||
| 333 | * @return Client |
||
| 334 | */ |
||
| 335 | public function apply(Event $event): self |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | private function getEventMap(): array |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param ClientEvent\ClientCreatedEvent $event |
||
| 364 | * |
||
| 365 | * @return Client |
||
| 366 | */ |
||
| 367 | protected function applyClientCreatedEvent(ClientEvent\ClientCreatedEvent $event): self |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param ClientEvent\ClientOwnerChangedEvent $event |
||
| 379 | * |
||
| 380 | * @return Client |
||
| 381 | */ |
||
| 382 | protected function applyClientOwnerChangedEvent(ClientEvent\ClientOwnerChangedEvent $event): self |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param ClientEvent\ClientDeletedEvent $event |
||
| 392 | * |
||
| 393 | * @return Client |
||
| 394 | */ |
||
| 395 | protected function applyClientDeletedEvent(ClientEvent\ClientDeletedEvent $event): self |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param ClientEvent\ClientParametersUpdatedEvent $event |
||
| 405 | * |
||
| 406 | * @return Client |
||
| 407 | */ |
||
| 408 | protected function applyClientParametersUpdatedEvent(ClientEvent\ClientParametersUpdatedEvent $event): self |
||
| 415 | } |
||
| 416 |