Skip to content

Commit b815c48

Browse files
authored
Merge pull request #7667 from paulbalandan/refactor-file-handler-cache
refactor: [Cache] simplify code of `FileHandler::getItem()`
2 parents baf3fdb + 78ff86f commit b815c48

3 files changed

Lines changed: 39 additions & 44 deletions

File tree

phpstan-baseline.neon.dist

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ parameters:
1010
count: 1
1111
path: system/Autoloader/Autoloader.php
1212

13-
-
14-
message: "#^Comparison operation \"\\>\" between int and \\(array\\|float\\|int\\) results in an error\\.$#"
15-
count: 1
16-
path: system/Cache/Handlers/FileHandler.php
17-
18-
-
19-
message: "#^If condition is always true\\.$#"
20-
count: 1
21-
path: system/Cache/Handlers/FileHandler.php
22-
2313
-
2414
message: "#^Property CodeIgniter\\\\Cache\\\\Handlers\\\\RedisHandler\\:\\:\\$redis \\(Redis\\) in isset\\(\\) is not nullable\\.$#"
2515
count: 1

system/Cache/Handlers/FileHandler.php

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -144,43 +144,30 @@ public function deleteMatching(string $pattern)
144144
*/
145145
public function increment(string $key, int $offset = 1)
146146
{
147-
$key = static::validateKey($key, $this->prefix);
148-
$data = $this->getItem($key);
147+
$key = static::validateKey($key, $this->prefix);
148+
$tmp = $this->getItem($key);
149149

150-
if ($data === false) {
151-
$data = [
152-
'data' => 0,
153-
'ttl' => 60,
154-
];
155-
} elseif (! is_int($data['data'])) {
150+
if ($tmp === false) {
151+
$tmp = ['data' => 0, 'ttl' => 60];
152+
}
153+
154+
['data' => $value, 'ttl' => $ttl] = $tmp;
155+
156+
if (! is_int($value)) {
156157
return false;
157158
}
158159

159-
$newValue = $data['data'] + $offset;
160+
$value += $offset;
160161

161-
return $this->save($key, $newValue, $data['ttl']) ? $newValue : false;
162+
return $this->save($key, $value, $ttl) ? $value : false;
162163
}
163164

164165
/**
165166
* {@inheritDoc}
166167
*/
167168
public function decrement(string $key, int $offset = 1)
168169
{
169-
$key = static::validateKey($key, $this->prefix);
170-
$data = $this->getItem($key);
171-
172-
if ($data === false) {
173-
$data = [
174-
'data' => 0,
175-
'ttl' => 60,
176-
];
177-
} elseif (! is_int($data['data'])) {
178-
return false;
179-
}
180-
181-
$newValue = $data['data'] - $offset;
182-
183-
return $this->save($key, $newValue, $data['ttl']) ? $newValue : false;
170+
return $this->increment($key, -$offset);
184171
}
185172

186173
/**
@@ -229,7 +216,8 @@ public function isSupported(): bool
229216
* Does the heavy lifting of actually retrieving the file and
230217
* verifying it's age.
231218
*
232-
* @return array|bool|float|int|object|string|null
219+
* @return array<string, mixed>|false
220+
* @phpstan-return array{data: mixed, ttl: int, time: int}|false
233221
*/
234222
protected function getItem(string $filename)
235223
{
@@ -238,15 +226,21 @@ protected function getItem(string $filename)
238226
}
239227

240228
$data = @unserialize(file_get_contents($this->path . $filename));
241-
if (! is_array($data) || ! isset($data['ttl'])) {
229+
230+
if (! is_array($data)) {
231+
return false;
232+
}
233+
234+
if (! isset($data['ttl']) || ! is_int($data['ttl'])) {
235+
return false;
236+
}
237+
238+
if (! isset($data['time']) || ! is_int($data['time'])) {
242239
return false;
243240
}
244241

245242
if ($data['ttl'] > 0 && Time::now()->getTimestamp() > $data['time'] + $data['ttl']) {
246-
// If the file is still there then try to remove it
247-
if (is_file($this->path . $filename)) {
248-
@unlink($this->path . $filename);
249-
}
243+
@unlink($this->path . $filename);
250244

251245
return false;
252246
}

tests/system/Cache/Handlers/FileHandlerTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public function testNew()
7878
$this->assertInstanceOf(FileHandler::class, $this->handler);
7979
}
8080

81+
/**
82+
* chmod('path', 0444) does not work on Windows
83+
*
84+
* @requires OS Linux|Darwin
85+
*/
8186
public function testNewWithNonWritablePath()
8287
{
8388
$this->expectException(CacheException::class);
@@ -132,6 +137,11 @@ public function testRemember()
132137
$this->assertNull($this->handler->get(self::$key1));
133138
}
134139

140+
/**
141+
* chmod('path', 0444) does not work on Windows
142+
*
143+
* @requires OS Linux|Darwin
144+
*/
135145
public function testSave()
136146
{
137147
$this->assertTrue($this->handler->save(self::$key1, 'value'));
@@ -265,10 +275,11 @@ public function testIsSupported()
265275
/**
266276
* @dataProvider modeProvider
267277
*
268-
* @param mixed $int
269-
* @param mixed $string
278+
* permissions given on Windows are fixed to `0666`
279+
*
280+
* @requires OS Linux|Darwin
270281
*/
271-
public function testSaveMode($int, $string)
282+
public function testSaveMode(int $int, string $string): void
272283
{
273284
// Initialize mode
274285
$config = new Cache();

0 commit comments

Comments
 (0)