1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-20 12:41:23 +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:
level: 7
level: 6
paths:
- src
reportUnmatchedIgnoredErrors: false

View File

@@ -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,
];
}
}

View File

@@ -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
{

View File

@@ -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
{