Skip to content

Commit e5f9a53

Browse files
committed
fix phpstan
1 parent 83ac363 commit e5f9a53

File tree

3 files changed

+68
-25
lines changed

3 files changed

+68
-25
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
}
3232
},
3333
"require-dev": {
34-
"phpunit/phpunit": "^7.5||9.5"
34+
"phpunit/phpunit": "^7.5||9.5",
3535
"psr/cache": "^3.0",
3636
"guzzlehttp/guzzle": "^7.4",
3737
"phpspec/prophecy-phpunit": "^2.0"

src/CachedKeySet.php

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,64 @@
77
use Psr\Http\Message\RequestFactoryInterface;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Cache\CacheItemPoolInterface;
10+
use Psr\Cache\CacheItemInterface;
1011
use LogicException;
1112
use OutOfBoundsException;
1213
use RuntimeException;
1314

15+
/**
16+
* @template-implements ArrayAccess<string, Key>
17+
*/
1418
class CachedKeySet implements ArrayAccess
1519
{
20+
/**
21+
* @var string
22+
*/
1623
private $jwkUri;
24+
/**
25+
* @var ClientInterface
26+
*/
1727
private $httpClient;
28+
/**
29+
* @var RequestFactoryInterface
30+
*/
1831
private $httpFactory;
32+
/**
33+
* @var CacheItemPoolInterface
34+
*/
1935
private $cache;
36+
/**
37+
* @var int
38+
*/
2039
private $expiresAfter;
2140

41+
/**
42+
* @var ?CacheItemInterface
43+
*/
2244
private $cacheItem;
45+
/**
46+
* @var array<string, Key>
47+
*/
2348
private $keySet;
49+
/**
50+
* @var string
51+
*/
52+
private $cacheKey;
53+
/**
54+
* @var string
55+
*/
2456
private $cacheKeyPrefix = 'jwk';
57+
/**
58+
* @var int
59+
*/
2560
private $maxKeyLength = 64;
2661

2762
public function __construct(
28-
$jwkUri,
63+
string $jwkUri,
2964
ClientInterface $httpClient,
3065
RequestFactoryInterface $httpFactory,
3166
CacheItemPoolInterface $cache,
32-
$expiresAfter = null
67+
int $expiresAfter = null
3368
) {
3469
$this->jwkUri = $jwkUri;
3570
$this->cacheKey = $this->getCacheKey($jwkUri);
@@ -39,30 +74,45 @@ public function __construct(
3974
$this->expiresAfter = $expiresAfter;
4075
}
4176

42-
public function offsetGet($keyId)
77+
/**
78+
* @param string $keyId
79+
* @return Key
80+
*/
81+
public function offsetGet($keyId): Key
4382
{
4483
if (!$this->keyIdExists($keyId)) {
4584
throw new OutOfBoundsException('Key ID not found');
4685
}
4786
return $this->keySet[$keyId];
4887
}
4988

50-
public function offsetExists($keyId)
89+
/**
90+
* @param string $keyId
91+
* @return bool
92+
*/
93+
public function offsetExists($keyId): bool
5194
{
5295
return $this->keyIdExists($keyId);
5396
}
5497

55-
public function offsetSet($offset, $value)
98+
/**
99+
* @param string $offset
100+
* @param Key $value
101+
*/
102+
public function offsetSet($offset, $value): void
56103
{
57104
throw new LogicException('Method not implemented');
58105
}
59106

60-
public function offsetUnset($offset)
107+
/**
108+
* @param string $offset
109+
*/
110+
public function offsetUnset($offset): void
61111
{
62112
throw new LogicException('Method not implemented');
63113
}
64114

65-
private function keyIdExists($keyId)
115+
private function keyIdExists(string $keyId): bool
66116
{
67117
$keySetToCache = null;
68118
if (null === $this->keySet) {
@@ -97,18 +147,18 @@ private function keyIdExists($keyId)
97147
return true;
98148
}
99149

100-
private function getCacheItem()
150+
private function getCacheItem(): CacheItemInterface
101151
{
102-
if ($this->cacheItem) {
103-
return $this->cacheItem;
152+
if (is_null($this->cacheItem)) {
153+
$this->cacheItem = $this->cache->getItem($this->cacheKey);
104154
}
105155

106-
return $this->cacheItem = $this->cache->getItem($this->cacheKey);
156+
return $this->cacheItem;
107157
}
108158

109-
private function getCacheKey($jwkUri)
159+
private function getCacheKey(string $jwkUri): string
110160
{
111-
if (is_null($jwkUri)) {
161+
if (empty($jwkUri)) {
112162
throw new RuntimeException('JWK URI is empty');
113163
}
114164

src/JWT.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class JWT
6060
* Decodes a JWT string into a PHP object.
6161
*
6262
* @param string $jwt The JWT
63-
* @param Key|array<string, Key> $keyOrKeyArray The Key or associative array of key IDs (kid) to Key objects.
63+
* @param Key|array<string,Key> $keyOrKeyArray The Key or associative array of key IDs (kid) to Key objects.
6464
* If the algorithm used is asymmetric, this is the public key
6565
* Each Key object contains an algorithm and matching key.
6666
* Supported algorithms are 'ES384','ES256', 'HS256', 'HS384',
@@ -395,7 +395,7 @@ public static function urlsafeB64Encode(string $input): string
395395
/**
396396
* Determine if an algorithm has been provided for each Key
397397
*
398-
* @param Key|ArrayAccess|array<string, Key> $keyOrKeyArray
398+
* @param Key|ArrayAccess<string,Key>|array<string,Key> $keyOrKeyArray
399399
* @param string|null $kid
400400
*
401401
* @throws UnexpectedValueException
@@ -411,18 +411,11 @@ private static function getKey(
411411
}
412412

413413
if ($keyOrKeyArray instanceof CachedKeySet) {
414+
// Skip "isset" check, as this will automatically refresh if not set
414415
return $keyOrKeyArray[$kid];
415416
}
416417

417-
foreach ($keyOrKeyArray as $keyId => $key) {
418-
if (!$key instanceof Key) {
419-
throw new TypeError(
420-
'$keyOrKeyArray must be an instance of Firebase\JWT\Key key or an '
421-
. 'array of Firebase\JWT\Key keys'
422-
);
423-
}
424-
}
425-
if (!isset($kid)) {
418+
if (empty($kid)) {
426419
throw new UnexpectedValueException('"kid" empty, unable to lookup correct key');
427420
}
428421
if (!isset($keyOrKeyArray[$kid])) {

0 commit comments

Comments
 (0)