From b3b7d6ba50ffca56cf7c53c188d5feedce765e26 Mon Sep 17 00:00:00 2001 From: Jakob Givoni Date: Wed, 12 Feb 2025 15:07:03 +0100 Subject: [PATCH] 24-increase-phpstan-level-to-10 --- aliases | 6 ++++++ composer.sh | 2 -- phpstan.neon | 8 +------- phpstan.sh | 2 -- phpunit.sh | 2 -- src/CacheAdapter.php | 36 +++++++++++++++++++++--------------- src/CacheItemsTrait.php | 4 ++-- 7 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 aliases delete mode 100755 composer.sh delete mode 100755 phpstan.sh delete mode 100755 phpunit.sh diff --git a/aliases b/aliases new file mode 100644 index 0000000..e1de027 --- /dev/null +++ b/aliases @@ -0,0 +1,6 @@ +# Source this file with `. ./aliases` to use the following shortcuts when developing: +alias d='docker compose exec -it php "$@"' +alias php='d php "$@"' +alias composer='d composer "$@"' +alias phpstan='d vendor/bin/phpstan analyze "$@"' +alias phpunit='d vendor/bin/phpunit "$@"' diff --git a/composer.sh b/composer.sh deleted file mode 100755 index 92cfcf2..0000000 --- a/composer.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -docker compose exec -it php composer "$@" diff --git a/phpstan.neon b/phpstan.neon index c51f86a..f138c52 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,12 +1,6 @@ parameters: - level: 9 + level: 10 paths: - src - tests - - # ignoreErrors: - # - "/no value type specified in iterable type array\\.$/" - # - "/is always (true|false)\\.$/" - - reportUnmatchedIgnoredErrors: false diff --git a/phpstan.sh b/phpstan.sh deleted file mode 100755 index eb57883..0000000 --- a/phpstan.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -docker compose exec -it -e XDEBUG_MODE=off php php -d memory_limit=256M vendor/bin/phpstan analyze "$@" diff --git a/phpunit.sh b/phpunit.sh deleted file mode 100755 index dffe8e6..0000000 --- a/phpunit.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -docker compose exec -it php vendor/bin/phpunit "$@" diff --git a/src/CacheAdapter.php b/src/CacheAdapter.php index bef5437..bc9e5e3 100644 --- a/src/CacheAdapter.php +++ b/src/CacheAdapter.php @@ -246,10 +246,10 @@ public function visibility(string $path): FileAttributes try { return $this->getFileAttributes( path: $path, - loader: function () use ($path) { + loader: function () use ($path): FileAttributes { return $this->adapter->visibility($path); }, - attributeAccessor: function (FileAttributes $fileAttributes) { + attributeAccessor: function (FileAttributes $fileAttributes): ?string { return $fileAttributes->visibility(); }, ); @@ -266,10 +266,10 @@ public function mimeType(string $path): FileAttributes try { return $this->getFileAttributes( path: $path, - loader: function () use ($path) { + loader: function () use ($path): FileAttributes { return $this->adapter->mimeType($path); }, - attributeAccessor: function (FileAttributes $fileAttributes) { + attributeAccessor: function (FileAttributes $fileAttributes): ?string { return $fileAttributes->mimeType(); }, ); @@ -286,10 +286,10 @@ public function lastModified(string $path): FileAttributes try { return $this->getFileAttributes( path: $path, - loader: function () use ($path) { + loader: function () use ($path): FileAttributes { return $this->adapter->lastModified($path); }, - attributeAccessor: function (FileAttributes $fileAttributes) { + attributeAccessor: function (FileAttributes $fileAttributes): ?int { return $fileAttributes->lastModified(); }, ); @@ -306,10 +306,10 @@ public function fileSize(string $path): FileAttributes try { return $this->getFileAttributes( path: $path, - loader: function () use ($path) { + loader: function () use ($path): FileAttributes { return $this->adapter->fileSize($path); }, - attributeAccessor: function (FileAttributes $fileAttributes) { + attributeAccessor: function (FileAttributes $fileAttributes): ?int { return $fileAttributes->fileSize(); }, ); @@ -331,22 +331,28 @@ public function checksum(string $path, Config $config): string $metadataKey = isset($algo) ? 'checksum_' . $algo : 'checksum'; - $attributeAccessor = function (StorageAttributes $storageAttributes) use ($metadataKey) { + $attributeAccessor = function (StorageAttributes $storageAttributes) use ($metadataKey): ?string { if (\is_a($this->adapter, 'League\Flysystem\AwsS3V3\AwsS3V3Adapter')) { // Special optimization for AWS S3, but won't break if adapter not installed $etag = $storageAttributes->extraMetadata()['ETag'] ?? \null; - if (isset($etag)) { - $checksum = trim($etag, '" '); + if (is_string($etag)) { + return trim($etag, '" '); } } - return $checksum ?? $storageAttributes->extraMetadata()[$metadataKey] ?? \null; + $checksum = $storageAttributes->extraMetadata()[$metadataKey] ?? \null; + + if (isset($checksum) && !is_string($checksum)) { + throw new RuntimeException('Checksum must be a string.'); + } + + return $checksum; }; try { $fileAttributes = $this->getFileAttributes( path: $path, - loader: function () use ($path, $config, $metadataKey) { + loader: function () use ($path, $config, $metadataKey): FileAttributes { // This part is "mirrored" from FileSystem class to provide the fallback mechanism // and be able to cache the result try { @@ -363,11 +369,11 @@ public function checksum(string $path, Config $config): string }, attributeAccessor: $attributeAccessor ); + + return $attributeAccessor($fileAttributes) ?? ''; } catch (RuntimeException $e) { throw new UnableToProvideChecksum($e->getMessage(), $path, $e); } - - return $attributeAccessor($fileAttributes); } /** diff --git a/src/CacheItemsTrait.php b/src/CacheItemsTrait.php index 4cbdb64..1f99fcf 100644 --- a/src/CacheItemsTrait.php +++ b/src/CacheItemsTrait.php @@ -117,8 +117,8 @@ protected static function mergeDirectoryAttributes( * Returns FileAttributes from cache if desired attribute is found, * or loads the desired missing attribute from the adapter and merges it with the cached attributes. * - * @param Closure $loader Returns FileAttributes with the desired attribute loaded from adapter - * @param Closure $attributeAccessor Returns value of desired attribute from cached item + * @param Closure(): FileAttributes $loader Returns FileAttributes with the desired attribute loaded from adapter + * @param Closure(FileAttributes): mixed $attributeAccessor Returns value of desired attribute from cached item */ protected function getFileAttributes( string $path,