From 60f3840b67606f13e6942a457667e9c016dce8c5 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Mon, 11 Nov 2024 10:44:15 +0100 Subject: [PATCH] Optimize code --- src/Drivers/Gd/Decoders/AbstractDecoder.php | 8 -------- src/Drivers/Gd/Frame.php | 6 ++++++ src/Drivers/Imagick/Frame.php | 6 ++++++ tests/Unit/Drivers/Gd/FrameTest.php | 4 ++-- tests/Unit/Drivers/Imagick/FrameTest.php | 8 ++++---- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Drivers/Gd/Decoders/AbstractDecoder.php b/src/Drivers/Gd/Decoders/AbstractDecoder.php index 0ad9f110..d585f4da 100644 --- a/src/Drivers/Gd/Decoders/AbstractDecoder.php +++ b/src/Drivers/Gd/Decoders/AbstractDecoder.php @@ -26,10 +26,6 @@ abstract class AbstractDecoder extends SpecializableDecoder implements Specializ throw new DecoderException('Unable to detect media (MIME) from data in file path.'); } - if (!array_key_exists('mime', $info)) { - throw new DecoderException('Unable to detect media (MIME) from data in file path.'); - } - return MediaType::from($info['mime']); } @@ -48,10 +44,6 @@ abstract class AbstractDecoder extends SpecializableDecoder implements Specializ throw new DecoderException('Unable to detect media (MIME) from binary data.'); } - if (!array_key_exists('mime', $info)) { - throw new DecoderException('Unable to detect media (MIME) from binary data.'); - } - return MediaType::from($info['mime']); } } diff --git a/src/Drivers/Gd/Frame.php b/src/Drivers/Gd/Frame.php index af1f3f19..c0ff3e29 100644 --- a/src/Drivers/Gd/Frame.php +++ b/src/Drivers/Gd/Frame.php @@ -6,6 +6,7 @@ namespace Intervention\Image\Drivers\Gd; use GdImage; use Intervention\Image\Exceptions\ColorException; +use Intervention\Image\Exceptions\InputException; use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Image; use Intervention\Image\Interfaces\DriverInterface; @@ -112,9 +113,14 @@ class Frame implements FrameInterface * {@inheritdoc} * * @see FrameInterface::setDispose() + * @throws InputException */ public function setDispose(int $dispose): FrameInterface { + if (!in_array($dispose, [0, 1, 2, 3])) { + throw new InputException('Value for argument $dispose must be 0, 1, 2 or 3.'); + } + $this->dispose = $dispose; return $this; diff --git a/src/Drivers/Imagick/Frame.php b/src/Drivers/Imagick/Frame.php index 354504cf..a957ba5a 100644 --- a/src/Drivers/Imagick/Frame.php +++ b/src/Drivers/Imagick/Frame.php @@ -7,6 +7,7 @@ namespace Intervention\Image\Drivers\Imagick; use Imagick; use ImagickException; use ImagickPixel; +use Intervention\Image\Exceptions\InputException; use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Image; use Intervention\Image\Interfaces\DriverInterface; @@ -111,9 +112,14 @@ class Frame implements FrameInterface * {@inheritdoc} * * @see DriverInterface::setDispose() + * @throws InputException */ public function setDispose(int $dispose): FrameInterface { + if (!in_array($dispose, [0, 1, 2, 3])) { + throw new InputException('Value for argument $dispose must be 0, 1, 2 or 3.'); + } + $this->native->setImageDispose($dispose); return $this; diff --git a/tests/Unit/Drivers/Gd/FrameTest.php b/tests/Unit/Drivers/Gd/FrameTest.php index f7a33169..d63210ef 100644 --- a/tests/Unit/Drivers/Gd/FrameTest.php +++ b/tests/Unit/Drivers/Gd/FrameTest.php @@ -66,9 +66,9 @@ final class FrameTest extends BaseTestCase $frame = $this->getTestFrame(); $this->assertEquals(1, $frame->dispose()); - $result = $frame->setDispose(100); + $result = $frame->setDispose(3); $this->assertInstanceOf(Frame::class, $result); - $this->assertEquals(100, $frame->dispose()); + $this->assertEquals(3, $frame->dispose()); } public function testSetGetOffsetLeft(): void diff --git a/tests/Unit/Drivers/Imagick/FrameTest.php b/tests/Unit/Drivers/Imagick/FrameTest.php index 632dce40..0654697c 100644 --- a/tests/Unit/Drivers/Imagick/FrameTest.php +++ b/tests/Unit/Drivers/Imagick/FrameTest.php @@ -23,7 +23,7 @@ final class FrameTest extends BaseTestCase $imagick = new Imagick(); $imagick->newImage(3, 2, new ImagickPixel('red'), 'png'); $imagick->setImageDelay(125); // 1.25 seconds - $imagick->setImageDispose(5); + $imagick->setImageDispose(0); $imagick->setImagePage(3, 2, 8, 9); return new Frame($imagick); @@ -55,11 +55,11 @@ final class FrameTest extends BaseTestCase public function testSetGetDispose(): void { $frame = $this->getTestFrame(); - $this->assertEquals(5, $frame->dispose()); + $this->assertEquals(0, $frame->dispose()); - $result = $frame->setDispose(100); + $result = $frame->setDispose(3); $this->assertInstanceOf(Frame::class, $result); - $this->assertEquals(100, $frame->dispose()); + $this->assertEquals(3, $frame->dispose()); } public function testSetGetOffsetLeft(): void