Skip to content

Commit 76bea16

Browse files
feat(cache): add native types to all CacheInterface methods (#9811)
* feat(cache): add native return types to all CacheInterface methods + remove deprecated `false` type in `getMetaData()` method + remove unnecessary @inheritdoc annotation * docs(changelogs): add note for WincacheHandler bug fix + clarify deprecation removal of `false` type for `CacheInterface::getMetaData()` * feat(cache): set native type for $value param in CacheInterface::save() method * docs(changelogs): update method signature changes for CacheInterface
1 parent 43bd5bb commit 76bea16

File tree

13 files changed

+103
-315
lines changed

13 files changed

+103
-315
lines changed

system/Cache/CacheInterface.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@ interface CacheInterface
1717
{
1818
/**
1919
* Takes care of any handler-specific setup that must be done.
20-
*
21-
* @return void
2220
*/
23-
public function initialize();
21+
public function initialize(): void;
2422

2523
/**
2624
* Attempts to fetch an item from the cache store.
2725
*
2826
* @param string $key Cache item name
29-
*
30-
* @return mixed
3127
*/
32-
public function get(string $key);
28+
public function get(string $key): mixed;
3329

3430
/**
3531
* Saves an item to the cache store.
@@ -40,7 +36,7 @@ public function get(string $key);
4036
*
4137
* @return bool Success or failure
4238
*/
43-
public function save(string $key, $value, int $ttl = 60);
39+
public function save(string $key, mixed $value, int $ttl = 60): bool;
4440

4541
/**
4642
* Deletes a specific item from the cache store.
@@ -49,7 +45,7 @@ public function save(string $key, $value, int $ttl = 60);
4945
*
5046
* @return bool Success or failure
5147
*/
52-
public function delete(string $key);
48+
public function delete(string $key): bool;
5349

5450
/**
5551
* Deletes items from the cache store matching a given pattern.
@@ -65,27 +61,23 @@ public function deleteMatching(string $pattern): int;
6561
*
6662
* @param string $key Cache ID
6763
* @param int $offset Step/value to increase by
68-
*
69-
* @return bool|int
7064
*/
71-
public function increment(string $key, int $offset = 1);
65+
public function increment(string $key, int $offset = 1): bool|int;
7266

7367
/**
7468
* Performs atomic decrementation of a raw stored value.
7569
*
7670
* @param string $key Cache ID
7771
* @param int $offset Step/value to increase by
78-
*
79-
* @return bool|int
8072
*/
81-
public function decrement(string $key, int $offset = 1);
73+
public function decrement(string $key, int $offset = 1): bool|int;
8274

8375
/**
8476
* Will delete all items in the entire cache.
8577
*
8678
* @return bool Success or failure
8779
*/
88-
public function clean();
80+
public function clean(): bool;
8981

9082
/**
9183
* Returns information on the entire cache.
@@ -95,18 +87,17 @@ public function clean();
9587
*
9688
* @return array<array-key, mixed>|false|object|null
9789
*/
98-
public function getCacheInfo();
90+
public function getCacheInfo(): array|false|object|null;
9991

10092
/**
10193
* Returns detailed information about the specific item in the cache.
10294
*
10395
* @param string $key Cache item name.
10496
*
105-
* @return array<string, mixed>|false|null Returns null if the item does not exist, otherwise array<string, mixed>
106-
* 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.
97+
* @return array<string, mixed>|null Returns null if the item does not exist, otherwise array<string, mixed>
98+
* with at least the 'expire' key for absolute epoch expiry (or null).
10899
*/
109-
public function getMetaData(string $key);
100+
public function getMetaData(string $key): ?array;
110101

111102
/**
112103
* 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: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,96 +22,60 @@
2222
*/
2323
class DummyHandler extends BaseHandler
2424
{
25-
/**
26-
* {@inheritDoc}
27-
*/
28-
public function initialize()
25+
public function initialize(): void
2926
{
3027
}
3128

32-
/**
33-
* {@inheritDoc}
34-
*/
35-
public function get(string $key)
29+
public function get(string $key): mixed
3630
{
3731
return null;
3832
}
3933

40-
/**
41-
* {@inheritDoc}
42-
*/
43-
public function remember(string $key, int $ttl, Closure $callback)
34+
public function remember(string $key, int $ttl, Closure $callback): mixed
4435
{
4536
return null;
4637
}
4738

48-
/**
49-
* {@inheritDoc}
50-
*/
51-
public function save(string $key, $value, int $ttl = 60)
39+
public function save(string $key, mixed $value, int $ttl = 60): bool
5240
{
5341
return true;
5442
}
5543

56-
/**
57-
* {@inheritDoc}
58-
*/
59-
public function delete(string $key)
44+
public function delete(string $key): bool
6045
{
6146
return true;
6247
}
6348

64-
/**
65-
* {@inheritDoc}
66-
*/
6749
public function deleteMatching(string $pattern): int
6850
{
6951
return 0;
7052
}
7153

72-
/**
73-
* {@inheritDoc}
74-
*/
75-
public function increment(string $key, int $offset = 1)
54+
public function increment(string $key, int $offset = 1): bool
7655
{
7756
return true;
7857
}
7958

80-
/**
81-
* {@inheritDoc}
82-
*/
83-
public function decrement(string $key, int $offset = 1)
59+
public function decrement(string $key, int $offset = 1): bool
8460
{
8561
return true;
8662
}
8763

88-
/**
89-
* {@inheritDoc}
90-
*/
91-
public function clean()
64+
public function clean(): bool
9265
{
9366
return true;
9467
}
9568

96-
/**
97-
* {@inheritDoc}
98-
*/
99-
public function getCacheInfo()
69+
public function getCacheInfo(): ?array
10070
{
10171
return null;
10272
}
10373

104-
/**
105-
* {@inheritDoc}
106-
*/
107-
public function getMetaData(string $key)
74+
public function getMetaData(string $key): ?array
10875
{
10976
return null;
11077
}
11178

112-
/**
113-
* {@inheritDoc}
114-
*/
11579
public function isSupported(): bool
11680
{
11781
return true;

system/Cache/Handlers/FileHandler.php

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,19 @@ public function __construct(Cache $config)
7272
helper('filesystem');
7373
}
7474

75-
/**
76-
* {@inheritDoc}
77-
*/
78-
public function initialize()
75+
public function initialize(): void
7976
{
8077
}
8178

82-
/**
83-
* {@inheritDoc}
84-
*/
85-
public function get(string $key)
79+
public function get(string $key): mixed
8680
{
8781
$key = static::validateKey($key, $this->prefix);
8882
$data = $this->getItem($key);
8983

9084
return is_array($data) ? $data['data'] : null;
9185
}
9286

93-
/**
94-
* {@inheritDoc}
95-
*/
96-
public function save(string $key, $value, int $ttl = 60)
87+
public function save(string $key, mixed $value, int $ttl = 60): bool
9788
{
9889
$key = static::validateKey($key, $this->prefix);
9990

@@ -119,19 +110,13 @@ public function save(string $key, $value, int $ttl = 60)
119110
return false;
120111
}
121112

122-
/**
123-
* {@inheritDoc}
124-
*/
125-
public function delete(string $key)
113+
public function delete(string $key): bool
126114
{
127115
$key = static::validateKey($key, $this->prefix);
128116

129117
return is_file($this->path . $key) && unlink($this->path . $key);
130118
}
131119

132-
/**
133-
* {@inheritDoc}
134-
*/
135120
public function deleteMatching(string $pattern): int
136121
{
137122
$deleted = 0;
@@ -145,10 +130,7 @@ public function deleteMatching(string $pattern): int
145130
return $deleted;
146131
}
147132

148-
/**
149-
* {@inheritDoc}
150-
*/
151-
public function increment(string $key, int $offset = 1)
133+
public function increment(string $key, int $offset = 1): bool|int
152134
{
153135
$prefixedKey = static::validateKey($key, $this->prefix);
154136
$tmp = $this->getItem($prefixedKey);
@@ -168,39 +150,27 @@ public function increment(string $key, int $offset = 1)
168150
return $this->save($key, $value, $ttl) ? $value : false;
169151
}
170152

171-
/**
172-
* {@inheritDoc}
173-
*/
174-
public function decrement(string $key, int $offset = 1)
153+
public function decrement(string $key, int $offset = 1): bool|int
175154
{
176155
return $this->increment($key, -$offset);
177156
}
178157

179-
/**
180-
* {@inheritDoc}
181-
*/
182-
public function clean()
158+
public function clean(): bool
183159
{
184160
return delete_files($this->path, false, true);
185161
}
186162

187-
/**
188-
* {@inheritDoc}
189-
*/
190-
public function getCacheInfo()
163+
public function getCacheInfo(): array
191164
{
192165
return get_dir_file_info($this->path);
193166
}
194167

195-
/**
196-
* {@inheritDoc}
197-
*/
198-
public function getMetaData(string $key)
168+
public function getMetaData(string $key): ?array
199169
{
200170
$key = static::validateKey($key, $this->prefix);
201171

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

206176
return [
@@ -210,9 +180,6 @@ public function getMetaData(string $key)
210180
];
211181
}
212182

213-
/**
214-
* {@inheritDoc}
215-
*/
216183
public function isSupported(): bool
217184
{
218185
return is_writable($this->path);
@@ -224,7 +191,7 @@ public function isSupported(): bool
224191
*
225192
* @return array{data: mixed, ttl: int, time: int}|false
226193
*/
227-
protected function getItem(string $filename)
194+
protected function getItem(string $filename): array|false
228195
{
229196
if (! is_file($this->path . $filename)) {
230197
return false;
@@ -271,10 +238,8 @@ protected function getItem(string $filename)
271238
* @param string $path
272239
* @param string $data
273240
* @param string $mode
274-
*
275-
* @return bool
276241
*/
277-
protected function writeFile($path, $data, $mode = 'wb')
242+
protected function writeFile($path, $data, $mode = 'wb'): bool
278243
{
279244
if (($fp = @fopen($path, $mode)) === false) {
280245
return false;
@@ -353,7 +318,7 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs
353318
* relative_path: string,
354319
* }>|false
355320
*/
356-
protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false)
321+
protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false): array|false
357322
{
358323
static $filedata = [];
359324

@@ -412,7 +377,7 @@ protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true,
412377
* fileperms?: int
413378
* }|false
414379
*/
415-
protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date'])
380+
protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date']): array|false
416381
{
417382
if (! is_file($file)) {
418383
return false;

0 commit comments

Comments
 (0)