Complex classes like Authorization 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 Authorization, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | final class Authorization |
||
23 | { |
||
24 | /** |
||
25 | * @var bool|null |
||
26 | */ |
||
27 | private $authorized = null; |
||
28 | |||
29 | /** |
||
30 | * @var Client |
||
31 | */ |
||
32 | private $client; |
||
33 | |||
34 | /** |
||
35 | * @var UserAccount|null |
||
36 | */ |
||
37 | private $userAccount = null; |
||
38 | |||
39 | /** |
||
40 | * @var null|bool |
||
41 | */ |
||
42 | private $userAccountFullyAuthenticated = null; |
||
43 | |||
44 | /** |
||
45 | * @var string[] |
||
46 | */ |
||
47 | private $scopes = []; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | private $data = []; |
||
53 | |||
54 | /** |
||
55 | * @var TokenType|null |
||
56 | */ |
||
57 | private $tokenType = null; |
||
58 | |||
59 | /** |
||
60 | * @var ResponseType[] |
||
61 | */ |
||
62 | private $responseTypes = []; |
||
63 | |||
64 | /** |
||
65 | * @var ResponseMode|null |
||
66 | */ |
||
67 | private $responseMode = null; |
||
68 | |||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | private $queryParameters = []; |
||
73 | |||
74 | /** |
||
75 | * @var string|null |
||
76 | */ |
||
77 | private $redirectUri = null; |
||
78 | |||
79 | /** |
||
80 | * @var array |
||
81 | */ |
||
82 | private $consentScreenOptions = []; |
||
83 | |||
84 | /** |
||
85 | * @var array |
||
86 | */ |
||
87 | private $responseParameters = []; |
||
88 | |||
89 | /** |
||
90 | * @var array |
||
91 | */ |
||
92 | private $responseHeaders = []; |
||
93 | |||
94 | /** |
||
95 | * @var null|ResourceServer |
||
96 | */ |
||
97 | private $resourceServer = null; |
||
98 | |||
99 | /** |
||
100 | * Authorization constructor. |
||
101 | * |
||
102 | * @param Client $client |
||
103 | * @param array $queryParameters |
||
104 | */ |
||
105 | private function __construct(Client $client, array $queryParameters) |
||
110 | |||
111 | /** |
||
112 | * @param Client $client |
||
113 | * @param array $queryParameters |
||
114 | * |
||
115 | * @return Authorization |
||
|
|||
116 | */ |
||
117 | public static function create(Client $client, array $queryParameters): self |
||
121 | |||
122 | /** |
||
123 | * @return array |
||
124 | */ |
||
125 | public function getQueryParams(): array |
||
129 | |||
130 | /** |
||
131 | * @param string $param |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | public function hasQueryParam(string $param): bool |
||
139 | |||
140 | /** |
||
141 | * @param string $param |
||
142 | * |
||
143 | * @return mixed |
||
144 | */ |
||
145 | public function getQueryParam(string $param) |
||
153 | |||
154 | /** |
||
155 | * @return Client |
||
156 | */ |
||
157 | public function getClient(): Client |
||
161 | |||
162 | /** |
||
163 | * @param TokenType $tokenType |
||
164 | * |
||
165 | * @return Authorization |
||
166 | */ |
||
167 | public function withTokenType(TokenType $tokenType): self |
||
174 | |||
175 | /** |
||
176 | * @return null|TokenType |
||
177 | */ |
||
178 | public function getTokenType(): ? TokenType |
||
182 | |||
183 | /** |
||
184 | * @param ResponseType[] $responseTypes |
||
185 | * |
||
186 | * @return Authorization |
||
187 | */ |
||
188 | public function withResponseTypes(array $responseTypes): self |
||
195 | |||
196 | /** |
||
197 | * @return ResponseType[] |
||
198 | */ |
||
199 | public function getResponseTypes(): array |
||
203 | |||
204 | /** |
||
205 | * @param ResponseMode $responseMode |
||
206 | * |
||
207 | * @return Authorization |
||
208 | */ |
||
209 | public function withResponseMode(ResponseMode $responseMode): self |
||
216 | |||
217 | /** |
||
218 | * @return null|ResponseMode |
||
219 | */ |
||
220 | public function getResponseMode(): ? ResponseMode |
||
224 | |||
225 | /** |
||
226 | * @param string $redirectUri |
||
227 | * |
||
228 | * @return Authorization |
||
229 | */ |
||
230 | public function withRedirectUri(string $redirectUri): self |
||
237 | |||
238 | /** |
||
239 | * @return null|string |
||
240 | */ |
||
241 | public function getRedirectUri(): ? string |
||
245 | |||
246 | /** |
||
247 | * @param UserAccount $userAccount |
||
248 | * @param bool $isFullyAuthenticated |
||
249 | * |
||
250 | * @return Authorization |
||
251 | */ |
||
252 | public function withUserAccount(UserAccount $userAccount, bool $isFullyAuthenticated): self |
||
260 | |||
261 | /** |
||
262 | * @return null|UserAccount |
||
263 | */ |
||
264 | public function getUserAccount(): ? UserAccount |
||
268 | |||
269 | /** |
||
270 | * @param string $responseParameter |
||
271 | * @param mixed $value |
||
272 | * |
||
273 | * @return Authorization |
||
274 | */ |
||
275 | public function withResponseParameter(string $responseParameter, $value): self |
||
282 | |||
283 | /** |
||
284 | * @return array |
||
285 | */ |
||
286 | public function getResponseParameters(): array |
||
290 | |||
291 | /** |
||
292 | * @param string $param |
||
293 | * |
||
294 | * @return mixed |
||
295 | */ |
||
296 | public function getResponseParameter(string $param) |
||
304 | |||
305 | /** |
||
306 | * @param string $param |
||
307 | * |
||
308 | * @return bool |
||
309 | */ |
||
310 | public function hasResponseParameter(string $param): bool |
||
314 | |||
315 | /** |
||
316 | * @param string $responseHeader |
||
317 | * @param mixed $value |
||
318 | * |
||
319 | * @return Authorization |
||
320 | */ |
||
321 | public function withResponseHeader(string $responseHeader, $value): self |
||
328 | |||
329 | /** |
||
330 | * @return array |
||
331 | */ |
||
332 | public function getResponseHeaders(): array |
||
336 | |||
337 | /** |
||
338 | * @return bool|null |
||
339 | */ |
||
340 | public function isUserAccountFullyAuthenticated(): ? bool |
||
344 | |||
345 | /** |
||
346 | * @return string[] |
||
347 | */ |
||
348 | public function getPrompt(): array |
||
356 | |||
357 | /** |
||
358 | * @return bool |
||
359 | */ |
||
360 | public function hasUiLocales(): bool |
||
364 | |||
365 | /** |
||
366 | * @return string[] |
||
367 | */ |
||
368 | public function getUiLocales(): array |
||
372 | |||
373 | /** |
||
374 | * @param string $prompt |
||
375 | * |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function hasPrompt(string $prompt): bool |
||
382 | |||
383 | /** |
||
384 | * @param array $scope |
||
385 | * |
||
386 | * @return Authorization |
||
387 | */ |
||
388 | public function withScopes(array $scope): self |
||
395 | |||
396 | /** |
||
397 | * @return array |
||
398 | */ |
||
399 | public function getScopes(): array |
||
403 | |||
404 | /** |
||
405 | * @param string $scope |
||
406 | * |
||
407 | * @return bool |
||
408 | */ |
||
409 | public function hasScope(string $scope): bool |
||
413 | |||
414 | /** |
||
415 | * @param string $scope |
||
416 | * |
||
417 | * @return Authorization |
||
418 | */ |
||
419 | public function withoutScope(string $scope): self |
||
429 | |||
430 | /** |
||
431 | * @param string $scope |
||
432 | * |
||
433 | * @return Authorization |
||
434 | */ |
||
435 | public function addScope(string $scope): self |
||
445 | |||
446 | /** |
||
447 | * @return bool|null |
||
448 | */ |
||
449 | public function isAuthorized(): ? bool |
||
453 | |||
454 | /** |
||
455 | * @return Authorization |
||
456 | */ |
||
457 | public function allow(): self |
||
464 | |||
465 | /** |
||
466 | * @return Authorization |
||
467 | */ |
||
468 | public function deny(): self |
||
475 | |||
476 | /** |
||
477 | * @param string $key |
||
478 | * |
||
479 | * @return bool |
||
480 | */ |
||
481 | public function hasData(string $key): bool |
||
485 | |||
486 | /** |
||
487 | * @param string $key |
||
488 | * |
||
489 | * @return mixed |
||
490 | */ |
||
491 | public function getData(string $key) |
||
499 | |||
500 | /** |
||
501 | * @param string $key |
||
502 | * @param mixed $data |
||
503 | * |
||
504 | * @return Authorization |
||
505 | */ |
||
506 | public function withData(string $key, $data): self |
||
513 | |||
514 | /** |
||
515 | * @return null|ResourceServer |
||
516 | */ |
||
517 | public function getResourceServer(): ? ResourceServer |
||
521 | |||
522 | /** |
||
523 | * @param ResourceServer $resourceServer |
||
524 | * |
||
525 | * @return Authorization |
||
526 | */ |
||
527 | public function withResourceServer(ResourceServer $resourceServer): self |
||
534 | |||
535 | /** |
||
536 | * @return bool |
||
537 | */ |
||
538 | public function hasOfflineAccess(): bool |
||
553 | |||
554 | /** |
||
555 | * @param string $option |
||
556 | * @param mixed $value |
||
557 | * |
||
558 | * @return Authorization |
||
559 | */ |
||
560 | public function withConsentScreenOption(string $option, $value): self |
||
567 | |||
568 | /** |
||
569 | * @param string $option |
||
570 | * |
||
571 | * @return Authorization |
||
572 | */ |
||
573 | public function withoutConsentScreenOption(string $option): self |
||
584 | } |
||
585 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.