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:
| 1 | <?php |
||
| 10 | class TokenValidator |
||
| 11 | { |
||
| 12 | protected $sign; |
||
| 13 | protected $base64url; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Create a new TokenValidator. |
||
| 17 | * |
||
| 18 | * @param callable $sign Callable used for generating the token signatures |
||
| 19 | */ |
||
| 20 | public function __construct(callable $sign) |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Determine constraint violations of a CSRF token. |
||
| 28 | * |
||
| 29 | * @param string $nonce Value used to associate a client session |
||
| 30 | * @param string $token The token to validate |
||
| 31 | * @param int $now The current time, defaults to `time()` |
||
| 32 | * @param int $leeway The leeway in seconds |
||
| 33 | * |
||
| 34 | * @return InvalidArgumentException[] Constraint violations; if $token is valid, an empty array |
||
| 35 | */ |
||
| 36 | public function __invoke($nonce, $token, $now = null, $leeway = 0) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Parse a CSRF token. |
||
| 49 | * |
||
| 50 | * @param string $token The token to parse |
||
| 51 | * |
||
| 52 | * @return \stdClass Parse result containing payload and constraint violations |
||
| 53 | */ |
||
| 54 | protected function parse($token) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Validate the payload of a CSRF token. |
||
| 86 | * |
||
| 87 | * @param string $nonce Value used to associate a client session |
||
| 88 | * @param \stdClass $payload The token payload to validate |
||
| 89 | * @param int $now The current time, defaults to `time()` |
||
| 90 | * @param int $leeway The leeway in seconds |
||
| 91 | * |
||
| 92 | * @return InvalidArgumentException[] Constraint violations; if $payload is valid, an empty array |
||
| 93 | */ |
||
| 94 | protected function validatePayload($nonce, \stdClass $payload, $now = null, $leeway = 0) |
||
| 115 | } |
||
| 116 |