Skip to content

Commit

Permalink
refactor: add typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Dec 13, 2019
1 parent cca8b01 commit 17477d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);

Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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') {
Expand All @@ -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];

Expand All @@ -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);
Expand All @@ -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, '-_', '+/'));
Expand Down
2 changes: 1 addition & 1 deletion src/ValidatesJWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 17477d9

Please sign in to comment.