Skip to content

Commit 17477d9

Browse files
committed
refactor: add typehints
1 parent cca8b01 commit 17477d9

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

‎src/JWT.php‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ class JWT
7777
* @param int $leeway Leeway for clock skew. Shouldnot be more than 2 minutes (120s).
7878
* @param string $pass The passphrase (only for RS* algos).
7979
*/
80-
public function __construct($key, $algo = 'HS256', $maxAge = 3600, $leeway = 0, $pass = null)
81-
{
80+
public function __construct(
81+
$key,
82+
string $algo = 'HS256',
83+
int $maxAge = 3600,
84+
int $leeway = 0,
85+
string $pass = null
86+
) {
8287
$this->validateConfig($key, $algo, $maxAge, $leeway);
8388

8489
if (\is_array($key)) {
@@ -100,7 +105,7 @@ public function __construct($key, $algo = 'HS256', $maxAge = 3600, $leeway = 0,
100105
*
101106
* @return self
102107
*/
103-
public function registerKeys(array $keys)
108+
public function registerKeys(array $keys): self
104109
{
105110
$this->keys = \array_merge($this->keys, $keys);
106111

@@ -115,7 +120,7 @@ public function registerKeys(array $keys)
115120
*
116121
* @return string URL safe JWT token.
117122
*/
118-
public function encode(array $payload, array $header = [])
123+
public function encode(array $payload, array $header = []): string
119124
{
120125
$header = ['typ' => 'JWT', 'alg' => $this->algo] + $header;
121126

@@ -139,7 +144,7 @@ public function encode(array $payload, array $header = [])
139144
*
140145
* @return array
141146
*/
142-
public function decode($token)
147+
public function decode(string $token): array
143148
{
144149
if (\substr_count($token, '.') < 2) {
145150
throw new JWTException('Invalid token: Incomplete segments', static::ERROR_TOKEN_INVALID);
@@ -165,7 +170,7 @@ public function decode($token)
165170
*
166171
* @param int|null $timestamp
167172
*/
168-
public function setTestTimestamp($timestamp = null)
173+
public function setTestTimestamp(int $timestamp = null): self
169174
{
170175
$this->timestamp = $timestamp;
171176

@@ -179,7 +184,7 @@ public function setTestTimestamp($timestamp = null)
179184
*
180185
* @return string
181186
*/
182-
protected function sign($input)
187+
protected function sign(string $input): string
183188
{
184189
// HMAC SHA.
185190
if (\substr($this->algo, 0, 2) === 'HS') {
@@ -203,7 +208,7 @@ protected function sign($input)
203208
*
204209
* @return bool
205210
*/
206-
protected function verify($input, $signature)
211+
protected function verify(string $input, string $signature): bool
207212
{
208213
$algo = $this->algos[$this->algo];
209214

@@ -230,7 +235,7 @@ protected function verify($input, $signature)
230235
*
231236
* @return string
232237
*/
233-
protected function urlSafeEncode($data)
238+
protected function urlSafeEncode($data): string
234239
{
235240
if (\is_array($data)) {
236241
$data = \json_encode($data, \JSON_UNESCAPED_SLASHES);
@@ -250,7 +255,7 @@ protected function urlSafeEncode($data)
250255
*
251256
* @return array|\stdClass|string
252257
*/
253-
protected function urlSafeDecode($data, $asJson = true)
258+
protected function urlSafeDecode($data, bool $asJson = true)
254259
{
255260
if (!$asJson) {
256261
return \base64_decode(\strtr($data, '-_', '+/'));

‎src/ValidatesJWT.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ trait ValidatesJWT
2626
*
2727
* @codeCoverageIgnore
2828
*/
29-
protected function validateConfig($key, $algo, $maxAge, $leeway)
29+
protected function validateConfig($key, string $algo, int $maxAge, int $leeway)
3030
{
3131
if (empty($key)) {
3232
throw new JWTException('Signing key cannot be empty', static::ERROR_KEY_EMPTY);

0 commit comments

Comments
 (0)