| Conditions | 27 |
| Paths | 23 |
| Total Lines | 44 |
| Code Lines | 26 |
| Lines | 9 |
| Ratio | 20.45 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types = 1); |
||
| 153 | public function validateClaims(array $claims) |
||
| 154 | { |
||
| 155 | if ($this->requiredClaims) { |
||
|
|
|||
| 156 | $missing = array_diff_key(array_flip($this->requiredClaims), $claims); |
||
| 157 | if (count($missing)) { |
||
| 158 | throw new MissingClaimsException("Missing claims: " . implode(', ', $missing)); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | if ($this->issuer && !isset($claims['iss'])) { |
||
| 162 | throw new MissingClaimsException("Claim 'iss' is required"); |
||
| 163 | } |
||
| 164 | if ($this->minIssueTime && !isset($claims['iat'])) { |
||
| 165 | throw new MissingClaimsException("Claim 'iat' is required"); |
||
| 166 | } |
||
| 167 | if (!empty($this->audience) && !isset($claims['aud'])) { |
||
| 168 | throw new MissingClaimsException("Claim 'aud' is required"); |
||
| 169 | } |
||
| 170 | if ((!isset($claims['sub']) || empty($claims['sub'])) && (!isset($claims['prn']) || empty($claims['prn']))) { |
||
| 171 | throw new MissingClaimsException("Missing principle subject claim"); |
||
| 172 | } |
||
| 173 | View Code Duplication | if (isset($claims['exp']) && $claims['exp'] + $this->issuerTimeLeeway < time()) { |
|
| 174 | throw new InvalidTimeException("Token is expired by 'exp'"); |
||
| 175 | } |
||
| 176 | View Code Duplication | if (isset($claims['iat']) && $claims['iat'] < ($this->minIssueTime + $this->issuerTimeLeeway)) { |
|
| 177 | throw new InvalidTimeException("Server deemed your token too old"); |
||
| 178 | } |
||
| 179 | View Code Duplication | if (isset($claims['nbf']) && ($claims['nbf'] - $this->issuerTimeLeeway) > time()) { |
|
| 180 | throw new InvalidTimeException("Token not valid yet"); |
||
| 181 | } |
||
| 182 | if (isset($claims['iss']) && $claims['iss'] !== $this->issuer) { |
||
| 183 | throw new KeyTokenMismatchException("Issuer mismatch"); |
||
| 184 | } |
||
| 185 | |||
| 186 | if (count($this->audience)) { |
||
| 187 | if (isset($claims['aud']) && |
||
| 188 | ( |
||
| 189 | (is_array($this->audience) && !in_array($claims['aud'], $this->audience)) |
||
| 190 | || (!is_array($this->audience) && $claims['aud'] !== $this->audience) |
||
| 191 | ) |
||
| 192 | ) { |
||
| 193 | throw new KeyTokenMismatchException("Audience mismatch"); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 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.