From 17477d9f3c9b85d7ef61f5682afba823143602ed Mon Sep 17 00:00:00 2001 From: Jitendra Adhikari Date: Fri, 13 Dec 2019 18:46:10 +0700 Subject: [PATCH] refactor: add typehints --- src/JWT.php | 25 +++++++++++++++---------- src/ValidatesJWT.php | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/JWT.php b/src/JWT.php index 9b414a2..2f08a07 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -77,8 +77,13 @@ class JWT * @param int $leeway Leeway for clock skew. Shouldnot be more than 2 minutes (120s). * @param string $pass The passphrase (only for RS* algos). */ - public function __construct($key, $algo = 'HS256', $maxAge = 3600, $leeway = 0, $pass = null) - { + public function __construct( + $key, + string $algo = 'HS256', + int $maxAge = 3600, + int $leeway = 0, + string $pass = null + ) { $this->validateConfig($key, $algo, $maxAge, $leeway); if (\is_array($key)) { @@ -100,7 +105,7 @@ public function __construct($key, $algo = 'HS256', $maxAge = 3600, $leeway = 0, * * @return self */ - public function registerKeys(array $keys) + public function registerKeys(array $keys): self { $this->keys = \array_merge($this->keys, $keys); @@ -115,7 +120,7 @@ public function registerKeys(array $keys) * * @return string URL safe JWT token. */ - public function encode(array $payload, array $header = []) + public function encode(array $payload, array $header = []): string { $header = ['typ' => 'JWT', 'alg' => $this->algo] + $header; @@ -139,7 +144,7 @@ public function encode(array $payload, array $header = []) * * @return array */ - public function decode($token) + public function decode(string $token): array { if (\substr_count($token, '.') < 2) { throw new JWTException('Invalid token: Incomplete segments', static::ERROR_TOKEN_INVALID); @@ -165,7 +170,7 @@ public function decode($token) * * @param int|null $timestamp */ - public function setTestTimestamp($timestamp = null) + public function setTestTimestamp(int $timestamp = null): self { $this->timestamp = $timestamp; @@ -179,7 +184,7 @@ public function setTestTimestamp($timestamp = null) * * @return string */ - protected function sign($input) + protected function sign(string $input): string { // HMAC SHA. if (\substr($this->algo, 0, 2) === 'HS') { @@ -203,7 +208,7 @@ protected function sign($input) * * @return bool */ - protected function verify($input, $signature) + protected function verify(string $input, string $signature): bool { $algo = $this->algos[$this->algo]; @@ -230,7 +235,7 @@ protected function verify($input, $signature) * * @return string */ - protected function urlSafeEncode($data) + protected function urlSafeEncode($data): string { if (\is_array($data)) { $data = \json_encode($data, \JSON_UNESCAPED_SLASHES); @@ -250,7 +255,7 @@ protected function urlSafeEncode($data) * * @return array|\stdClass|string */ - protected function urlSafeDecode($data, $asJson = true) + protected function urlSafeDecode($data, bool $asJson = true) { if (!$asJson) { return \base64_decode(\strtr($data, '-_', '+/')); diff --git a/src/ValidatesJWT.php b/src/ValidatesJWT.php index 5da59fd..9db8ea1 100644 --- a/src/ValidatesJWT.php +++ b/src/ValidatesJWT.php @@ -26,7 +26,7 @@ trait ValidatesJWT * * @codeCoverageIgnore */ - protected function validateConfig($key, $algo, $maxAge, $leeway) + protected function validateConfig($key, string $algo, int $maxAge, int $leeway) { if (empty($key)) { throw new JWTException('Signing key cannot be empty', static::ERROR_KEY_EMPTY);