Complex classes like IdTokenBuilder 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 IdTokenBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class IdTokenBuilder |
||
|
|||
35 | { |
||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $issuer; |
||
40 | |||
41 | /** |
||
42 | * @var Client |
||
43 | */ |
||
44 | private $client; |
||
45 | |||
46 | /** |
||
47 | * @var UserAccount |
||
48 | */ |
||
49 | private $userAccount; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $redirectUri; |
||
55 | |||
56 | /** |
||
57 | * @var UserInfo |
||
58 | */ |
||
59 | private $userinfo; |
||
60 | |||
61 | /** |
||
62 | * @var JWKSet |
||
63 | */ |
||
64 | private $signatureKeys; |
||
65 | |||
66 | /** |
||
67 | * @var int |
||
68 | */ |
||
69 | private $lifetime; |
||
70 | |||
71 | /** |
||
72 | * @var string|null |
||
73 | */ |
||
74 | private $scope = null; |
||
75 | |||
76 | /** |
||
77 | * @var array |
||
78 | */ |
||
79 | private $requestedClaims = []; |
||
80 | |||
81 | /** |
||
82 | * @var string|null |
||
83 | */ |
||
84 | private $claimsLocales = null; |
||
85 | |||
86 | /** |
||
87 | * @var TokenId|null |
||
88 | */ |
||
89 | private $accessTokenId = null; |
||
90 | |||
91 | /** |
||
92 | * @var TokenId|null |
||
93 | */ |
||
94 | private $authorizationCodeId = null; |
||
95 | |||
96 | /** |
||
97 | * @var string|null |
||
98 | */ |
||
99 | private $nonce = null; |
||
100 | |||
101 | /** |
||
102 | * @var bool |
||
103 | */ |
||
104 | private $withAuthenticationTime = false; |
||
105 | |||
106 | /** |
||
107 | * @var JWSBuilder|null |
||
108 | */ |
||
109 | private $jwsBuilder = null; |
||
110 | |||
111 | /** |
||
112 | * @var string|null |
||
113 | */ |
||
114 | private $signatureAlgorithm = null; |
||
115 | |||
116 | /** |
||
117 | * @var JWEBuilder|null |
||
118 | */ |
||
119 | private $jweBuilder; |
||
120 | |||
121 | /** |
||
122 | * @var string|null |
||
123 | */ |
||
124 | private $keyEncryptionAlgorithm = null; |
||
125 | |||
126 | /** |
||
127 | * @var string|null |
||
128 | */ |
||
129 | private $contentEncryptionAlgorithm = null; |
||
130 | |||
131 | /** |
||
132 | * @var \DateTimeImmutable|null |
||
133 | */ |
||
134 | private $expiresAt = null; |
||
135 | |||
136 | /** |
||
137 | * @var null|JKUFactory |
||
138 | */ |
||
139 | private $jkuFactory = null; |
||
140 | |||
141 | /** |
||
142 | * @var null|AuthorizationCodeRepository |
||
143 | */ |
||
144 | private $authorizationCodeRepository = null; |
||
145 | |||
146 | private function __construct(string $issuer, UserInfo $userinfo, int $lifetime, Client $client, UserAccount $userAccount, string $redirectUri, ?JKUFactory $jkuFactory, ?AuthorizationCodeRepository $authorizationCodeRepository) |
||
157 | |||
158 | public static function create(string $issuer, UserInfo $userinfo, int $lifetime, Client $client, UserAccount $userAccount, string $redirectUri, ?JKUFactory $jkuFactory, ?AuthorizationCodeRepository $authorizationCodeRepository): self |
||
162 | |||
163 | /** |
||
164 | * @param AccessToken $accessToken |
||
165 | * |
||
166 | * @return IdTokenBuilder |
||
167 | */ |
||
168 | public function withAccessToken(AccessToken $accessToken): self |
||
192 | |||
193 | /** |
||
194 | * @param AccessTokenId $accessTokenId |
||
195 | * |
||
196 | * @return IdTokenBuilder |
||
197 | */ |
||
198 | public function withAccessTokenId(AccessTokenId $accessTokenId): self |
||
204 | |||
205 | /** |
||
206 | * @param AuthorizationCodeId $authorizationCodeId |
||
207 | * |
||
208 | * @return IdTokenBuilder |
||
209 | */ |
||
210 | public function withAuthorizationCodeId(AuthorizationCodeId $authorizationCodeId): self |
||
216 | |||
217 | /** |
||
218 | * @param string $claimsLocales |
||
219 | * |
||
220 | * @return IdTokenBuilder |
||
221 | */ |
||
222 | public function withClaimsLocales(string $claimsLocales): self |
||
228 | |||
229 | /** |
||
230 | * @return IdTokenBuilder |
||
231 | */ |
||
232 | public function withAuthenticationTime(): self |
||
238 | |||
239 | /** |
||
240 | * @param string $scope |
||
241 | * |
||
242 | * @return IdTokenBuilder |
||
243 | */ |
||
244 | public function withScope(string $scope): self |
||
250 | |||
251 | /** |
||
252 | * @param array $requestedClaims |
||
253 | * |
||
254 | * @return IdTokenBuilder |
||
255 | */ |
||
256 | public function withRequestedClaims(array $requestedClaims): self |
||
262 | |||
263 | /** |
||
264 | * @param string $nonce |
||
265 | * |
||
266 | * @return IdTokenBuilder |
||
267 | */ |
||
268 | public function withNonce(string $nonce): self |
||
274 | |||
275 | /** |
||
276 | * @param \DateTimeImmutable $expiresAt |
||
277 | * |
||
278 | * @return IdTokenBuilder |
||
279 | */ |
||
280 | public function withExpirationAt(\DateTimeImmutable $expiresAt): self |
||
286 | |||
287 | /** |
||
288 | * @return IdTokenBuilder |
||
289 | */ |
||
290 | public function withoutAuthenticationTime(): self |
||
296 | |||
297 | /** |
||
298 | * @param JWSBuilder $jwsBuilder |
||
299 | * @param JWKSet $signatureKeys |
||
300 | * @param string $signatureAlgorithm |
||
301 | * |
||
302 | * @return IdTokenBuilder |
||
303 | */ |
||
304 | public function withSignature(JWSBuilder $jwsBuilder, JWKSet $signatureKeys, string $signatureAlgorithm): self |
||
318 | |||
319 | /** |
||
320 | * @param JWEBuilder $jweBuilder |
||
321 | * @param string $keyEncryptionAlgorithm |
||
322 | * @param string $contentEncryptionAlgorithm |
||
323 | * |
||
324 | * @return IdTokenBuilder |
||
325 | */ |
||
326 | public function withEncryption(JWEBuilder $jweBuilder, string $keyEncryptionAlgorithm, string $contentEncryptionAlgorithm): self |
||
340 | |||
341 | /** |
||
342 | * @return string |
||
343 | */ |
||
344 | public function build(): string |
||
368 | |||
369 | private function updateClaimsWithJwtClaims(array $claims): array |
||
384 | |||
385 | /** |
||
386 | * @param array $claims |
||
387 | * @param UserAccount $userAccount |
||
388 | * |
||
389 | * @return array |
||
390 | */ |
||
391 | private function updateClaimsWithAuthenticationTime(array $claims, UserAccount $userAccount, array $requestedClaims): array |
||
399 | |||
400 | private function updateClaimsWithNonce(array $claims): array |
||
408 | |||
409 | private function updateClaimsAudience(array $claims): array |
||
419 | |||
420 | private function computeIdToken(array $claims): string |
||
435 | |||
436 | private function tryToEncrypt(Client $client, string $jwt): string |
||
461 | |||
462 | /** |
||
463 | * @param string $signatureAlgorithm |
||
464 | * |
||
465 | * @return JWK |
||
466 | */ |
||
467 | private function getSignatureKey(string $signatureAlgorithm): JWK |
||
489 | |||
490 | /** |
||
491 | * @param JWK $signatureKey |
||
492 | * @param string $signatureAlgorithm |
||
493 | * |
||
494 | * @return array |
||
495 | */ |
||
496 | private function getHeaders(JWK $signatureKey, string $signatureAlgorithm): array |
||
508 | |||
509 | /** |
||
510 | * @param array $claims |
||
511 | * |
||
512 | * @return array |
||
513 | */ |
||
514 | private function updateClaimsWithTokenHash(array $claims): array |
||
528 | |||
529 | /** |
||
530 | * @param TokenId $tokenId |
||
531 | * |
||
532 | * @return string |
||
533 | */ |
||
534 | private function getHash(TokenId $tokenId): string |
||
538 | |||
539 | /** |
||
540 | * @throws \InvalidArgumentException |
||
541 | * |
||
542 | * @return string |
||
543 | */ |
||
544 | private function getHashMethod(): string |
||
567 | |||
568 | /** |
||
569 | * @throws \InvalidArgumentException |
||
570 | * |
||
571 | * @return int |
||
572 | */ |
||
573 | private function getHashSize(): int |
||
596 | |||
597 | /** |
||
598 | * @param Client $client |
||
599 | * |
||
600 | * @return JWKSet |
||
601 | */ |
||
602 | private function getClientKeySet(Client $client): JWKSet |
||
632 | } |
||
633 |