Skip to content

Commit 690ecb1

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 690ecb1

File tree

13 files changed

+132
-285
lines changed

13 files changed

+132
-285
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: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,98 +22,72 @@
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

52+
<<<<<<< HEAD
6453
/**
6554
* {@inheritDoc}
6655
*
6756
* @return int
6857
*/
6958
public function deleteMatching(string $pattern)
59+
=======
60+
public function deleteMatching(string $pattern): int
61+
>>>>>>> a8ad4f0f8c (refactor(cache): add native return types to all methods in CacheInterface)
7062
{
7163
return 0;
7264
}
7365

74-
/**
75-
* {@inheritDoc}
76-
*/
77-
public function increment(string $key, int $offset = 1)
66+
public function increment(string $key, int $offset = 1): bool
7867
{
7968
return true;
8069
}
8170

82-
/**
83-
* {@inheritDoc}
84-
*/
85-
public function decrement(string $key, int $offset = 1)
71+
public function decrement(string $key, int $offset = 1): bool
8672
{
8773
return true;
8874
}
8975

90-
/**
91-
* {@inheritDoc}
92-
*/
93-
public function clean()
76+
public function clean(): bool
9477
{
9578
return true;
9679
}
9780

98-
/**
99-
* {@inheritDoc}
100-
*/
101-
public function getCacheInfo()
81+
public function getCacheInfo(): ?array
10282
{
10383
return null;
10484
}
10585

106-
/**
107-
* {@inheritDoc}
108-
*/
109-
public function getMetaData(string $key)
86+
public function getMetaData(string $key): ?array
11087
{
11188
return null;
11289
}
11390

114-
/**
115-
* {@inheritDoc}
116-
*/
11791
public function isSupported(): bool
11892
{
11993
return true;

system/Cache/Handlers/FileHandler.php

Lines changed: 19 additions & 44 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,23 @@ 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

123+
<<<<<<< HEAD
132124
/**
133125
* {@inheritDoc}
134126
*
135127
* @return int
136128
*/
137129
public function deleteMatching(string $pattern)
130+
=======
131+
public function deleteMatching(string $pattern): int
132+
>>>>>>> a8ad4f0f8c (refactor(cache): add native return types to all methods in CacheInterface)
138133
{
139134
$deleted = 0;
140135

@@ -147,10 +142,7 @@ public function deleteMatching(string $pattern)
147142
return $deleted;
148143
}
149144

150-
/**
151-
* {@inheritDoc}
152-
*/
153-
public function increment(string $key, int $offset = 1)
145+
public function increment(string $key, int $offset = 1): bool|int
154146
{
155147
$prefixedKey = static::validateKey($key, $this->prefix);
156148
$tmp = $this->getItem($prefixedKey);
@@ -170,39 +162,27 @@ public function increment(string $key, int $offset = 1)
170162
return $this->save($key, $value, $ttl) ? $value : false;
171163
}
172164

173-
/**
174-
* {@inheritDoc}
175-
*/
176-
public function decrement(string $key, int $offset = 1)
165+
public function decrement(string $key, int $offset = 1): bool|int
177166
{
178167
return $this->increment($key, -$offset);
179168
}
180169

181-
/**
182-
* {@inheritDoc}
183-
*/
184-
public function clean()
170+
public function clean(): bool
185171
{
186172
return delete_files($this->path, false, true);
187173
}
188174

189-
/**
190-
* {@inheritDoc}
191-
*/
192-
public function getCacheInfo()
175+
public function getCacheInfo(): array
193176
{
194177
return get_dir_file_info($this->path);
195178
}
196179

197-
/**
198-
* {@inheritDoc}
199-
*/
200-
public function getMetaData(string $key)
180+
public function getMetaData(string $key): ?array
201181
{
202182
$key = static::validateKey($key, $this->prefix);
203183

204184
if (false === $data = $this->getItem($key)) {
205-
return false; // @TODO This will return null in a future release
185+
return null;
206186
}
207187

208188
return [
@@ -212,9 +192,6 @@ public function getMetaData(string $key)
212192
];
213193
}
214194

215-
/**
216-
* {@inheritDoc}
217-
*/
218195
public function isSupported(): bool
219196
{
220197
return is_writable($this->path);
@@ -226,7 +203,7 @@ public function isSupported(): bool
226203
*
227204
* @return array{data: mixed, ttl: int, time: int}|false
228205
*/
229-
protected function getItem(string $filename)
206+
protected function getItem(string $filename): array|false
230207
{
231208
if (! is_file($this->path . $filename)) {
232209
return false;
@@ -273,10 +250,8 @@ protected function getItem(string $filename)
273250
* @param string $path
274251
* @param string $data
275252
* @param string $mode
276-
*
277-
* @return bool
278253
*/
279-
protected function writeFile($path, $data, $mode = 'wb')
254+
protected function writeFile($path, $data, $mode = 'wb'): bool
280255
{
281256
if (($fp = @fopen($path, $mode)) === false) {
282257
return false;
@@ -355,7 +330,7 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs
355330
* relative_path: string,
356331
* }>|false
357332
*/
358-
protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false)
333+
protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true, bool $_recursion = false): array|false
359334
{
360335
static $filedata = [];
361336

@@ -414,7 +389,7 @@ protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true,
414389
* fileperms?: int
415390
* }|false
416391
*/
417-
protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date'])
392+
protected function getFileInfo(string $file, $returnedValues = ['name', 'server_path', 'size', 'date']): array|false
418393
{
419394
if (! is_file($file)) {
420395
return false;

0 commit comments

Comments
 (0)