1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-22 13:32:56 +02:00

Optimize code

This commit is contained in:
Oliver Vogel
2025-08-02 08:03:21 +02:00
parent eecd98e338
commit d1f135b9e1
6 changed files with 44 additions and 21 deletions

View File

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

View File

@@ -37,7 +37,7 @@ class Core implements CoreInterface, Iterator
public function has(int|string $key): bool
{
try {
$result = $this->imagick->setIteratorIndex($key);
$result = $this->imagick->setIteratorIndex((int) $key);
} catch (ImagickException) {
return false;
}
@@ -63,7 +63,7 @@ class Core implements CoreInterface, Iterator
public function get(int|string $key, mixed $default = null): mixed
{
try {
$this->imagick->setIteratorIndex($key);
$this->imagick->setIteratorIndex((int) $key);
} catch (ImagickException) {
return $default;
}

View File

@@ -6,6 +6,7 @@ namespace Intervention\Image;
use Error;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\RuntimeException;
enum FileExtension: string
{
@@ -35,6 +36,7 @@ enum FileExtension: string
*
* @param string|Format|MediaType|FileExtension $identifier
* @throws NotSupportedException
* @throws RuntimeException
*/
public static function create(string|self|Format|MediaType $identifier): self
{
@@ -66,8 +68,7 @@ enum FileExtension: string
/**
* Try to create media type from given identifier and return null on failure
*
* @param string|Format|MediaType|FileExtension $identifier
* @return FileExtension|null
* @throws RuntimeException
*/
public static function tryCreate(string|self|Format|MediaType $identifier): ?self
{
@@ -119,6 +120,8 @@ enum FileExtension: string
/**
* Return the first found media type for the current format
*
* @throws RuntimeException
*/
public function mediaType(): MediaType
{

View File

@@ -15,6 +15,7 @@ use Intervention\Image\Encoders\PngEncoder;
use Intervention\Image\Encoders\TiffEncoder;
use Intervention\Image\Encoders\WebpEncoder;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\EncoderInterface;
use ReflectionClass;
use ReflectionParameter;
@@ -94,12 +95,20 @@ enum Format
/**
* Return the first found media type for the current format
*
* @throws RuntimeException
*/
public function mediaType(): MediaType
{
$types = $this->mediaTypes();
return reset($types);
$result = reset($types);
if (!($result instanceof MediaType)) {
throw new RuntimeException('Unable to retrieve media type.');
}
return $result;
}
/**
@@ -117,12 +126,20 @@ enum Format
/**
* Return the first found file extension for the current format
*
* @throws RuntimeException
*/
public function fileExtension(): FileExtension
{
$extensions = $this->fileExtensions();
return reset($extensions);
$result = reset($extensions);
if (!($result instanceof FileExtension)) {
throw new RuntimeException('Unable to retrieve file extension.');
}
return $result;
}
/**

View File

@@ -6,6 +6,8 @@ namespace Intervention\Image;
use Error;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Exceptions\RuntimeException as ExceptionsRuntimeException;
enum MediaType: string
{
@@ -41,7 +43,7 @@ enum MediaType: string
/**
* Create media type from given identifier
*
* @param string|Format|MediaType|FileExtension $identifier
* @throws ExceptionsRuntimeException
* @throws NotSupportedException
*/
public static function create(string|self|Format|FileExtension $identifier): self
@@ -135,6 +137,8 @@ enum MediaType: string
/**
* Return the first file extension for the current media type
*
* @throws RuntimeException
*/
public function fileExtension(): FileExtension
{

View File

@@ -13,26 +13,25 @@ trait CanBuildFilePointer
*
* @param resource|string|null $data
* @throws RuntimeException
* @return resource|false
* @return resource
*/
public function buildFilePointer(mixed $data = null)
{
switch (true) {
case is_string($data):
$buildPointerStrategy = match (true) {
is_null($data) => fn(mixed $data) => fopen('php://temp', 'r+'),
is_resource($data) && get_resource_type($data) === 'stream' => fn(mixed $data) => $data,
is_string($data) => function (mixed $data) {
$pointer = fopen('php://temp', 'r+');
fwrite($pointer, $data);
break;
return $pointer;
},
default => throw new RuntimeException('Unable to build file pointer.'),
};
case is_resource($data) && get_resource_type($data) === 'stream':
$pointer = $data;
break;
$pointer = call_user_func($buildPointerStrategy, $data);
case is_null($data):
$pointer = fopen('php://temp', 'r+');
break;
default:
throw new RuntimeException('Unable to build file pointer.');
if ($pointer === false) {
throw new RuntimeException('Unable to build file pointer.');
}
rewind($pointer);