Skip to content

Commit dd80109

Browse files
committed
Add Types
1 parent cf632b7 commit dd80109

20 files changed

+267
-106
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
github: byjg

.github/workflows/phpunit.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
- uses: actions/checkout@v4
3737
- run: composer install
3838
- run: ./vendor/bin/phpunit --stderr
39+
- run: ./vendor/bin/psalm
3940

4041
Documentation:
4142
if: github.ref == 'refs/heads/master'

.run/PHPUnit.run.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="PHPUnit" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
3+
<TestRunner bootstrap_file="$PROJECT_DIR$/phpunit.xml.dist" configuration_file="$PROJECT_DIR$/phpunit.xml.dist" directory="$PROJECT_DIR$" scope="XML" options="--stderr" use_alternative_configuration_file="true" />
4+
<method v="2" />
5+
</configuration>
6+
</component>

.run/PSalm.run.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="PSalm" type="PhpLocalRunConfigurationType" factoryName="PHP Console" path="$PROJECT_DIR$/vendor/bin/psalm">
3+
<method v="2" />
4+
</configuration>
5+
</component>

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
"ByJG\\Cache\\": "src/"
77
}
88
},
9+
"autoload-dev": {
10+
"psr-4": {
11+
"Tests\\": "tests/"
12+
}
13+
},
914
"require": {
1015
"php": ">=8.1",
1116
"psr/cache": "^1.0|^2.0|^3.0",
@@ -15,7 +20,7 @@
1520
},
1621
"require-dev": {
1722
"phpunit/phpunit": "^9.6",
18-
"vimeo/psalm": "^6.0"
23+
"vimeo/psalm": "^5.9"
1924
},
2025
"suggest": {
2126
"ext-memcached": "*",

psalm.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="4"
4+
resolveFromConfigFile="true"
5+
findUnusedBaselineEntry="true"
6+
findUnusedCode="false"
7+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8+
xmlns="https://getpsalm.org/schema/config"
9+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
10+
>
11+
<projectFiles>
12+
<directory name="src" />
13+
<directory name="tests" />
14+
<ignoreFiles>
15+
<directory name="vendor" />
16+
</ignoreFiles>
17+
</projectFiles>
18+
</psalm>

src/Psr16/ArrayCacheEngine.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
use DateInterval;
77
use Psr\Container\ContainerExceptionInterface;
88
use Psr\Container\NotFoundExceptionInterface;
9+
use Psr\Log\LoggerInterface;
910
use Psr\Log\NullLogger;
1011

1112
class ArrayCacheEngine extends BaseCacheEngine
1213
{
1314

14-
protected $cache = array();
15+
protected array $cache = [];
1516

16-
protected $logger = null;
17+
protected LoggerInterface|null $logger = null;
1718

18-
public function __construct($logger = null)
19+
public function __construct(LoggerInterface|null $logger = null)
1920
{
2021
$this->logger = $logger;
2122
if (is_null($logger)) {

src/Psr16/BaseCacheEngine.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ abstract class BaseCacheEngine implements CacheInterface, CacheAvailabilityInter
1717

1818
/**
1919
* @param string|iterable $keys
20-
* @param null $default
21-
* @return iterable
20+
* @param mixed $default
21+
* @return iterable<string, mixed>
2222
* @throws \Psr\SimpleCache\InvalidArgumentException
2323
*/
2424
public function getMultiple(string|iterable $keys, mixed $default = null): iterable
@@ -36,11 +36,11 @@ public function getMultiple(string|iterable $keys, mixed $default = null): itera
3636

3737
/**
3838
* @param iterable $values
39-
* @param null $ttl
39+
* @param DateInterval|int|null $ttl
4040
* @return bool
4141
* @throws \Psr\SimpleCache\InvalidArgumentException
4242
*/
43-
public function setMultiple(iterable $values, $ttl = null): bool
43+
public function setMultiple(iterable $values, DateInterval|int|null $ttl = null): bool
4444
{
4545
foreach ($values as $key => $value) {
4646
$this->set($key, $value, $ttl);
@@ -87,11 +87,7 @@ protected function convertToSeconds(DateInterval|int|null $ttl): DateInterval|in
8787
return $ttl;
8888
}
8989

90-
if ($ttl instanceof DateInterval) {
91-
return $ttl->days*86400 + $ttl->h*3600 + $ttl->i*60 + $ttl->s;
92-
}
93-
94-
throw new InvalidArgumentException('Invalid TTL');
90+
return $ttl->days*86400 + $ttl->h*3600 + $ttl->i*60 + $ttl->s;
9591
}
9692

9793

src/Psr16/FileSystemCacheEngine.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
use ByJG\Cache\CacheLockInterface;
66
use DateInterval;
77
use Exception;
8+
use Psr\Container\ContainerExceptionInterface;
9+
use Psr\Container\NotFoundExceptionInterface;
10+
use Psr\Log\LoggerInterface;
811
use Psr\Log\NullLogger;
12+
use Psr\SimpleCache\InvalidArgumentException;
913

1014
class FileSystemCacheEngine extends BaseCacheEngine implements CacheLockInterface
1115
{
1216

13-
protected $logger = null;
17+
protected ?LoggerInterface $logger = null;
1418

15-
protected $prefix = null;
16-
protected $path = null;
19+
protected ?string $prefix = null;
20+
protected ?string $path = null;
1721

18-
public function __construct($prefix = 'cache', $path = null, $logger = null)
22+
public function __construct(string $prefix = 'cache', ?string $path = null, ?LoggerInterface $logger = null)
1923
{
2024
$this->prefix = $prefix;
2125
$this->path = $path ?? sys_get_temp_dir();
@@ -30,7 +34,9 @@ public function __construct($prefix = 'cache', $path = null, $logger = null)
3034
* @param string $key The object KEY
3135
* @param mixed $default IGNORED IN MEMCACHED.
3236
* @return mixed Description
33-
* @throws \Psr\SimpleCache\InvalidArgumentException
37+
* @throws ContainerExceptionInterface
38+
* @throws NotFoundExceptionInterface
39+
* @throws \ByJG\Cache\Exception\InvalidArgumentException
3440
*/
3541
public function get(string $key, mixed $default = null): mixed
3642
{
@@ -104,7 +110,7 @@ public function set(string $key, mixed $value, DateInterval|int|null $ttl = null
104110

105111
$validUntil = $this->addToNow($ttl);
106112
if (!empty($validUntil)) {
107-
file_put_contents($fileKey . ".ttl", $validUntil);
113+
file_put_contents($fileKey . ".ttl", (string)$validUntil);
108114
}
109115
} catch (Exception $ex) {
110116
$this->logger->warning("[Filesystem cache] I could not write to cache on file '" . basename($key) . "'. Switching to nocache=true mode.");
@@ -117,7 +123,6 @@ public function set(string $key, mixed $value, DateInterval|int|null $ttl = null
117123
/**
118124
* @param string $key
119125
* @return bool
120-
* @throws \Psr\SimpleCache\InvalidArgumentException
121126
*/
122127
public function delete(string $key): bool
123128
{
@@ -146,7 +151,7 @@ public function lock(string $key): void
146151
* UnLock resource after set it.
147152
* @param string $key
148153
*/
149-
public function unlock($key): void
154+
public function unlock(string $key): void
150155
{
151156

152157
$this->logger->info("[Filesystem cache] Unlock '$key'");
@@ -158,12 +163,22 @@ public function unlock($key): void
158163
}
159164
}
160165

166+
/**
167+
* @throws ContainerExceptionInterface
168+
* @throws NotFoundExceptionInterface
169+
* @throws \ByJG\Cache\Exception\InvalidArgumentException
170+
*/
161171
public function isAvailable(): bool
162172
{
163173
return is_writable(dirname($this->fixKey('test')));
164174
}
165175

166-
protected function fixKey($key)
176+
/**
177+
* @throws ContainerExceptionInterface
178+
* @throws NotFoundExceptionInterface
179+
* @throws \ByJG\Cache\Exception\InvalidArgumentException
180+
*/
181+
protected function fixKey(string $key): string
167182
{
168183
$key = $this->getKeyFromContainer($key);
169184

@@ -177,6 +192,9 @@ protected function fixKey($key)
177192
* Wipes clean the entire cache's keys.
178193
*
179194
* @return bool True on success and false on failure.
195+
* @throws ContainerExceptionInterface
196+
* @throws NotFoundExceptionInterface
197+
* @throws \ByJG\Cache\Exception\InvalidArgumentException
180198
*/
181199
public function clear(): bool
182200
{
@@ -197,8 +215,9 @@ public function clear(): bool
197215
*
198216
* @param string $key The cache item key.
199217
* @return bool
200-
* @throws \Psr\SimpleCache\InvalidArgumentException
201-
* MUST be thrown if the $key string is not a legal value.
218+
* @throws ContainerExceptionInterface
219+
* @throws NotFoundExceptionInterface
220+
* @throws \ByJG\Cache\Exception\InvalidArgumentException
202221
*/
203222
public function has(string $key): bool
204223
{

src/Psr16/MemcachedEngine.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22

33
namespace ByJG\Cache\Psr16;
44

5+
use ByJG\Cache\Exception\InvalidArgumentException;
56
use ByJG\Cache\Exception\StorageErrorException;
67
use DateInterval;
78
use Memcached;
9+
use Psr\Container\ContainerExceptionInterface;
10+
use Psr\Container\NotFoundExceptionInterface;
11+
use Psr\Log\LoggerInterface;
812
use Psr\Log\NullLogger;
913

1014
class MemcachedEngine extends BaseCacheEngine
1115
{
1216

1317
/**
1418
*
15-
* @var Memcached
19+
* @var Memcached|null
1620
*/
17-
protected $memCached = null;
21+
protected Memcached|null $memCached = null;
1822

19-
protected $logger = null;
23+
protected LoggerInterface|null $logger = null;
2024

21-
protected $servers = null;
25+
protected ?array $servers = null;
2226

23-
public function __construct($servers = null, $logger = null)
27+
public function __construct(?array $servers = null, $logger = null)
2428
{
2529
$this->servers = (array)$servers;
2630
if (is_null($servers)) {
@@ -35,21 +39,27 @@ public function __construct($servers = null, $logger = null)
3539
}
3640
}
3741

38-
protected function fixKey($key) {
42+
/**
43+
* @throws ContainerExceptionInterface
44+
* @throws InvalidArgumentException
45+
* @throws NotFoundExceptionInterface
46+
*/
47+
protected function fixKey(string $key): string
48+
{
3949
$key = $this->getKeyFromContainer($key);
4050
return "cache-" . $key;
4151
}
4252

4353
/**
4454
* @throws StorageErrorException
4555
*/
46-
protected function lazyLoadMemCachedServers()
56+
protected function lazyLoadMemCachedServers(): void
4757
{
4858
if (is_null($this->memCached)) {
4959
$this->memCached = new Memcached();
5060
foreach ($this->servers as $server) {
5161
$data = explode(":", $server);
52-
$this->memCached->addServer($data[0], $data[1]);
62+
$this->memCached->addServer($data[0], intval($data[1]));
5363

5464
$stats = $this->memCached->getStats();
5565
if (!isset($stats[$server]) || $stats[$server]['pid'] === -1) {
@@ -60,9 +70,12 @@ protected function lazyLoadMemCachedServers()
6070
}
6171

6272
/**
63-
* @param string $key The object KEY
64-
* @param int $default IGNORED IN MEMCACHED.
65-
* @return mixed Description
73+
* @param string $key
74+
* @param mixed|null $default
75+
* @return mixed
76+
* @throws ContainerExceptionInterface
77+
* @throws InvalidArgumentException
78+
* @throws NotFoundExceptionInterface
6679
* @throws StorageErrorException
6780
*/
6881
public function get(string $key, mixed $default = null): mixed
@@ -83,6 +96,9 @@ public function get(string $key, mixed $default = null): mixed
8396
* @param mixed $value The object to be cached
8497
* @param DateInterval|int|null $ttl The time to live in seconds of this objects
8598
* @return bool If the object is successfully posted
99+
* @throws ContainerExceptionInterface
100+
* @throws InvalidArgumentException
101+
* @throws NotFoundExceptionInterface
86102
* @throws StorageErrorException
87103
*/
88104
public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool
@@ -103,6 +119,9 @@ public function set(string $key, mixed $value, DateInterval|int|null $ttl = null
103119
/**
104120
* @param string $key
105121
* @return bool
122+
* @throws ContainerExceptionInterface
123+
* @throws InvalidArgumentException
124+
* @throws NotFoundExceptionInterface
106125
* @throws StorageErrorException
107126
*/
108127
public function delete(string $key): bool
@@ -141,6 +160,9 @@ public function clear(): bool
141160
/**
142161
* @param string $key
143162
* @return bool
163+
* @throws ContainerExceptionInterface
164+
* @throws InvalidArgumentException
165+
* @throws NotFoundExceptionInterface
144166
* @throws StorageErrorException
145167
*/
146168
public function has(string $key): bool

0 commit comments

Comments
 (0)