Skip to content

Commit 5ba189c

Browse files
committed
feat(cache): add native return types to all CacheInterface methods
+ remove deprecated `false` type in `getMetaData()` method + remove unnecessary @inheritdoc annotation
1 parent 2609108 commit 5ba189c

File tree

13 files changed

+114
-321
lines changed

13 files changed

+114
-321
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,34 +45,30 @@ 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
* Performs atomic incrementation of a raw stored value.
5652
*
5753
* @param string $key Cache ID
5854
* @param int $offset Step/value to increase by
59-
*
60-
* @return bool|int
6155
*/
62-
public function increment(string $key, int $offset = 1);
56+
public function increment(string $key, int $offset = 1): bool|int;
6357

6458
/**
6559
* Performs atomic decrementation of a raw stored value.
6660
*
6761
* @param string $key Cache ID
6862
* @param int $offset Step/value to increase by
69-
*
70-
* @return bool|int
7163
*/
72-
public function decrement(string $key, int $offset = 1);
64+
public function decrement(string $key, int $offset = 1): bool|int;
7365

7466
/**
7567
* Will delete all items in the entire cache.
7668
*
7769
* @return bool Success or failure
7870
*/
79-
public function clean();
71+
public function clean(): bool;
8072

8173
/**
8274
* Returns information on the entire cache.
@@ -86,18 +78,17 @@ public function clean();
8678
*
8779
* @return array<array-key, mixed>|false|object|null
8880
*/
89-
public function getCacheInfo();
81+
public function getCacheInfo(): array|false|object|null;
9082

9183
/**
9284
* Returns detailed information about the specific item in the cache.
9385
*
9486
* @param string $key Cache item name.
9587
*
96-
* @return array<string, mixed>|false|null Returns null if the item does not exist, otherwise array<string, mixed>
97-
* with at least the 'expire' key for absolute epoch expiry (or null).
98-
* Some handlers may return false when an item does not exist, which is deprecated.
88+
* @return array<string, mixed>|null Returns null if the item does not exist, otherwise array<string, mixed>
89+
* with at least the 'expire' key for absolute epoch expiry (or null).
9990
*/
100-
public function getMetaData(string $key);
91+
public function getMetaData(string $key): ?array;
10192

10293
/**
10394
* 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
@@ -83,10 +83,8 @@ public static function validateKey($key, $prefix = ''): string
8383
* @param string $key Cache item name
8484
* @param int $ttl Time to live
8585
* @param Closure(): mixed $callback Callback return value
86-
*
87-
* @return mixed
8886
*/
89-
public function remember(string $key, int $ttl, Closure $callback)
87+
public function remember(string $key, int $ttl, Closure $callback): mixed
9088
{
9189
$value = $this->get($key);
9290

system/Cache/Handlers/DummyHandler.php

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,98 +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-
*
67-
* @return int
68-
*/
69-
public function deleteMatching(string $pattern)
52+
public function deleteMatching(string $pattern): int
7053
{
7154
return 0;
7255
}
7356

74-
/**
75-
* {@inheritDoc}
76-
*/
77-
public function increment(string $key, int $offset = 1)
57+
public function increment(string $key, int $offset = 1): bool
7858
{
7959
return true;
8060
}
8161

82-
/**
83-
* {@inheritDoc}
84-
*/
85-
public function decrement(string $key, int $offset = 1)
62+
public function decrement(string $key, int $offset = 1): bool
8663
{
8764
return true;
8865
}
8966

90-
/**
91-
* {@inheritDoc}
92-
*/
93-
public function clean()
67+
public function clean(): bool
9468
{
9569
return true;
9670
}
9771

98-
/**
99-
* {@inheritDoc}
100-
*/
101-
public function getCacheInfo()
72+
public function getCacheInfo(): ?array
10273
{
10374
return null;
10475
}
10576

106-
/**
107-
* {@inheritDoc}
108-
*/
109-
public function getMetaData(string $key)
77+
public function getMetaData(string $key): ?array
11078
{
11179
return null;
11280
}
11381

114-
/**
115-
* {@inheritDoc}
116-
*/
11782
public function isSupported(): bool
11883
{
11984
return true;

system/Cache/Handlers/FileHandler.php

Lines changed: 16 additions & 50 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,22 +113,14 @@ 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-
*
135-
* @return int
136-
*/
137-
public function deleteMatching(string $pattern)
123+
public function deleteMatching(string $pattern): int
138124
{
139125
$deleted = 0;
140126

@@ -147,10 +133,7 @@ public function deleteMatching(string $pattern)
147133
return $deleted;
148134
}
149135

150-
/**
151-
* {@inheritDoc}
152-
*/
153-
public function increment(string $key, int $offset = 1)
136+
public function increment(string $key, int $offset = 1): bool|int
154137
{
155138
$prefixedKey = static::validateKey($key, $this->prefix);
156139
$tmp = $this->getItem($prefixedKey);
@@ -170,39 +153,27 @@ public function increment(string $key, int $offset = 1)
170153
return $this->save($key, $value, $ttl) ? $value : false;
171154
}
172155

173-
/**
174-
* {@inheritDoc}
175-
*/
176-
public function decrement(string $key, int $offset = 1)
156+
public function decrement(string $key, int $offset = 1): bool|int
177157
{
178158
return $this->increment($key, -$offset);
179159
}
180160

181-
/**
182-
* {@inheritDoc}
183-
*/
184-
public function clean()
161+
public function clean(): bool
185162
{
186163
return delete_files($this->path, false, true);
187164
}
188165

189-
/**
190-
* {@inheritDoc}
191-
*/
192-
public function getCacheInfo()
166+
public function getCacheInfo(): array
193167
{
194168
return get_dir_file_info($this->path);
195169
}
196170

197-
/**
198-
* {@inheritDoc}
199-
*/
200-
public function getMetaData(string $key)
171+
public function getMetaData(string $key): ?array
201172
{
202173
$key = static::validateKey($key, $this->prefix);
203174

204175
if (false === $data = $this->getItem($key)) {
205-
return false; // @TODO This will return null in a future release
176+
return null;
206177
}
207178

208179
return [
@@ -212,9 +183,6 @@ public function getMetaData(string $key)
212183
];
213184
}
214185

215-
/**
216-
* {@inheritDoc}
217-
*/
218186
public function isSupported(): bool
219187
{
220188
return is_writable($this->path);
@@ -226,7 +194,7 @@ public function isSupported(): bool
226194
*
227195
* @return array{data: mixed, ttl: int, time: int}|false
228196
*/
229-
protected function getItem(string $filename)
197+
protected function getItem(string $filename): array|false
230198
{
231199
if (! is_file($this->path . $filename)) {
232200
return false;
@@ -273,10 +241,8 @@ protected function getItem(string $filename)
273241
* @param string $path
274242
* @param string $data
275243
* @param string $mode
276-
*
277-
* @return bool
278244
*/
279-
protected function writeFile($path, $data, $mode = 'wb')
245+
protected function writeFile($path, $data, $mode = 'wb'): bool
280246
{
281247
if (($fp = @fopen($path, $mode)) === false) {
282248
return false;
@@ -355,7 +321,7 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs
355321
* relative_path: string,
356322
* }>|false
357323
*/
358-
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
359325
{
360326
static $filedata = [];
361327

@@ -414,7 +380,7 @@ protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true,
414380
* fileperms?: int
415381
* }|false
416382
*/
417-
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
418384
{
419385
if (! is_file($file)) {
420386
return false;

0 commit comments

Comments
 (0)