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 | 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 array |
||
46 | */ |
||
47 | private $data = []; |
||
48 | |||
49 | /** |
||
50 | * @var TokenType|null |
||
51 | */ |
||
52 | private $tokenType = null; |
||
53 | |||
54 | /** |
||
55 | * @var ResponseType] |
||
56 | */ |
||
57 | private $responseType = null; |
||
58 | |||
59 | /** |
||
60 | * @var ResponseMode|null |
||
61 | */ |
||
62 | private $responseMode = null; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | private $queryParameters = []; |
||
68 | |||
69 | /** |
||
70 | * @var string|null |
||
71 | */ |
||
72 | private $redirectUri = null; |
||
73 | |||
74 | /** |
||
75 | * @var array |
||
76 | */ |
||
77 | private $consentScreenOptions = []; |
||
78 | |||
79 | /** |
||
80 | * @var array |
||
81 | */ |
||
82 | private $responseParameters = []; |
||
83 | |||
84 | /** |
||
85 | * @var array |
||
86 | */ |
||
87 | private $responseHeaders = []; |
||
88 | |||
89 | /** |
||
90 | * @var null|ResourceServer |
||
91 | */ |
||
92 | private $resourceServer = null; |
||
93 | |||
94 | /** |
||
95 | * Authorization constructor. |
||
96 | * |
||
97 | * @param Client $client |
||
98 | * @param array $queryParameters |
||
99 | */ |
||
100 | private function __construct(Client $client, array $queryParameters) |
||
105 | |||
106 | /** |
||
107 | * @param Client $client |
||
108 | * @param array $queryParameters |
||
109 | * |
||
110 | * @return Authorization |
||
111 | */ |
||
112 | public static function create(Client $client, array $queryParameters): self |
||
116 | |||
117 | /** |
||
118 | * @return array |
||
119 | */ |
||
120 | public function getQueryParams(): array |
||
124 | |||
125 | /** |
||
126 | * @param string $param |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function hasQueryParam(string $param): bool |
||
134 | |||
135 | /** |
||
136 | * @param string $param |
||
137 | * |
||
138 | * @return mixed |
||
139 | */ |
||
140 | public function getQueryParam(string $param) |
||
148 | |||
149 | /** |
||
150 | * @return Client |
||
151 | */ |
||
152 | public function getClient(): Client |
||
156 | |||
157 | /** |
||
158 | * @param TokenType $tokenType |
||
159 | * |
||
160 | * @return Authorization |
||
161 | */ |
||
162 | public function withTokenType(TokenType $tokenType): self |
||
169 | |||
170 | /** |
||
171 | * @return null|TokenType |
||
172 | */ |
||
173 | public function getTokenType(): ? TokenType |
||
177 | |||
178 | /** |
||
179 | * @param ResponseType $responseType |
||
180 | * |
||
181 | * @return Authorization |
||
182 | */ |
||
183 | public function withResponseType(ResponseType $responseType): self |
||
190 | |||
191 | /** |
||
192 | * @return ResponseType |
||
193 | */ |
||
194 | public function getResponseType(): ResponseType |
||
198 | |||
199 | /** |
||
200 | * @param ResponseMode $responseMode |
||
201 | * |
||
202 | * @return Authorization |
||
203 | */ |
||
204 | public function withResponseMode(ResponseMode $responseMode): self |
||
211 | |||
212 | /** |
||
213 | * @return null|ResponseMode |
||
214 | */ |
||
215 | public function getResponseMode(): ? ResponseMode |
||
219 | |||
220 | /** |
||
221 | * @param string $redirectUri |
||
222 | * |
||
223 | * @return Authorization |
||
224 | */ |
||
225 | public function withRedirectUri(string $redirectUri): self |
||
232 | |||
233 | /** |
||
234 | * @return null|string |
||
235 | */ |
||
236 | public function getRedirectUri(): ? string |
||
240 | |||
241 | /** |
||
242 | * @param UserAccount $userAccount |
||
243 | * @param bool $isFullyAuthenticated |
||
244 | * |
||
245 | * @return Authorization |
||
246 | */ |
||
247 | public function withUserAccount(UserAccount $userAccount, bool $isFullyAuthenticated): self |
||
255 | |||
256 | /** |
||
257 | * @return null|UserAccount |
||
258 | */ |
||
259 | public function getUserAccount(): ? UserAccount |
||
263 | |||
264 | /** |
||
265 | * @param string $responseParameter |
||
266 | * @param mixed $value |
||
267 | * |
||
268 | * @return Authorization |
||
269 | */ |
||
270 | public function withResponseParameter(string $responseParameter, $value): self |
||
277 | |||
278 | /** |
||
279 | * @return array |
||
280 | */ |
||
281 | public function getResponseParameters(): array |
||
285 | |||
286 | /** |
||
287 | * @param string $param |
||
288 | * |
||
289 | * @return mixed |
||
290 | */ |
||
291 | public function getResponseParameter(string $param) |
||
299 | |||
300 | /** |
||
301 | * @param string $param |
||
302 | * |
||
303 | * @return bool |
||
304 | */ |
||
305 | public function hasResponseParameter(string $param): bool |
||
309 | |||
310 | /** |
||
311 | * @param string $responseHeader |
||
312 | * @param mixed $value |
||
313 | * |
||
314 | * @return Authorization |
||
315 | */ |
||
316 | public function withResponseHeader(string $responseHeader, $value): self |
||
323 | |||
324 | /** |
||
325 | * @return array |
||
326 | */ |
||
327 | public function getResponseHeaders(): array |
||
331 | |||
332 | /** |
||
333 | * @return bool|null |
||
334 | */ |
||
335 | public function isUserAccountFullyAuthenticated(): ? bool |
||
339 | |||
340 | /** |
||
341 | * @return string[] |
||
342 | */ |
||
343 | public function getPrompt(): array |
||
351 | |||
352 | /** |
||
353 | * @return bool |
||
354 | */ |
||
355 | public function hasUiLocales(): bool |
||
359 | |||
360 | /** |
||
361 | * @return string[] |
||
362 | */ |
||
363 | public function getUiLocales(): array |
||
367 | |||
368 | /** |
||
369 | * @param string $prompt |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | public function hasPrompt(string $prompt): bool |
||
377 | |||
378 | /** |
||
379 | * @return bool|null |
||
380 | */ |
||
381 | public function isAuthorized(): ? bool |
||
385 | |||
386 | /** |
||
387 | * @return Authorization |
||
388 | */ |
||
389 | public function allow(): self |
||
396 | |||
397 | /** |
||
398 | * @return Authorization |
||
399 | */ |
||
400 | public function deny(): self |
||
407 | |||
408 | /** |
||
409 | * @param string $key |
||
410 | * |
||
411 | * @return bool |
||
412 | */ |
||
413 | public function hasData(string $key): bool |
||
417 | |||
418 | /** |
||
419 | * @param string $key |
||
420 | * |
||
421 | * @return mixed |
||
422 | */ |
||
423 | public function getData(string $key) |
||
431 | |||
432 | /** |
||
433 | * @param string $key |
||
434 | * @param mixed $data |
||
435 | * |
||
436 | * @return Authorization |
||
437 | */ |
||
438 | public function withData(string $key, $data): self |
||
445 | |||
446 | /** |
||
447 | * @return null|ResourceServer |
||
448 | */ |
||
449 | public function getResourceServer(): ? ResourceServer |
||
453 | |||
454 | /** |
||
455 | * @param ResourceServer $resourceServer |
||
456 | * |
||
457 | * @return Authorization |
||
458 | */ |
||
459 | public function withResourceServer(ResourceServer $resourceServer): self |
||
466 | |||
467 | /** |
||
468 | * @param string $option |
||
469 | * @param mixed $value |
||
470 | * |
||
471 | * @return Authorization |
||
472 | */ |
||
473 | public function withConsentScreenOption(string $option, $value): self |
||
480 | |||
481 | /** |
||
482 | * @param string $option |
||
483 | * |
||
484 | * @return Authorization |
||
485 | */ |
||
486 | public function withoutConsentScreenOption(string $option): self |
||
497 | } |
||
498 |