Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class User extends \yii\web\User |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var string JWT sign key. Must be random and secret. |
||
| 32 | * @see https://tools.ietf.org/html/rfc7519#section-11 |
||
| 33 | * @since 3.0 |
||
| 34 | */ |
||
| 35 | public $key; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool whether to use a [[IdentityInterface::getAuthKey()]] value to validate a token. |
||
| 39 | * @since 3.0 |
||
| 40 | */ |
||
| 41 | public $useAuthKey = true; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool whether to append a [[IdentityInterface::getAuthKey()]] value to the sign key or store it as a claim. |
||
| 45 | * @since 3.0 |
||
| 46 | */ |
||
| 47 | public $appendAuthKey = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var \Closure|string JWT audience claim ("aud"). |
||
| 51 | * @see https://tools.ietf.org/html/rfc7519#section-4.1.3 |
||
| 52 | * @since 1.1 |
||
| 53 | */ |
||
| 54 | public $audience; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \Closure|string JWT issuer claim ("iss"). |
||
| 58 | * @see https://tools.ietf.org/html/rfc7519#section-4.1.1 |
||
| 59 | * @since 3.0 |
||
| 60 | */ |
||
| 61 | public $issuer; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @inheritDoc |
||
| 65 | */ |
||
| 66 | 432 | protected function renewIdentityCookie() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @inheritDoc |
||
| 93 | */ |
||
| 94 | 432 | protected function sendIdentityCookie($identity, $duration) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @inheritDoc |
||
| 120 | */ |
||
| 121 | 768 | protected function getIdentityAndDurationFromCookie() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @return array|null |
||
| 148 | */ |
||
| 149 | 1974 | private function getIdentityAndTokenFromCookie() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param \Closure|string|null $value |
||
| 178 | * @return string|null |
||
| 179 | */ |
||
| 180 | 1074 | private function getPrincipal($value) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param IdentityInterface|null $identity |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | 1973 | private function getKey(IdentityInterface $identity = null) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @param Token $token |
||
| 212 | * @param IdentityInterface|null $identity |
||
| 213 | */ |
||
| 214 | 1973 | private function assertSignature(Token $token, IdentityInterface $identity = null) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @param Token $token |
||
| 224 | */ |
||
| 225 | 858 | private function assertClaims(Token $token) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @param Token $token |
||
| 243 | * @return IdentityInterface|null |
||
| 244 | */ |
||
| 245 | 834 | private function getIdentityFromToken(Token $token) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param Token $token |
||
| 272 | * @return Builder |
||
| 273 | */ |
||
| 274 | 216 | private function createBuilderFromToken(Token $token) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @param Builder $builder |
||
| 285 | * @param IdentityInterface $identity |
||
| 286 | */ |
||
| 287 | 432 | private function sendToken(Builder $builder, IdentityInterface $identity) |
|
| 296 | } |
||
| 297 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: