Skip to content

Commit 1a257ff

Browse files
committed
PHP 8.1 Implementation
1 parent b27df71 commit 1a257ff

15 files changed

+155
-118
lines changed

.github/workflows/phpunit.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ jobs:
1616
strategy:
1717
matrix:
1818
php-version:
19+
- "8.3"
1920
- "8.2"
2021
- "8.1"
21-
- "8.0"
22-
- "7.4"
2322

2423
# Service containers to run
2524
services:

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
}
88
},
99
"require": {
10-
"php": ">=7.4",
11-
"psr/cache": "^1.0",
12-
"psr/log": "^1.1",
13-
"psr/simple-cache": "^1.0",
10+
"php": ">=8.1",
11+
"psr/cache": "^1.0|^2.0|^3.0",
12+
"psr/log": "^1.1|^2.0|^3.0",
13+
"psr/simple-cache": "^1.0|^2.0|^3.0",
1414
"psr/container": "^2.0"
1515
},
1616
"require-dev": {
1717
"phpunit/phpunit": "5.7.*|7.4.*|^9.5"
1818
},
1919
"suggest": {
2020
"ext-memcached": "*",
21-
"ext-redis": "*"
21+
"ext-redis": "*",
22+
"ext-shmop": "*"
2223
},
2324
"license": "MIT"
2425
}

src/CacheAvailabilityInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ interface CacheAvailabilityInterface
99
* Return if this CacheEngine is available for use
1010
* @return bool
1111
*/
12-
public function isAvailable();
12+
public function isAvailable(): bool;
1313
}

src/CacheLockInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ interface CacheLockInterface
1414
* Lock resource before set it.
1515
* @param string $key
1616
*/
17-
public function lock($key);
17+
public function lock(string $key): void;
1818

1919
/**
2020
* Unlock resource
2121
* @param string $key
2222
*/
23-
public function unlock($key);
23+
public function unlock(string $key): void;
2424
}

src/Factory.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,54 @@
1313

1414
class Factory
1515
{
16-
public static function createNullPool()
16+
public static function createNullPool(): CachePool
1717
{
1818
return new CachePool(
1919
new NoCacheEngine()
2020
);
2121
}
2222

23-
public static function createSessionPool($prefix = null, $bufferSize = null)
23+
public static function createSessionPool($prefix = null, $bufferSize = null): CachePool
2424
{
2525
return new CachePool(
2626
new SessionCacheEngine($prefix),
2727
$bufferSize
2828
);
2929
}
3030

31-
public static function createFilePool($prefix = null, $path = null, $bufferSize = null, $logger = null)
31+
public static function createFilePool($prefix = null, $path = null, $bufferSize = null, $logger = null): CachePool
3232
{
3333
return new CachePool(
3434
new FileSystemCacheEngine($prefix, $path, $logger),
3535
$bufferSize
3636
);
3737
}
3838

39-
public static function createShmopPool($config = [], $bufferSize = null, $logger = null)
39+
public static function createShmopPool($config = [], $bufferSize = null, $logger = null): CachePool
4040
{
4141
return new CachePool(
4242
new ShmopCacheEngine($config, $logger),
4343
$bufferSize
4444
);
4545
}
4646

47-
public static function createArrayPool($bufferSize = null, $logger = null)
47+
public static function createArrayPool($bufferSize = null, $logger = null): CachePool
4848
{
4949
return new CachePool(
5050
new ArrayCacheEngine($logger),
5151
$bufferSize
5252
);
5353
}
5454

55-
public static function createMemcachedPool($servers = null, $bufferSize = null, $logger = null)
55+
public static function createMemcachedPool($servers = null, $bufferSize = null, $logger = null): CachePool
5656
{
5757
return new CachePool(
5858
new MemcachedEngine($servers, $logger),
5959
$bufferSize
6060
);
6161
}
6262

63-
public static function createRedisCacheEngine($servers = null, $password = null, $bufferSize = null, $logger = null)
63+
public static function createRedisCacheEngine($servers = null, $password = null, $bufferSize = null, $logger = null): CachePool
6464
{
6565
return new CachePool(
6666
new RedisCacheEngine($servers, $password, $logger),

src/Psr16/ArrayCacheEngine.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace ByJG\Cache\Psr16;
44

5+
use ByJG\Cache\Exception\InvalidArgumentException;
56
use DateInterval;
7+
use Psr\Container\ContainerExceptionInterface;
8+
use Psr\Container\NotFoundExceptionInterface;
69
use Psr\Log\NullLogger;
710

811
class ArrayCacheEngine extends BaseCacheEngine
@@ -29,10 +32,11 @@ public function __construct($logger = null)
2932
*
3033
* @param string $key The cache item key.
3134
* @return bool
32-
* @throws \Psr\SimpleCache\InvalidArgumentException
33-
* MUST be thrown if the $key string is not a legal value.
35+
* @throws InvalidArgumentException
36+
* @throws ContainerExceptionInterface
37+
* @throws NotFoundExceptionInterface
3438
*/
35-
public function has($key)
39+
public function has(string $key): bool
3640
{
3741
$key = $this->getKeyFromContainer($key);
3842
if (isset($this->cache[$key])) {
@@ -51,9 +55,11 @@ public function has($key)
5155
* @param string $key The object KEY
5256
* @param mixed $default IGNORED IN MEMCACHED.
5357
* @return mixed Description
54-
* @throws \Psr\SimpleCache\InvalidArgumentException
58+
* @throws ContainerExceptionInterface
59+
* @throws InvalidArgumentException
60+
* @throws NotFoundExceptionInterface
5561
*/
56-
public function get($key, $default = null)
62+
public function get(string $key, mixed $default = null): mixed
5763
{
5864
if ($this->has($key)) {
5965
$key = $this->getKeyFromContainer($key);
@@ -78,7 +84,7 @@ public function get($key, $default = null)
7884
*
7985
* MUST be thrown if the $key string is not a legal value.
8086
*/
81-
public function set($key, $value, $ttl = null)
87+
public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
8288
{
8389
$key = $this->getKeyFromContainer($key);
8490

@@ -92,9 +98,10 @@ public function set($key, $value, $ttl = null)
9298
return true;
9399
}
94100

95-
public function clear()
101+
public function clear(): bool
96102
{
97103
$this->cache = [];
104+
return true;
98105
}
99106

100107
/**
@@ -103,7 +110,7 @@ public function clear()
103110
* @param string $key
104111
* @return bool
105112
*/
106-
public function delete($key)
113+
public function delete(string $key): bool
107114
{
108115
$key = $this->getKeyFromContainer($key);
109116

@@ -112,7 +119,7 @@ public function delete($key)
112119
return true;
113120
}
114121

115-
public function isAvailable()
122+
public function isAvailable(): bool
116123
{
117124
return true;
118125
}

src/Psr16/BaseCacheEngine.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@
66
use ByJG\Cache\Exception\InvalidArgumentException;
77
use DateInterval;
88
use DateTime;
9+
use Psr\Container\ContainerExceptionInterface;
910
use Psr\Container\ContainerInterface;
11+
use Psr\Container\NotFoundExceptionInterface;
1012
use Psr\SimpleCache\CacheInterface;
1113

1214
abstract class BaseCacheEngine implements CacheInterface, CacheAvailabilityInterface
1315
{
1416
protected ?ContainerInterface $container;
1517

1618
/**
17-
* @param $keys
19+
* @param string|iterable $keys
1820
* @param null $default
19-
* @return array|iterable
21+
* @return iterable
2022
* @throws \Psr\SimpleCache\InvalidArgumentException
2123
*/
22-
public function getMultiple($keys, $default = null)
24+
public function getMultiple(string|iterable $keys, mixed $default = null): iterable
2325
{
24-
if (!is_array($keys)) {
25-
throw new InvalidArgumentException('getMultipleKeys expected an array');
26+
if (is_string($keys)) {
27+
$keys = [$keys];
2628
}
29+
2730
$result = [];
2831
foreach ($keys as $key) {
2932
$result[$key] = $this->get($key, $default);
@@ -34,31 +37,33 @@ public function getMultiple($keys, $default = null)
3437
/**
3538
* @param iterable $values
3639
* @param null $ttl
37-
* @return bool|void
40+
* @return bool
3841
* @throws \Psr\SimpleCache\InvalidArgumentException
3942
*/
40-
public function setMultiple($values, $ttl = null)
43+
public function setMultiple(iterable $values, $ttl = null): bool
4144
{
4245
foreach ($values as $key => $value) {
4346
$this->set($key, $value, $ttl);
4447
}
48+
return true;
4549
}
4650

4751
/**
4852
* @param iterable $keys
49-
* @return bool|void
53+
* @return bool
5054
* @throws \Psr\SimpleCache\InvalidArgumentException
5155
*/
52-
public function deleteMultiple($keys)
56+
public function deleteMultiple(iterable $keys): bool
5357
{
5458
foreach ($keys as $key) {
5559
$this->delete($key);
5660
}
61+
return true;
5762
}
5863

59-
abstract public function isAvailable();
64+
abstract public function isAvailable(): bool;
6065

61-
protected function addToNow($ttl)
66+
protected function addToNow(DateInterval|int|null $ttl): int|null
6267
{
6368
if (is_numeric($ttl)) {
6469
return strtotime("+$ttl second");
@@ -73,7 +78,10 @@ protected function addToNow($ttl)
7378
return null;
7479
}
7580

76-
protected function convertToSeconds($ttl)
81+
/**
82+
* @throws InvalidArgumentException
83+
*/
84+
protected function convertToSeconds(DateInterval|int $ttl)
7785
{
7886
if (empty($ttl) || is_numeric($ttl)) {
7987
return $ttl;
@@ -87,7 +95,12 @@ protected function convertToSeconds($ttl)
8795
}
8896

8997

90-
protected function getKeyFromContainer($key)
98+
/**
99+
* @throws ContainerExceptionInterface
100+
* @throws InvalidArgumentException
101+
* @throws NotFoundExceptionInterface
102+
*/
103+
protected function getKeyFromContainer(string $key): mixed
91104
{
92105
if (empty($this->container)) {
93106
return $key;

src/Psr16/FileSystemCacheEngine.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct($prefix = 'cache', $path = null, $logger = null)
3232
* @return mixed Description
3333
* @throws \Psr\SimpleCache\InvalidArgumentException
3434
*/
35-
public function get($key, $default = null)
35+
public function get(string $key, mixed $default = null): mixed
3636
{
3737
// Check if file is Locked
3838
$fileKey = $this->fixKey($key);
@@ -78,7 +78,7 @@ public function get($key, $default = null)
7878
*
7979
* MUST be thrown if the $key string is not a legal value.
8080
*/
81-
public function set($key, $value, $ttl = null)
81+
public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
8282
{
8383
$fileKey = $this->fixKey($key);
8484

@@ -119,7 +119,7 @@ public function set($key, $value, $ttl = null)
119119
* @return bool
120120
* @throws \Psr\SimpleCache\InvalidArgumentException
121121
*/
122-
public function delete($key)
122+
public function delete(string $key): bool
123123
{
124124
$this->set($key, null);
125125
return true;
@@ -129,7 +129,7 @@ public function delete($key)
129129
* Lock resource before set it.
130130
* @param string $key
131131
*/
132-
public function lock($key)
132+
public function lock(string $key): void
133133
{
134134
$this->logger->info("[Filesystem cache] Lock '$key'");
135135

@@ -146,7 +146,7 @@ public function lock($key)
146146
* UnLock resource after set it.
147147
* @param string $key
148148
*/
149-
public function unlock($key)
149+
public function unlock($key): void
150150
{
151151

152152
$this->logger->info("[Filesystem cache] Unlock '$key'");
@@ -158,7 +158,7 @@ public function unlock($key)
158158
}
159159
}
160160

161-
public function isAvailable()
161+
public function isAvailable(): bool
162162
{
163163
return is_writable(dirname($this->fixKey('test')));
164164
}
@@ -178,7 +178,7 @@ protected function fixKey($key)
178178
*
179179
* @return bool True on success and false on failure.
180180
*/
181-
public function clear()
181+
public function clear(): bool
182182
{
183183
$patternKey = $this->fixKey('*');
184184
$list = glob($patternKey);
@@ -200,7 +200,7 @@ public function clear()
200200
* @throws \Psr\SimpleCache\InvalidArgumentException
201201
* MUST be thrown if the $key string is not a legal value.
202202
*/
203-
public function has($key)
203+
public function has(string $key): bool
204204
{
205205
$fileKey = $this->fixKey($key);
206206
if (file_exists($fileKey)) {

0 commit comments

Comments
 (0)