From 10570616ca8ef233f32aeca915efbc9eb2573b73 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 2 Aug 2025 08:19:50 +0200 Subject: [PATCH] Optimize code --- phpstan.dist.neon | 2 +- src/EncodedImage.php | 9 ++++++++- src/File.php | 26 ++++++++++++++++++++++++-- src/MediaType.php | 3 +-- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/phpstan.dist.neon b/phpstan.dist.neon index fc55fa09..d4687cf4 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -1,5 +1,5 @@ parameters: - level: 7 + level: 6 paths: - src reportUnmatchedIgnoredErrors: false diff --git a/src/EncodedImage.php b/src/EncodedImage.php index e8c767a0..1d6bdb6c 100644 --- a/src/EncodedImage.php +++ b/src/EncodedImage.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Intervention\Image; use Intervention\Image\Interfaces\EncodedImageInterface; +use Throwable; class EncodedImage extends File implements EncodedImageInterface { @@ -57,9 +58,15 @@ class EncodedImage extends File implements EncodedImageInterface */ public function __debugInfo(): array { + try { + $size = $this->size(); + } catch (Throwable) { + $size = 0; + } + return [ 'mediaType' => $this->mediaType(), - 'size' => $this->size(), + 'size' => $size, ]; } } diff --git a/src/File.php b/src/File.php index b0750edc..955f0c0c 100644 --- a/src/File.php +++ b/src/File.php @@ -37,7 +37,13 @@ class File implements FileInterface, Stringable */ public static function fromPath(string $path): self { - return new self(fopen($path, 'r')); + $pointer = fopen($path, 'r'); + + if ($pointer === false) { + throw new RuntimeException('Unable to open file from path "' . $path . '".'); + } + + return new self($pointer); } /** @@ -80,10 +86,18 @@ class File implements FileInterface, Stringable * {@inheritdoc} * * @see FileInterface::toString() + * + * @throws RuntimeException */ public function toString(): string { - return stream_get_contents($this->toFilePointer(), offset: 0); + $data = stream_get_contents($this->toFilePointer(), offset: 0); + + if ($data === false) { + throw new RuntimeException('Unable to cast ' . self::class . 'object to string.'); + } + + return $data; } /** @@ -102,11 +116,17 @@ class File implements FileInterface, Stringable * {@inheritdoc} * * @see FileInterface::size() + * + * @throws RuntimeException */ public function size(): int { $info = fstat($this->toFilePointer()); + if (!is_array($info)) { + throw new RuntimeException('Unable to read size of file pointer.'); + } + return intval($info['size']); } @@ -114,6 +134,8 @@ class File implements FileInterface, Stringable * {@inheritdoc} * * @see FileInterface::__toString() + * + * @throws RuntimeException */ public function __toString(): string { diff --git a/src/MediaType.php b/src/MediaType.php index 33ba8058..31b3f41c 100644 --- a/src/MediaType.php +++ b/src/MediaType.php @@ -76,8 +76,7 @@ enum MediaType: string /** * Try to create media type from given identifier and return null on failure * - * @param string|Format|MediaType|FileExtension $identifier - * @return MediaType|null + * @throws RuntimeException */ public static function tryCreate(string|self|Format|FileExtension $identifier): ?self {