Skip to content

Commit a8ad4f0

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

File tree

13 files changed

+108
-303
lines changed

13 files changed

+108
-303
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, $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: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,96 +22,63 @@
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

4839
/**
49-
* {@inheritDoc}
40+
* @param mixed $value
5041
*/
51-
public function save(string $key, $value, int $ttl = 60)
42+
public function save(string $key, $value, int $ttl = 60): bool
5243
{
5344
return true;
5445
}
5546

56-
/**
57-
* {@inheritDoc}
58-
*/
59-
public function delete(string $key)
47+
public function delete(string $key): bool
6048
{
6149
return true;
6250
}
6351

64-
/**
65-
* {@inheritDoc}
66-
*/
6752
public function deleteMatching(string $pattern): int
6853
{
6954
return 0;
7055
}
7156

72-
/**
73-
* {@inheritDoc}
74-
*/
75-
public function increment(string $key, int $offset = 1)
57+
public function increment(string $key, int $offset = 1): bool
7658
{
7759
return true;
7860
}
7961

80-
/**
81-
* {@inheritDoc}
82-
*/
83-
public function decrement(string $key, int $offset = 1)
62+
public function decrement(string $key, int $offset = 1): bool
8463
{
8564
return true;
8665
}
8766

88-
/**
89-
* {@inheritDoc}
90-
*/
91-
public function clean()
67+
public function clean(): bool
9268
{
9369
return true;
9470
}
9571

96-
/**
97-
* {@inheritDoc}
98-
*/
99-
public function getCacheInfo()
72+
public function getCacheInfo(): ?array
10073
{
10174
return null;
10275
}
10376

104-
/**
105-
* {@inheritDoc}
106-
*/
107-
public function getMetaData(string $key)
77+
public function getMetaData(string $key): ?array
10878
{
10979
return null;
11080
}
11181

112-
/**
113-
* {@inheritDoc}
114-
*/
11582
public function isSupported(): bool
11683
{
11784
return true;

system/Cache/Handlers/FileHandler.php

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,11 @@ 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);
@@ -91,9 +85,9 @@ public function get(string $key)
9185
}
9286

9387
/**
94-
* {@inheritDoc}
88+
* @param mixed $value
9589
*/
96-
public function save(string $key, $value, int $ttl = 60)
90+
public function save(string $key, $value, int $ttl = 60): bool
9791
{
9892
$key = static::validateKey($key, $this->prefix);
9993

@@ -119,19 +113,13 @@ public function save(string $key, $value, int $ttl = 60)
119113
return false;
120114
}
121115

122-
/**
123-
* {@inheritDoc}
124-
*/
125-
public function delete(string $key)
116+
public function delete(string $key): bool
126117
{
127118
$key = static::validateKey($key, $this->prefix);
128119

129120
return is_file($this->path . $key) && unlink($this->path . $key);
130121
}
131122

132-
/**
133-
* {@inheritDoc}
134-
*/
135123
public function deleteMatching(string $pattern): int
136124
{
137125
$deleted = 0;
@@ -145,10 +133,7 @@ public function deleteMatching(string $pattern): int
145133
return $deleted;
146134
}
147135

148-
/**
149-
* {@inheritDoc}
150-
*/
151-
public function increment(string $key, int $offset = 1)
136+
public function increment(string $key, int $offset = 1): bool|int
152137
{
153138
$prefixedKey = static::validateKey($key, $this->prefix);
154139
$tmp = $this->getItem($prefixedKey);
@@ -168,39 +153,27 @@ public function increment(string $key, int $offset = 1)
168153
return $this->save($key, $value, $ttl) ? $value : false;
169154
}
170155

171-
/**
172-
* {@inheritDoc}
173-
*/
174-
public function decrement(string $key, int $offset = 1)
156+
public function decrement(string $key, int $offset = 1): bool|int
175157
{
176158
return $this->increment($key, -$offset);
177159
}
178160

179-
/**
180-
* {@inheritDoc}
181-
*/
182-
public function clean()
161+
public function clean(): bool
183162
{
184163
return delete_files($this->path, false, true);
185164
}
186165

187-
/**
188-
* {@inheritDoc}
189-
*/
190-
public function getCacheInfo()
166+
public function getCacheInfo(): array
191167
{
192168
return get_dir_file_info($this->path);
193169
}
194170

195-
/**
196-
* {@inheritDoc}
197-
*/
198-
public function getMetaData(string $key)
171+
public function getMetaData(string $key): ?array
199172
{
200173
$key = static::validateKey($key, $this->prefix);
201174

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

206179
return [
@@ -210,9 +183,6 @@ public function getMetaData(string $key)
210183
];
211184
}
212185

213-
/**
214-
* {@inheritDoc}
215-
*/
216186
public function isSupported(): bool
217187
{
218188
return is_writable($this->path);
@@ -224,7 +194,7 @@ public function isSupported(): bool
224194
*
225195
* @return array{data: mixed, ttl: int, time: int}|false
226196
*/
227-
protected function getItem(string $filename)
197+
protected function getItem(string $filename): array|false
228198
{
229199
if (! is_file($this->path . $filename)) {
230200
return false;
@@ -271,10 +241,8 @@ protected function getItem(string $filename)
271241
* @param string $path
272242
* @param string $data
273243
* @param string $mode
274-
*
275-
* @return bool
276244
*/
277-
protected function writeFile($path, $data, $mode = 'wb')
245+
protected function writeFile($path, $data, $mode = 'wb'): bool
278246
{
279247
if (($fp = @fopen($path, $mode)) === false) {
280248
return false;
@@ -353,7 +321,7 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs
353321
* relative_path: string,
354322
* }>|false
355323
*/
356-
protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false)
324+
protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false): array|false
357325
{
358326
static $filedata = [];
359327

@@ -412,7 +380,7 @@ protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true,
412380
* fileperms?: int
413381
* }|false
414382
*/
415-
protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date'])
383+
protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date']): array|false
416384
{
417385
if (! is_file($file)) {
418386
return false;

0 commit comments

Comments
 (0)