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