Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like JwtKey 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 JwtKey, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types = 1); |
||
| 21 | class JwtKey |
||
| 22 | { |
||
| 23 | const TYPE_HMAC = 'HS256'; |
||
| 24 | const TYPE_RSA = 'RS256'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $id; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $issuer; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $type = self::TYPE_HMAC; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $audience = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | private $minIssueTime; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $requiredClaims = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | private $issuerTimeLeeway; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private $secret; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var SecretLoader |
||
| 68 | */ |
||
| 69 | private $secretLoader; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param array $options |
||
| 73 | */ |
||
| 74 | public function __construct(array $options) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getId(): string |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param JwtToken $token |
||
| 114 | * |
||
| 115 | * @throws \InvalidArgumentException |
||
| 116 | */ |
||
| 117 | public function validateToken(JwtToken $token) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param array $header |
||
| 132 | * |
||
| 133 | * @throws \InvalidArgumentException |
||
| 134 | */ |
||
| 135 | public function validateHeader(array $header) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param array $claims |
||
| 150 | * |
||
| 151 | * @throws \InvalidArgumentException |
||
| 152 | */ |
||
| 153 | public function validateClaims(array $claims) |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * @return SignatureValidator |
||
| 201 | */ |
||
| 202 | public function getSignatureValidator(): SignatureValidator |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Prevent accidental persistence of secret |
||
| 213 | */ |
||
| 214 | final public function __sleep() |
||
| 218 | } |
||
| 219 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.