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 |
||
35 | final class Client implements ResourceOwner, ContainsRecordedMessages, DomainObject |
||
36 | { |
||
37 | use PrivateMessageRecorderCapabilities; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | private $deleted = false; |
||
43 | |||
44 | /** |
||
45 | * @var UserAccountId|null |
||
46 | */ |
||
47 | private $ownerId = null; |
||
48 | |||
49 | /** |
||
50 | * @var ClientId|null |
||
51 | */ |
||
52 | private $clientId = null; |
||
53 | |||
54 | /** |
||
55 | * @var DataBag |
||
56 | */ |
||
57 | protected $parameters; |
||
58 | |||
59 | /** |
||
60 | * Client constructor. |
||
61 | */ |
||
62 | private function __construct() |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public static function getSchema(): string |
||
74 | |||
75 | /** |
||
76 | * @return Client |
||
77 | */ |
||
78 | public static function createEmpty(): self |
||
82 | |||
83 | /** |
||
84 | * @param ClientId $clientId |
||
85 | * @param DataBag $parameters |
||
86 | * @param UserAccountId|null $ownerId |
||
87 | * |
||
88 | * @return Client |
||
89 | */ |
||
90 | public function create(ClientId $clientId, DataBag $parameters, ? UserAccountId $ownerId): self |
||
102 | |||
103 | /** |
||
104 | * @return UserAccountId|null |
||
105 | */ |
||
106 | public function getOwnerId(): ? UserAccountId |
||
110 | |||
111 | /** |
||
112 | * @param UserAccountId $ownerId |
||
113 | * |
||
114 | * @return Client |
||
115 | */ |
||
116 | public function withOwnerId(UserAccountId $ownerId): self |
||
129 | |||
130 | /** |
||
131 | * @param DataBag $parameters |
||
132 | * |
||
133 | * @return Client |
||
134 | */ |
||
135 | public function withParameters(DataBag $parameters): self |
||
144 | |||
145 | /** |
||
146 | * @return Client |
||
147 | */ |
||
148 | public function markAsDeleted(): self |
||
157 | |||
158 | /** |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function isDeleted(): bool |
||
165 | |||
166 | /** |
||
167 | * @param string $grant_type |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function isGrantTypeAllowed(string $grant_type): bool |
||
180 | |||
181 | /** |
||
182 | * @param string $response_type |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function isResponseTypeAllowed(string $response_type): bool |
||
195 | |||
196 | /** |
||
197 | * @param string $token_type |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | public function isTokenTypeAllowed(string $token_type): bool |
||
213 | |||
214 | /** |
||
215 | * @return bool |
||
216 | */ |
||
217 | public function isPublic(): bool |
||
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | public function getTokenEndpointAuthenticationMethod(): string |
||
226 | { |
||
227 | if ($this->has('token_endpoint_auth_method')) { |
||
228 | return $this->get('token_endpoint_auth_method'); |
||
229 | } |
||
230 | |||
231 | return 'client_secret_basic'; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return int |
||
236 | */ |
||
237 | public function getClientCredentialsExpiresAt(): int |
||
245 | |||
246 | /** |
||
247 | * @return bool |
||
248 | */ |
||
249 | public function areClientCredentialsExpired(): bool |
||
257 | |||
258 | /** |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function hasPublicKeySet(): bool |
||
265 | |||
266 | /** |
||
267 | * @return JWKSet |
||
268 | */ |
||
269 | public function getPublicKeySet(): JWKSet |
||
301 | |||
302 | /** |
||
303 | * {@inheritdoc} |
||
304 | */ |
||
305 | public function getPublicId(): ResourceOwnerId |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function has(string $key): bool |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | public function get(string $key) |
||
333 | |||
334 | /** |
||
335 | * @return array |
||
336 | */ |
||
337 | public function all(): array |
||
344 | |||
345 | /** |
||
346 | * {@inheritdoc} |
||
347 | */ |
||
348 | public function jsonSerialize() |
||
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | */ |
||
365 | public static function createFromJson(\stdClass $json): DomainObject |
||
380 | |||
381 | /** |
||
382 | * @param Event $event |
||
383 | * |
||
384 | * @return Client |
||
385 | */ |
||
386 | public function apply(Event $event): self |
||
399 | |||
400 | /** |
||
401 | * @return array |
||
402 | */ |
||
403 | private function getEventMap(): array |
||
412 | |||
413 | /** |
||
414 | * @param ClientEvent\ClientCreatedEvent $event |
||
415 | * |
||
416 | * @return Client |
||
417 | */ |
||
418 | protected function applyClientCreatedEvent(ClientEvent\ClientCreatedEvent $event): self |
||
427 | |||
428 | /** |
||
429 | * @param ClientEvent\ClientOwnerChangedEvent $event |
||
430 | * |
||
431 | * @return Client |
||
432 | */ |
||
433 | protected function applyClientOwnerChangedEvent(ClientEvent\ClientOwnerChangedEvent $event): self |
||
440 | |||
441 | /** |
||
442 | * @param ClientEvent\ClientDeletedEvent $event |
||
443 | * |
||
444 | * @return Client |
||
445 | */ |
||
446 | protected function applyClientDeletedEvent(ClientEvent\ClientDeletedEvent $event): self |
||
453 | |||
454 | /** |
||
455 | * @param ClientEvent\ClientParametersUpdatedEvent $event |
||
456 | * |
||
457 | * @return Client |
||
458 | */ |
||
459 | protected function applyClientParametersUpdatedEvent(ClientEvent\ClientParametersUpdatedEvent $event): self |
||
466 | } |
||
467 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.