From d1f135b9e15430b2231023d3c6f65023f679960d Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 2 Aug 2025 08:03:21 +0200 Subject: [PATCH] Optimize code --- phpstan.dist.neon | 2 +- src/Drivers/Imagick/Core.php | 4 ++-- src/FileExtension.php | 7 +++++-- src/Format.php | 21 +++++++++++++++++++-- src/MediaType.php | 6 +++++- src/Traits/CanBuildFilePointer.php | 25 ++++++++++++------------- 6 files changed, 44 insertions(+), 21 deletions(-) diff --git a/phpstan.dist.neon b/phpstan.dist.neon index d4687cf4..fc55fa09 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -1,5 +1,5 @@ parameters: - level: 6 + level: 7 paths: - src reportUnmatchedIgnoredErrors: false diff --git a/src/Drivers/Imagick/Core.php b/src/Drivers/Imagick/Core.php index 87dcf986..02af465e 100644 --- a/src/Drivers/Imagick/Core.php +++ b/src/Drivers/Imagick/Core.php @@ -37,7 +37,7 @@ class Core implements CoreInterface, Iterator public function has(int|string $key): bool { try { - $result = $this->imagick->setIteratorIndex($key); + $result = $this->imagick->setIteratorIndex((int) $key); } catch (ImagickException) { return false; } @@ -63,7 +63,7 @@ class Core implements CoreInterface, Iterator public function get(int|string $key, mixed $default = null): mixed { try { - $this->imagick->setIteratorIndex($key); + $this->imagick->setIteratorIndex((int) $key); } catch (ImagickException) { return $default; } diff --git a/src/FileExtension.php b/src/FileExtension.php index dce24c53..a8226659 100644 --- a/src/FileExtension.php +++ b/src/FileExtension.php @@ -6,6 +6,7 @@ namespace Intervention\Image; use Error; use Intervention\Image\Exceptions\NotSupportedException; +use Intervention\Image\Exceptions\RuntimeException; enum FileExtension: string { @@ -35,6 +36,7 @@ enum FileExtension: string * * @param string|Format|MediaType|FileExtension $identifier * @throws NotSupportedException + * @throws RuntimeException */ public static function create(string|self|Format|MediaType $identifier): self { @@ -66,8 +68,7 @@ enum FileExtension: string /** * Try to create media type from given identifier and return null on failure * - * @param string|Format|MediaType|FileExtension $identifier - * @return FileExtension|null + * @throws RuntimeException */ public static function tryCreate(string|self|Format|MediaType $identifier): ?self { @@ -119,6 +120,8 @@ enum FileExtension: string /** * Return the first found media type for the current format + * + * @throws RuntimeException */ public function mediaType(): MediaType { diff --git a/src/Format.php b/src/Format.php index 09be895d..168df766 100644 --- a/src/Format.php +++ b/src/Format.php @@ -15,6 +15,7 @@ use Intervention\Image\Encoders\PngEncoder; use Intervention\Image\Encoders\TiffEncoder; use Intervention\Image\Encoders\WebpEncoder; use Intervention\Image\Exceptions\NotSupportedException; +use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Interfaces\EncoderInterface; use ReflectionClass; use ReflectionParameter; @@ -94,12 +95,20 @@ enum Format /** * Return the first found media type for the current format + * + * @throws RuntimeException */ public function mediaType(): MediaType { $types = $this->mediaTypes(); - return reset($types); + $result = reset($types); + + if (!($result instanceof MediaType)) { + throw new RuntimeException('Unable to retrieve media type.'); + } + + return $result; } /** @@ -117,12 +126,20 @@ enum Format /** * Return the first found file extension for the current format + * + * @throws RuntimeException */ public function fileExtension(): FileExtension { $extensions = $this->fileExtensions(); - return reset($extensions); + $result = reset($extensions); + + if (!($result instanceof FileExtension)) { + throw new RuntimeException('Unable to retrieve file extension.'); + } + + return $result; } /** diff --git a/src/MediaType.php b/src/MediaType.php index 7718a549..33ba8058 100644 --- a/src/MediaType.php +++ b/src/MediaType.php @@ -6,6 +6,8 @@ namespace Intervention\Image; use Error; use Intervention\Image\Exceptions\NotSupportedException; +use Intervention\Image\Exceptions\RuntimeException; +use Intervention\Image\Exceptions\RuntimeException as ExceptionsRuntimeException; enum MediaType: string { @@ -41,7 +43,7 @@ enum MediaType: string /** * Create media type from given identifier * - * @param string|Format|MediaType|FileExtension $identifier + * @throws ExceptionsRuntimeException * @throws NotSupportedException */ public static function create(string|self|Format|FileExtension $identifier): self @@ -135,6 +137,8 @@ enum MediaType: string /** * Return the first file extension for the current media type + * + * @throws RuntimeException */ public function fileExtension(): FileExtension { diff --git a/src/Traits/CanBuildFilePointer.php b/src/Traits/CanBuildFilePointer.php index 29d613d1..a442cca8 100644 --- a/src/Traits/CanBuildFilePointer.php +++ b/src/Traits/CanBuildFilePointer.php @@ -13,26 +13,25 @@ trait CanBuildFilePointer * * @param resource|string|null $data * @throws RuntimeException - * @return resource|false + * @return resource */ public function buildFilePointer(mixed $data = null) { - switch (true) { - case is_string($data): + $buildPointerStrategy = match (true) { + is_null($data) => fn(mixed $data) => fopen('php://temp', 'r+'), + is_resource($data) && get_resource_type($data) === 'stream' => fn(mixed $data) => $data, + is_string($data) => function (mixed $data) { $pointer = fopen('php://temp', 'r+'); fwrite($pointer, $data); - break; + return $pointer; + }, + default => throw new RuntimeException('Unable to build file pointer.'), + }; - case is_resource($data) && get_resource_type($data) === 'stream': - $pointer = $data; - break; + $pointer = call_user_func($buildPointerStrategy, $data); - case is_null($data): - $pointer = fopen('php://temp', 'r+'); - break; - - default: - throw new RuntimeException('Unable to build file pointer.'); + if ($pointer === false) { + throw new RuntimeException('Unable to build file pointer.'); } rewind($pointer);