1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-21 05:01:20 +02:00

Optimize code

This commit is contained in:
Oliver Vogel
2025-08-02 08:19:50 +02:00
parent f217168372
commit 10570616ca
4 changed files with 34 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
parameters: parameters:
level: 7 level: 6
paths: paths:
- src - src
reportUnmatchedIgnoredErrors: false reportUnmatchedIgnoredErrors: false

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Intervention\Image; namespace Intervention\Image;
use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\EncodedImageInterface;
use Throwable;
class EncodedImage extends File implements EncodedImageInterface class EncodedImage extends File implements EncodedImageInterface
{ {
@@ -57,9 +58,15 @@ class EncodedImage extends File implements EncodedImageInterface
*/ */
public function __debugInfo(): array public function __debugInfo(): array
{ {
try {
$size = $this->size();
} catch (Throwable) {
$size = 0;
}
return [ return [
'mediaType' => $this->mediaType(), 'mediaType' => $this->mediaType(),
'size' => $this->size(), 'size' => $size,
]; ];
} }
} }

View File

@@ -37,7 +37,13 @@ class File implements FileInterface, Stringable
*/ */
public static function fromPath(string $path): self 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} * {@inheritdoc}
* *
* @see FileInterface::toString() * @see FileInterface::toString()
*
* @throws RuntimeException
*/ */
public function toString(): string 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} * {@inheritdoc}
* *
* @see FileInterface::size() * @see FileInterface::size()
*
* @throws RuntimeException
*/ */
public function size(): int public function size(): int
{ {
$info = fstat($this->toFilePointer()); $info = fstat($this->toFilePointer());
if (!is_array($info)) {
throw new RuntimeException('Unable to read size of file pointer.');
}
return intval($info['size']); return intval($info['size']);
} }
@@ -114,6 +134,8 @@ class File implements FileInterface, Stringable
* {@inheritdoc} * {@inheritdoc}
* *
* @see FileInterface::__toString() * @see FileInterface::__toString()
*
* @throws RuntimeException
*/ */
public function __toString(): string public function __toString(): string
{ {

View File

@@ -76,8 +76,7 @@ enum MediaType: string
/** /**
* Try to create media type from given identifier and return null on failure * Try to create media type from given identifier and return null on failure
* *
* @param string|Format|MediaType|FileExtension $identifier * @throws RuntimeException
* @return MediaType|null
*/ */
public static function tryCreate(string|self|Format|FileExtension $identifier): ?self public static function tryCreate(string|self|Format|FileExtension $identifier): ?self
{ {