Skip to content

Commit d6c7fd6

Browse files
committed
refactor(cache): add native return types to all methods in CacheInterface
+ remove deprecated `false` type in `getMetaData()` method
1 parent 48a1c3a commit d6c7fd6

File tree

11 files changed

+91
-100
lines changed

11 files changed

+91
-100
lines changed

system/Cache/CacheInterface.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ public function initialize();
2626
* Attempts to fetch an item from the cache store.
2727
*
2828
* @param string $key Cache item name
29-
*
30-
* @return mixed
3129
*/
32-
public function get(string $key);
30+
public function get(string $key): mixed;
3331

3432
/**
3533
* Saves an item to the cache store.
@@ -40,7 +38,7 @@ public function get(string $key);
4038
*
4139
* @return bool Success or failure
4240
*/
43-
public function save(string $key, $value, int $ttl = 60);
41+
public function save(string $key, $value, int $ttl = 60): bool;
4442

4543
/**
4644
* Deletes a specific item from the cache store.
@@ -49,7 +47,7 @@ public function save(string $key, $value, int $ttl = 60);
4947
*
5048
* @return bool Success or failure
5149
*/
52-
public function delete(string $key);
50+
public function delete(string $key): bool;
5351

5452
/**
5553
* Deletes items from the cache store matching a given pattern.
@@ -65,48 +63,43 @@ public function deleteMatching(string $pattern): int;
6563
*
6664
* @param string $key Cache ID
6765
* @param int $offset Step/value to increase by
68-
*
69-
* @return bool|int
7066
*/
71-
public function increment(string $key, int $offset = 1);
67+
public function increment(string $key, int $offset = 1): bool|int;
7268

7369
/**
7470
* Performs atomic decrementation of a raw stored value.
7571
*
7672
* @param string $key Cache ID
7773
* @param int $offset Step/value to increase by
78-
*
79-
* @return bool|int
8074
*/
81-
public function decrement(string $key, int $offset = 1);
75+
public function decrement(string $key, int $offset = 1): bool|int;
8276

8377
/**
8478
* Will delete all items in the entire cache.
8579
*
8680
* @return bool Success or failure
8781
*/
88-
public function clean();
82+
public function clean(): bool;
8983

9084
/**
9185
* Returns information on the entire cache.
9286
*
9387
* The information returned and the structure of the data
9488
* varies depending on the handler.
9589
*
96-
* @return array<array-key, mixed>|false|object|null
90+
* @return array<array-key, mixed>|object|false|null
9791
*/
98-
public function getCacheInfo();
92+
public function getCacheInfo(): array|object|false|null;
9993

10094
/**
10195
* Returns detailed information about the specific item in the cache.
10296
*
10397
* @param string $key Cache item name.
10498
*
105-
* @return array<string, mixed>|false|null Returns null if the item does not exist, otherwise array<string, mixed>
99+
* @return array<string, mixed>|null Returns null if the item does not exist, otherwise array<string, mixed>
106100
* with at least the 'expire' key for absolute epoch expiry (or null).
107-
* Some handlers may return false when an item does not exist, which is deprecated.
108101
*/
109-
public function getMetaData(string $key);
102+
public function getMetaData(string $key): array|null;
110103

