mirror of
https://github.com/Intervention/image.git
synced 2025-08-20 12:41:23 +02:00
Optimize code
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
parameters:
|
||||
level: 7
|
||||
level: 6
|
||||
paths:
|
||||
- src
|
||||
reportUnmatchedIgnoredErrors: false
|
||||
|
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
26
src/File.php
26
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
|
||||
{
|
||||
|
@@ -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
|
||||
{
|
||||
|
Reference in New Issue
Block a user