From c02f3266b7b0c80e73aaf6d935336f206762dee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Demonchaux?= Date: Wed, 8 Nov 2023 11:59:54 +0100 Subject: [PATCH] remove a lot of psalm error --- psalm-baseline.xml | 133 +++--------------- src/Filesystem.php | 7 +- .../SimpleCache/FilesystemIntegrationTest.php | 1 + test/unit/FilesystemTest.php | 27 +++- 4 files changed, 46 insertions(+), 122 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index dd0bd9d..b2c3663 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,142 +1,49 @@ - + - - bool - bool - - - $pathname - $pathname - $pathname - - - parent::addItems($keyValuePairs) - parent::replaceItems($keyValuePairs) - - - bool - bool - - - parent::setOptions($options) - - - $dir - $event - $event - - + $key - $params['namespace_separator'] + - - $metadata - - - $params['namespace_separator'] - - + $content - $contents[$this->formatFilename($filespec)] + formatFilename($filespec)]]]> $key - $params - $params $value - - $clearFolder($pathname) - - + FilesystemOptions - bool - bool - bool - - attach - detach - getParams - getParams - - - $dir - - - $this->options + + options]]> - + $options - - - Filesystem - - - $pathname - $pathname - $pathname - $pathname - $pathname - $pathname - $pathname - - $diff - $diff - - - Filesystem - - + (string) $namespace (string) $prefix - - $clearFolder - - - - + + totalSpace]]> + + + formatFilename($filespec)] = &$value]]> + - + (int) $mode - + $options - - - $dirPermission - $filePermission - $umask - - + + (int) $dirPermission (int) $filePermission (int) $umask - - - $cacheDir - - - - - $meta['filespec'] - $meta['filespec'] - - - $cacheDir - - - $err['message'] - $err['message'] - - - $glob - - diff --git a/src/Filesystem.php b/src/Filesystem.php index 5afc2ca..570fe17 100644 --- a/src/Filesystem.php +++ b/src/Filesystem.php @@ -25,6 +25,7 @@ use Laminas\EventManager\EventInterface; use Laminas\EventManager\EventManagerInterface; use Laminas\Stdlib\ErrorHandler; +use SplObjectStorage; use stdClass; use Traversable; @@ -39,6 +40,7 @@ use function func_num_args; use function glob; use function implode; +use function is_callable; use function is_string; use function max; use function md5; @@ -102,8 +104,8 @@ final class Filesystem extends AbstractAdapter implements public function __construct($options = null, ?FilesystemInteractionInterface $filesystem = null) { parent::__construct($options); - $this->pluginRegistry = new \SplObjectStorage(); - $this->filesystem = $filesystem ?? new LocalFilesystemInteraction(); + $this->pluginRegistry = new SplObjectStorage(); + $this->filesystem = $filesystem ?? new LocalFilesystemInteraction(); // clean total space buffer on change cache_dir $events = $this->getEventManager(); @@ -230,6 +232,7 @@ public function clearExpired() ErrorHandler::start(); foreach ($glob as $pathname) { + $pathname = (string) $pathname; // get last modification time of the file but ignore if the file is missing // to fix a possible race condition if another process removed the file already. try { diff --git a/test/integration/Psr/SimpleCache/FilesystemIntegrationTest.php b/test/integration/Psr/SimpleCache/FilesystemIntegrationTest.php index c2c0465..846a3e3 100644 --- a/test/integration/Psr/SimpleCache/FilesystemIntegrationTest.php +++ b/test/integration/Psr/SimpleCache/FilesystemIntegrationTest.php @@ -12,6 +12,7 @@ use function assert; use function getenv; +use function is_string; use function mkdir; use function sys_get_temp_dir; use function tempnam; diff --git a/test/unit/FilesystemTest.php b/test/unit/FilesystemTest.php index 5bfbf8d..7f6deac 100644 --- a/test/unit/FilesystemTest.php +++ b/test/unit/FilesystemTest.php @@ -11,6 +11,7 @@ use Laminas\Cache\Storage\Plugin\PluginOptions; use LaminasTest\Cache\Storage\Adapter\Filesystem\TestAsset\DelayedFilesystemInteraction; +use function array_key_exists; use function chmod; use function count; use function error_get_last; @@ -18,12 +19,15 @@ use function filectime; use function getenv; use function glob; +use function is_array; +use function is_string; use function md5; use function mkdir; use function pcntl_fork; use function posix_getpid; use function posix_kill; use function sleep; +use function sprintf; use function str_repeat; use function substr; use function sys_get_temp_dir; @@ -47,21 +51,26 @@ protected function setUp(): void { $this->umask = umask(); - if (getenv('TESTS_LAMINAS_CACHE_FILESYSTEM_DIR')) { - $cacheDir = getenv('TESTS_LAMINAS_CACHE_FILESYSTEM_DIR'); - } else { - $cacheDir = sys_get_temp_dir(); - } + $cacheDir = getenv('TESTS_LAMINAS_CACHE_FILESYSTEM_DIR'); + $cacheDir = is_string($cacheDir) ? $cacheDir : sys_get_temp_dir(); + + $getMessage = static function (array|null $value): string { + if (! is_array($value) || ! array_key_exists('message', $value) || ! is_string($value['message'])) { + return 'no information'; + } + + return $value['message']; + }; $this->tmpCacheDir = tempnam($cacheDir, 'laminas_cache_test_'); if (! $this->tmpCacheDir) { $this->fail("Can't create temporary cache directory-file."); } elseif (! @unlink($this->tmpCacheDir)) { $err = error_get_last(); - $this->fail("Can't remove temporary cache directory-file: {$err['message']}"); + $this->fail(sprintf("Can't remove temporary cache directory-file: %s", $getMessage($err))); } elseif (! @mkdir($this->tmpCacheDir, 0777)) { $err = error_get_last(); - $this->fail("Can't create temporary cache directory: {$err['message']}"); + $this->fail(sprintf("Can't create temporary cache directory: %s", $getMessage($err))); } $this->options = new FilesystemOptions([ @@ -127,6 +136,8 @@ public function testGetMetadataWithCtime(): void $meta = $this->storage->getMetadata('test'); $this->assertIsArray($meta); + $this->assertArrayHasKey('filespec', $meta); + $this->assertIsString($meta['filespec']); $expectedCtime = filectime($meta['filespec'] . '.dat'); $this->assertEquals($expectedCtime, $meta['ctime']); @@ -140,6 +151,8 @@ public function testGetMetadataWithAtime(): void $meta = $this->storage->getMetadata('test'); $this->assertIsArray($meta); + $this->assertArrayHasKey('filespec', $meta); + $this->assertIsString($meta['filespec']); $expectedAtime = fileatime($meta['filespec'] . '.dat'); $this->assertEquals($expectedAtime, $meta['atime']);