Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Enh #88: Don't create cache directory on `FileCache` initialization (@vjik)
- Bug #88: Set correct permissions for nested directories (@vjik)
- Bug #85: Clear stat cache in `FileCache::set()` (@samdark)
- Bug #86: Handle race condition when creating cache directory (@vjik)

## 3.1.0 October 09, 2023

Expand Down
21 changes: 17 additions & 4 deletions src/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
use function is_dir;
use function is_file;
use function iterator_to_array;
use function mkdir;
use function opendir;
use function posix_geteuid;
use function random_int;
use function readdir;
use function rmdir;
use function serialize;
use function sprintf;
use function strpbrk;
use function substr;
use function unlink;
Expand Down Expand Up @@ -115,7 +117,7 @@
$this->gc();
$expiration = $this->ttlToExpiration($ttl);

if ($expiration <= self::EXPIRATION_EXPIRED) {

Check warning on line 120 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "LessThanOrEqualTo": --- Original +++ New @@ @@ $this->validateKey($key); $this->gc(); $expiration = $this->ttlToExpiration($ttl); - if ($expiration <= self::EXPIRATION_EXPIRED) { + if ($expiration < self::EXPIRATION_EXPIRED) { return $this->delete($key); } $file = $this->getCacheFile($key, ensureDirectory: true);
return $this->delete($key);
}

Expand Down Expand Up @@ -185,7 +187,7 @@
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
$values = $this->iterableToArray($values);
$this->validateKeys(array_map('\strval', array_keys($values)));

Check warning on line 190 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function setMultiple(iterable $values, null|int|DateInterval $ttl = null) : bool { $values = $this->iterableToArray($values); - $this->validateKeys(array_map('\\strval', array_keys($values))); + foreach ($values as $key => $value) { $this->set((string) $key, $value, $ttl); }

foreach ($values as $key => $value) {
$this->set((string) $key, $value, $ttl);
Expand All @@ -203,7 +205,7 @@
$this->delete($key);
}

return true;

Check warning on line 208 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ foreach ($keys as $key) { $this->delete($key); } - return true; + return false; } public function has(string $key) : bool {
}

public function has(string $key): bool
Expand Down Expand Up @@ -328,10 +330,21 @@
throw new CacheException("Failed to create cache directory, file with the same name exists: \"$path\".");
}

mkdir($path, recursive: true);

if (!is_dir($path)) {
throw new CacheException("Failed to create cache directory \"$path\".");
set_error_handler(
static function (int $errorNumber, string $errorString) use ($path): bool {
if (is_dir($path)) {
return true;

Check warning on line 336 in src/FileCache.php

View check run for this annotation

Codecov / codecov/patch

src/FileCache.php#L336

Added line #L336 was not covered by tests
}
throw new CacheException(
sprintf('Failed to create directory "%s". %s', $path, $errorString),
$errorNumber,
);
}
);
try {

Check warning on line 344 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "UnwrapFinally": --- Original +++ New @@ @@ } throw new CacheException(sprintf('Failed to create directory "%s". %s', $path, $errorString), $errorNumber); }); - try { - mkdir($path, recursive: true); - } finally { - restore_error_handler(); - } + mkdir($path, recursive: true); + restore_error_handler(); chmod($path, $this->directoryMode); } /**
mkdir($path, recursive: true);
} finally {
restore_error_handler();
}

chmod($path, $this->directoryMode);
Expand Down
9 changes: 9 additions & 0 deletions tests/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,13 @@ public function testSetClearsStatCache(): void
$this->assertTrue($this->cache->set(__FUNCTION__, 'cache2', 2));
$this->assertSame('cache2', $this->cache->get(__FUNCTION__));
}

public function testMkdirConvertingErrorToException(): void
{
$cache = new FileCache('');

$this->expectException(CacheException::class);
$this->expectExceptionMessage('Failed to create directory "". mkdir(): Invalid path');
$cache->set('test', 0);
}
}
Loading