From 6b0f95118772710dd7a139f5c25ff4572725d9cc Mon Sep 17 00:00:00 2001 From: Arnold Daniels Date: Mon, 8 Feb 2021 02:53:22 +0100 Subject: [PATCH] Make PHPStan happy. --- src/Confirmation/HashidsConfirmation.php | 5 ++++- src/Confirmation/TokenConfirmation.php | 8 +++++--- src/Session/Jwt.php | 8 ++++++-- src/User/BasicUser.php | 4 ++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Confirmation/HashidsConfirmation.php b/src/Confirmation/HashidsConfirmation.php index b9c4369..5613bf3 100644 --- a/src/Confirmation/HashidsConfirmation.php +++ b/src/Confirmation/HashidsConfirmation.php @@ -48,7 +48,10 @@ public function __construct(string $secret, ?callable $createHashids = null) ? \Closure::fromCallable($createHashids) : fn(string $salt) => new Hashids($salt); - $this->encodeUid = fn(string $uid) => unpack('H*', $uid)[1]; + $this->encodeUid = function (string $uid) { + $unpacked = unpack('H*', $uid); + return $unpacked !== false ? $unpacked[1] : ''; + }; $this->decodeUid = fn(string $hex) => pack('H*', $hex); $this->logger = new NullLogger(); diff --git a/src/Confirmation/TokenConfirmation.php b/src/Confirmation/TokenConfirmation.php index ff4b532..4250581 100644 --- a/src/Confirmation/TokenConfirmation.php +++ b/src/Confirmation/TokenConfirmation.php @@ -21,7 +21,6 @@ class TokenConfirmation implements ConfirmationInterface protected int $numberOfBytes; - /** @phpstan-param \Closure&callable(string):string */ protected \Closure $encode; protected TokenStorageInterface $storage; @@ -31,7 +30,7 @@ class TokenConfirmation implements ConfirmationInterface /** * Class constructor. - * + * * @param int $numberOfBytes Number of bytes of the random string. * @param callable|null $encode Method to encode random string. */ @@ -44,7 +43,10 @@ public function __construct(int $numberOfBytes = 16, ?callable $encode = null) } /** - * @inheritDoc + * Get copy with storage service. + * + * @param Storage $storage + * @return static */ public function withStorage(Storage $storage): self { diff --git a/src/Session/Jwt.php b/src/Session/Jwt.php index ed84942..e982681 100644 --- a/src/Session/Jwt.php +++ b/src/Session/Jwt.php @@ -4,10 +4,13 @@ namespace Jasny\Auth\Session; +use DateTimeImmutable; use Jasny\Auth\Session\Jwt\Cookie; use Jasny\Auth\Session\Jwt\CookieInterface; use Jasny\Immutable; use Lcobucci\JWT\Configuration; +use Lcobucci\JWT\Token; +use Lcobucci\JWT\UnencryptedToken; /** * Use JSON Web Token and JSON Web Signature (RFC 7519) to store auth session info. @@ -55,14 +58,14 @@ public function getInfo(): array { $jwt = $this->cookie->get(); + /** @var (Token&UnencryptedToken)|null $token */ $token = $jwt !== null && $jwt !== '' ? $this->jwtConfig->parser()->parse($jwt) : null; $constraints = $this->jwtConfig->validationConstraints(); - if ( - $token === null || + if ($token === null || ($constraints !== [] && !$this->jwtConfig->validator()->validate($token, ...$constraints)) ) { return ['user' => null, 'context' => null, 'checksum' => null, 'timestamp' => null]; @@ -91,6 +94,7 @@ public function persist($userId, $contextId, ?string $checksum, ?\DateTimeInterf if ($timestamp instanceof \DateTime) { $timestamp = \DateTimeImmutable::createFromMutable($timestamp); } + /** @var DateTimeImmutable|null $timestamp */ $time = $timestamp ?? new \DateTimeImmutable(); $expire = $time->add(new \DateInterval("PT{$this->ttl}S")); diff --git a/src/User/BasicUser.php b/src/User/BasicUser.php index 1a0498c..14c7159 100644 --- a/src/User/BasicUser.php +++ b/src/User/BasicUser.php @@ -63,8 +63,8 @@ public function requiresMfa(): bool /** * Factory method; create object from data loaded from DB. * - * @param array $data - * @return static + * @phpstan-param array $data + * @phpstan-return self */ public static function fromData(array $data): self {