Skip to content

Commit

Permalink
refactor: simplify code of FileHandler::getItem()
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Jul 6, 2023
1 parent 388029c commit d13b6e7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ parameters:
count: 1
path: system/Autoloader/Autoloader.php

-
message: "#^Comparison operation \"\\>\" between int and \\(array\\|float\\|int\\) results in an error\\.$#"
count: 1
path: system/Cache/Handlers/FileHandler.php

-
message: "#^If condition is always true\\.$#"
count: 1
path: system/Cache/Handlers/FileHandler.php

-
message: "#^Property CodeIgniter\\\\Cache\\\\Handlers\\\\RedisHandler\\:\\:\\$redis \\(Redis\\) in isset\\(\\) is not nullable\\.$#"
count: 1
Expand Down
20 changes: 14 additions & 6 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,32 @@ public function isSupported(): bool
* Does the heavy lifting of actually retrieving the file and
* verifying it's age.
*
* @return array|bool|float|int|object|string|null
* @return array<string, mixed>|false
* @phpstan-return array{data: mixed, ttl: int, time: int}|false
*/
protected function getItem(string $filename)
{
if (! is_file($this->path . $filename)) {
return false;
}

/** @phpstan-var array{data: mixed, ttl?: int, time?: int}|mixed~array $data */
$data = @unserialize(file_get_contents($this->path . $filename));
if (! is_array($data) || ! isset($data['ttl'])) {

if (! is_array($data)) {
return false;
}

if (! isset($data['ttl']) || ! is_int($data['ttl'])) {
return false;
}

if (! isset($data['time']) || ! is_int($data['time'])) {
$data['time'] = 0; // offset time is not needed by increment/decrement methods
}

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

return false;
}
Expand Down
17 changes: 14 additions & 3 deletions tests/system/Cache/Handlers/FileHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public function testNew()
$this->assertInstanceOf(FileHandler::class, $this->handler);
}

/**
* chmod('path', 0444) does not work on Windows
*
* @requires OS Linux|Darwin
*/
public function testNewWithNonWritablePath()
{
$this->expectException(CacheException::class);
Expand Down Expand Up @@ -132,6 +137,11 @@ public function testRemember()
$this->assertNull($this->handler->get(self::$key1));
}

/**
* chmod('path', 0444) does not work on Windows
*
* @requires OS Linux|Darwin
*/
public function testSave()
{
$this->assertTrue($this->handler->save(self::$key1, 'value'));
Expand Down Expand Up @@ -265,10 +275,11 @@ public function testIsSupported()
/**
* @dataProvider modeProvider
*
* @param mixed $int
* @param mixed $string
* permissions given on Windows are fixed to `0666`
*
* @requires OS Linux|Darwin
*/
public function testSaveMode($int, $string)
public function testSaveMode(int $int, string $string): void
{
// Initialize mode
$config = new Cache();
Expand Down

0 comments on commit d13b6e7

Please sign in to comment.