Skip to content

Commit 0c06619

Browse files
committed
- adds PHP 8 types
1 parent f792f96 commit 0c06619

3 files changed

Lines changed: 15 additions & 55 deletions

File tree

CacheItem.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,52 @@
44

55
use Psr\Cache\CacheItemInterface;
66

7-
87
abstract class CacheItem implements CacheItemInterface
98
{
10-
/** @var bool */
11-
protected $isHit = false;
12-
13-
/** @var string Expects a valid cache key name */
14-
private $key;
15-
16-
/** @var mixed */
17-
private $value;
18-
19-
/** @var int Number of seconds for the expiration time */
20-
private $expiresAt;
21-
9+
protected bool $isHit = false;
10+
private string $key;
11+
private mixed $value = null;
12+
private ?int $expiresAt;
2213

2314
public function __construct($key, ?int $ttl)
2415
{
2516
$this->key = $key;
2617
$this->expiresAt = $ttl;
2718
}
2819

29-
3020
public function getKey(): string
3121
{
3222
return $this->key;
3323
}
3424

35-
3625
public function get()
3726
{
3827
return $this->value;
3928
}
4029

41-
4230
public function isHit(): bool
4331
{
4432
return $this->isHit;
4533
}
4634

47-
4835
public function set($value)
4936
{
5037
$this->value = $value;
51-
5238
return $this;
5339
}
5440

55-
56-
public function expiresAfter($time)
41+
public function expiresAfter($time): static
5742
{
5843
// The TTL is calculated in the cache client instance
5944
return $this->expiresAt($time);
6045
}
6146

62-
63-
public function expiresAt($expiration)
47+
public function expiresAt($expiration): static
6448
{
6549
$this->expiresAt = normalize_ttl($expiration ?? $this->expiresAt);
66-
6750
if ($this->expiresAt < 1) {
6851
$this->isHit = false;
6952
}
70-
7153
return $this;
7254
}
7355

CacheItemPool.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
use Psr\Cache\{CacheItemInterface, CacheItemPoolInterface};
77
use function Koded\Stdlib\now;
88

9-
109
abstract class CacheItemPool implements CacheItemPoolInterface
1110
{
12-
/** @var Cache */
13-
protected $client;
11+
protected Cache $client;
1412

1513
/** @var CacheItemInterface[] */
16-
private $deferred = [];
17-
14+
private array $deferred = [];
1815

1916
abstract public function __construct(string $client, array $parameters);
2017

@@ -32,34 +29,28 @@ public function commit(): bool
3229
unset($this->deferred[$key]);
3330
}
3431
}
35-
3632
return empty($this->deferred);
3733
}
3834

39-
4035
public function save(CacheItemInterface $item): bool
4136
{
4237
/** @var CacheItem $item */
4338
return $this->client->set($item->getKey(), $item->get(), $item->getExpiresAt());
4439
}
4540

46-
4741
public function getItems(array $keys = []): array
4842
{
4943
$items = [];
5044
foreach ($keys as $key) {
5145
$items[$key] = $this->getItem($key);
5246
}
53-
5447
return $items;
5548
}
5649

57-
5850
public function getItem($key): CacheItemInterface
5951
{
6052
try {
6153
$item = new class($key, $this->client->getTtl()) extends CacheItem {};
62-
6354
if (false === $this->client->has($key)) {
6455
if (isset($this->deferred[$key])) {
6556
return clone $this->deferred[$key];
@@ -79,7 +70,6 @@ public function getItem($key): CacheItemInterface
7970
}
8071
}
8172

82-
8373
public function hasItem($key): bool
8474
{
8575
try {
@@ -89,17 +79,14 @@ public function hasItem($key): bool
8979
}
9080
}
9181

92-
9382
public function clear(): bool
9483
{
9584
if ($cleared = $this->client->clear()) {
9685
$this->deferred = [];
9786
}
98-
9987
return $cleared;
10088
}
10189

102-
10390
public function deleteItems(array $keys): bool
10491
{
10592
try {
@@ -109,22 +96,18 @@ public function deleteItems(array $keys): bool
10996
}
11097
}
11198

112-
11399
public function deleteItem($key): bool
114100
{
115101
try {
116102
if ($deleted = $this->client->delete($key)) {
117103
unset($this->deferred[$key]);
118104
}
119-
120105
return $deleted;
121-
122106
} catch (Exception $e) {
123107
throw CachePoolException::from($e);
124108
}
125109
}
126110

127-
128111
public function saveDeferred(CacheItemInterface $item): bool
129112
{
130113
/** @var CacheItem $item */
@@ -139,4 +122,4 @@ public function saveDeferred(CacheItemInterface $item): bool
139122

140123
return true;
141124
}
142-
}
125+
}

CachePool.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,20 @@
2222
*/
2323
final class CachePool
2424
{
25-
/**
26-
* @var CacheItemPoolInterface[] The registry
27-
*/
28-
private static $instances = [];
25+
/** @var CacheItemPoolInterface[] The registry */
26+
private static array $instances = [];
2927

3028
/**
3129
* @param string $client The name of the cache client
3230
* @param array $parameters [optional] Configuration parameters for the cache client
33-
*
3431
* @return CacheItemPoolInterface
3532
*/
3633
public static function use(string $client, array $parameters = []): CacheItemPoolInterface
3734
{
38-
$ident = md5($client . serialize($parameters));
39-
35+
$ident = \md5($client . \serialize($parameters));
4036
if (isset(self::$instances[$ident])) {
4137
return self::$instances[$ident];
4238
}
43-
4439
return self::$instances[$ident] = new class($client, $parameters) extends CacheItemPool
4540
{
4641
public function __construct(string $client, array $parameters)
@@ -57,8 +52,8 @@ public function __construct(string $client, array $parameters)
5752
*/
5853
class CachePoolException extends Exception implements InvalidArgumentException
5954
{
60-
public static function from(Exception $e)
55+
public static function from(Exception $e): static
6156
{
62-
return new self($e->getMessage(), $e->getCode());
57+
return new static($e->getMessage(), $e->getCode());
6358
}
64-
}
59+
}

0 commit comments

Comments
 (0)