111104
/**
112105
* Determines if the driver is supported on this system.

system/Cache/Handlers/BaseHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@ public static function validateKey($key, $prefix = ''): string
8181
* @param string $key Cache item name
8282
* @param int $ttl Time to live
8383
* @param Closure(): mixed $callback Callback return value
84-
*
85-
* @return mixed
8684
*/
87-
public function remember(string $key, int $ttl, Closure $callback)
85+
public function remember(string $key, int $ttl, Closure $callback): mixed
8886
{
8987
$value = $this->get($key);
9088

system/Cache/Handlers/DummyHandler.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,31 @@ public function initialize()
3232
/**
3333
* {@inheritDoc}
3434
*/
35-
public function get(string $key)
35+
public function get(string $key): mixed
3636
{
3737
return null;
3838
}
3939

4040
/**
4141
* {@inheritDoc}
4242
*/
43-
public function remember(string $key, int $ttl, Closure $callback)
43+
public function remember(string $key, int $ttl, Closure $callback): mixed
4444
{
4545
return null;
4646
}
4747

4848
/**
4949
* {@inheritDoc}
5050
*/
51-
public function save(string $key, $value, int $ttl = 60)
51+
public function save(string $key, $value, int $ttl = 60): bool
5252
{
5353
return true;
5454
}
5555

5656
/**
5757
* {@inheritDoc}
5858
*/
59-
public function delete(string $key)
59+
public function delete(string $key): bool
6060
{
6161
return true;
6262
}
@@ -72,39 +72,39 @@ public function deleteMatching(string $pattern): int
7272
/**
7373
* {@inheritDoc}
7474
*/
75-
public function increment(string $key, int $offset = 1)
75+
public function increment(string $key, int $offset = 1): bool
7676
{
7777
return true;
7878
}
7979

8080
/**
8181
* {@inheritDoc}
8282
*/
83-
public function decrement(string $key, int $offset = 1)
83+
public function decrement(string $key, int $offset = 1): bool
8484
{
8585
return true;
8686
}
8787

8888
/**
8989
* {@inheritDoc}
9090
*/
91-
public function clean()
91+
public function clean(): bool
9292
{
9393
return true;
9494
}
9595

9696
/**
9797
* {@inheritDoc}
9898
*/
99-
public function getCacheInfo()
99+
public function getCacheInfo(): array|null
100100
{
101101
return null;
102102
}
103103

104104
/**
105105
* {@inheritDoc}
106106
*/
107-
public function getMetaData(string $key)
107+
public function getMetaData(string $key): array|null
108108
{
109109
return null;
110110
}

system/Cache/Handlers/FileHandler.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function initialize()
8282
/**
8383
* {@inheritDoc}
8484
*/
85-
public function get(string $key)
85+
public function get(string $key): mixed
8686
{
8787
$key = static::validateKey($key, $this->prefix);
8888
$data = $this->getItem($key);
@@ -93,7 +93,7 @@ public function get(string $key)
9393
/**
9494
* {@inheritDoc}
9595
*/
96-
public function save(string $key, $value, int $ttl = 60)
96+
public function save(string $key, $value, int $ttl = 60): bool
9797
{
9898
$key = static::validateKey($key, $this->prefix);
9999

@@ -122,7 +122,7 @@ public function save(string $key, $value, int $ttl = 60)
122122
/**
123123
* {@inheritDoc}
124124
*/
125-
public function delete(string $key)
125+
public function delete(string $key): bool
126126
{
127127
$key = static::validateKey($key, $this->prefix);
128128

@@ -148,7 +148,7 @@ public function deleteMatching(string $pattern): int
148148
/**
149149
* {@inheritDoc}
150150
*/
151-
public function increment(string $key, int $offset = 1)
151+
public function increment(string $key, int $offset = 1): bool|int
152152
{
153153
$prefixedKey = static::validateKey($key, $this->prefix);
154154
$tmp = $this->getItem($prefixedKey);
@@ -171,36 +171,36 @@ public function increment(string $key, int $offset = 1)
171171
/**
172172
* {@inheritDoc}
173173
*/
174-
public function decrement(string $key, int $offset = 1)
174+
public function decrement(string $key, int $offset = 1): bool|int
175175
{
176176
return $this->increment($key, -$offset);
177177
}
178178

179179
/**
180180
* {@inheritDoc}
181181
*/
182-
public function clean()
182+
public function clean(): bool
183183
{
184184
return delete_files($this->path, false, true);
185185
}
186186

187187
/**
188188
* {@inheritDoc}
189189
*/
190-
public function getCacheInfo()
190+
public function getCacheInfo(): array
191191
{
192192
return get_dir_file_info($this->path);
193193
}
194194

195195
/**
196196
* {@inheritDoc}
197197
*/
198-
public function getMetaData(string $key)
198+
public function getMetaData(string $key): array|null
199199
{
200200
$key = static::validateKey($key, $this->prefix);
201201

202202
if (false === $data = $this->getItem($key)) {
203-
return false; // @TODO This will return null in a future release
203+
return null;
204204
}
205205

206206
return [
@@ -224,7 +224,7 @@ public function isSupported(): bool
224224
*
225225
* @return array{data: mixed, ttl: int, time: int}|false
226226
*/
227-
protected function getItem(string $filename)
227+
protected function getItem(string $filename): array|false
228228
{
229229
if (! is_file($this->path . $filename)) {
230230
return false;
@@ -271,10 +271,8 @@ protected function getItem(string $filename)
271271
* @param string $path
272272
* @param string $data
273273
* @param string $mode
274-
*
275-
* @return bool
276274
*/
277-
protected function writeFile($path, $data, $mode = 'wb')
275+
protected function writeFile($path, $data, $mode = 'wb'): bool
278276
{
279277
if (($fp = @fopen($path, $mode)) === false) {
280278
return false;
@@ -353,7 +351,7 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs
353351
* relative_path: string,
354352
* }>|false
355353
*/
356-
protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false)
354+
protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false): array|false
357355
{
358356
static $filedata = [];
359357

@@ -412,7 +410,7 @@ protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true,
412410
* fileperms?: int
413411
* }|false
414412
*/
415-
protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date'])
413+
protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date']): array|false
416414
{
417415
if (! is_file($file)) {
418416
return false;

system/Cache/Handlers/MemcachedHandler.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function initialize()
119119
/**
120120
* {@inheritDoc}
121121
*/
122-
public function get(string $key)
122+
public function get(string $key): mixed
123123
{
124124
$data = [];
125125
$key = static::validateKey($key, $this->prefix);
@@ -147,7 +147,7 @@ public function get(string $key)
147147
/**
148148
* {@inheritDoc}
149149
*/
150-
public function save(string $key, $value, int $ttl = 60)
150+
public function save(string $key, $value, int $ttl = 60): bool
151151
{
152152
$key = static::validateKey($key, $this->prefix);
153153

@@ -173,7 +173,7 @@ public function save(string $key, $value, int $ttl = 60)
173173
/**
174174
* {@inheritDoc}
175175
*/
176-
public function delete(string $key)
176+
public function delete(string $key): bool
177177
{
178178
$key = static::validateKey($key, $this->prefix);
179179

@@ -191,7 +191,7 @@ public function deleteMatching(string $pattern): never
191191
/**
192192
* {@inheritDoc}
193193
*/
194-
public function increment(string $key, int $offset = 1)
194+
public function increment(string $key, int $offset = 1): int|false
195195
{
196196
if (! $this->config['raw']) {
197197
return false;
@@ -205,7 +205,7 @@ public function increment(string $key, int $offset = 1)
205205
/**
206206
* {@inheritDoc}
207207
*/
208-
public function decrement(string $key, int $offset = 1)
208+
public function decrement(string $key, int $offset = 1): int|false
209209
{
210210
if (! $this->config['raw']) {
211211
return false;
@@ -221,30 +221,30 @@ public function decrement(string $key, int $offset = 1)
221221
/**
222222
* {@inheritDoc}
223223
*/
224-
public function clean()
224+
public function clean(): bool
225225
{
226226
return $this->memcached->flush();
227227
}
228228

229229
/**
230230
* {@inheritDoc}
231231
*/
232-
public function getCacheInfo()
232+
public function getCacheInfo(): array|false
233233
{
234234
return $this->memcached->getStats();
235235
}
236236

237237
/**
238238
* {@inheritDoc}
239239
*/
240-
public function getMetaData(string $key)
240+
public function getMetaData(string $key): array|null
241241
{
242242
$key = static::validateKey($key, $this->prefix);
243243
$stored = $this->memcached->get($key);
244244

245245
// if not an array, don't try to count for PHP7.2
246246
if (! is_array($stored) || count($stored) !== 3) {
247-
return false; // @TODO This will return null in a future release
247+
return null;
248248
}
249249

250250
[$data, $time, $limit] = $stored;

0 commit comments

Comments
 (0)