diff --git a/composer.json b/composer.json index 00da9c2..4190b85 100755 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "require-dev": { "phpunit/phpunit": "^10.0 || ^11.0", - "phpstan/phpstan": "^2", + "phpstan/phpstan": "^2.1", "squizlabs/php_codesniffer": "^3.8", "slevomat/coding-standard": "~8.0" }, diff --git a/src/AbstractEntity.php b/src/AbstractEntity.php index 605bf7d..6acd426 100644 --- a/src/AbstractEntity.php +++ b/src/AbstractEntity.php @@ -8,8 +8,9 @@ use Intervention\Gif\Traits\CanDecode; use Intervention\Gif\Traits\CanEncode; use ReflectionClass; +use Stringable; -abstract class AbstractEntity +abstract class AbstractEntity implements Stringable { use CanEncode; use CanDecode; diff --git a/src/Blocks/Color.php b/src/Blocks/Color.php index 368160b..7d473ae 100644 --- a/src/Blocks/Color.php +++ b/src/Blocks/Color.php @@ -27,7 +27,7 @@ public function __construct( * * @return int */ - public function getRed() + public function getRed(): int { return $this->r; } @@ -49,7 +49,7 @@ public function setRed(int $value): self * * @return int */ - public function getGreen() + public function getGreen(): int { return $this->g; } @@ -71,7 +71,7 @@ public function setGreen(int $value): self * * @return int */ - public function getBlue() + public function getBlue(): int { return $this->b; } diff --git a/src/Blocks/ColorTable.php b/src/Blocks/ColorTable.php index e32a9b5..1a70f71 100644 --- a/src/Blocks/ColorTable.php +++ b/src/Blocks/ColorTable.php @@ -16,6 +16,7 @@ class ColorTable extends AbstractEntity */ public function __construct(protected array $colors = []) { + // } /** @@ -111,31 +112,16 @@ public function empty(): self */ public function getLogicalSize(): int { - switch ($this->countColors()) { - case 4: - return 1; - - case 8: - return 2; - - case 16: - return 3; - - case 32: - return 4; - - case 64: - return 5; - - case 128: - return 6; - - case 256: - return 7; - - default: - return 0; - } + return match ($this->countColors()) { + 4 => 1, + 8 => 2, + 16 => 3, + 32 => 4, + 64 => 5, + 128 => 6, + 256 => 7, + default => 0, + }; } /** diff --git a/src/Blocks/CommentExtension.php b/src/Blocks/CommentExtension.php index 74c3396..7c8a54e 100644 --- a/src/Blocks/CommentExtension.php +++ b/src/Blocks/CommentExtension.php @@ -20,9 +20,9 @@ class CommentExtension extends AbstractExtension /** * Get all or one comment * - * @return mixed + * @return array */ - public function getComments() + public function getComments(): array { return $this->comments; } diff --git a/src/Blocks/FrameBlock.php b/src/Blocks/FrameBlock.php index 176856c..b551669 100644 --- a/src/Blocks/FrameBlock.php +++ b/src/Blocks/FrameBlock.php @@ -37,49 +37,23 @@ public function __construct( protected ImageDescriptor $imageDescriptor = new ImageDescriptor(), protected ImageData $imageData = new ImageData() ) { + // } public function addEntity(AbstractEntity $entity): self { - switch (true) { - case $entity instanceof TableBasedImage: - $this->setTableBasedImage($entity); - break; - - case $entity instanceof GraphicControlExtension: - $this->setGraphicControlExtension($entity); - break; - - case $entity instanceof ImageDescriptor: - $this->setImageDescriptor($entity); - break; - - case $entity instanceof ColorTable: - $this->setColorTable($entity); - break; - - case $entity instanceof ImageData: - $this->setImageData($entity); - break; - - case $entity instanceof PlainTextExtension: - $this->setPlainTextExtension($entity); - break; - - case $entity instanceof NetscapeApplicationExtension: - $this->addApplicationExtension($entity); - break; - - case $entity instanceof ApplicationExtension: - $this->addApplicationExtension($entity); - break; - - case $entity instanceof CommentExtension: - $this->addCommentExtension($entity); - break; - } - - return $this; + return match (true) { + $entity instanceof TableBasedImage => $this->setTableBasedImage($entity), + $entity instanceof GraphicControlExtension => $this->setGraphicControlExtension($entity), + $entity instanceof ImageDescriptor => $this->setImageDescriptor($entity), + $entity instanceof ColorTable => $this->setColorTable($entity), + $entity instanceof ImageData => $this->setImageData($entity), + $entity instanceof PlainTextExtension => $this->setPlainTextExtension($entity), + $entity instanceof NetscapeApplicationExtension, + $entity instanceof ApplicationExtension => $this->addApplicationExtension($entity), + $entity instanceof CommentExtension => $this->addCommentExtension($entity), + default => $this, + }; } /** @@ -259,9 +233,10 @@ public function addCommentExtension(CommentExtension $extension): self */ public function getNetscapeExtension(): ?NetscapeApplicationExtension { - $extensions = array_filter($this->applicationExtensions, function ($extension) { - return $extension instanceof NetscapeApplicationExtension; - }); + $extensions = array_filter( + $this->applicationExtensions, + fn(ApplicationExtension $extension): bool => $extension instanceof NetscapeApplicationExtension, + ); return count($extensions) ? reset($extensions) : null; } diff --git a/src/Blocks/ImageDescriptor.php b/src/Blocks/ImageDescriptor.php index 22482ca..29e2f65 100644 --- a/src/Blocks/ImageDescriptor.php +++ b/src/Blocks/ImageDescriptor.php @@ -141,7 +141,7 @@ public function setPosition(int $left, int $top): self */ public function isInterlaced(): bool { - return $this->interlaced === true; + return $this->interlaced; } /** @@ -182,7 +182,7 @@ public function hasLocalColorTable(): bool * @param bool $existance * @return self */ - public function setLocalColorTableExistance($existance = true): self + public function setLocalColorTableExistance(bool $existance = true): self { $this->localColorTableExistance = $existance; @@ -205,7 +205,7 @@ public function getLocalColorTableSorted(): bool * @param bool $sorted * @return self */ - public function setLocalColorTableSorted($sorted = true): self + public function setLocalColorTableSorted(bool $sorted = true): self { $this->localColorTableSorted = $sorted; diff --git a/src/Blocks/LogicalScreenDescriptor.php b/src/Blocks/LogicalScreenDescriptor.php index f107e87..821f1b2 100644 --- a/src/Blocks/LogicalScreenDescriptor.php +++ b/src/Blocks/LogicalScreenDescriptor.php @@ -124,7 +124,7 @@ public function hasGlobalColorTable(): bool * @param bool $existance * @return self */ - public function setGlobalColorTableExistance($existance = true): self + public function setGlobalColorTableExistance(bool $existance = true): self { $this->globalColorTableExistance = $existance; @@ -147,7 +147,7 @@ public function getGlobalColorTableSorted(): bool * @param bool $sorted * @return self */ - public function setGlobalColorTableSorted($sorted = true): self + public function setGlobalColorTableSorted(bool $sorted = true): self { $this->globalColorTableSorted = $sorted; @@ -235,7 +235,7 @@ public function setPixelAspectRatio(int $ratio): self * * @return int */ - public function getBitsPerPixel() + public function getBitsPerPixel(): int { return $this->bitsPerPixel; } diff --git a/src/Blocks/PlainTextExtension.php b/src/Blocks/PlainTextExtension.php index c6f1995..6b102f9 100644 --- a/src/Blocks/PlainTextExtension.php +++ b/src/Blocks/PlainTextExtension.php @@ -58,6 +58,6 @@ public function setText(array $text): self */ public function hasText(): bool { - return count($this->text) > 0; + return $this->text !== []; } } diff --git a/src/Builder.php b/src/Builder.php index 91a9c26..8654951 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -73,7 +73,7 @@ public static function canvas(int $width, int $height): self */ public function setLoops(int $loops): self { - if (count($this->gif->getFrames()) === 0) { + if ($this->gif->getFrames() === []) { throw new Exception('Add at least one frame before setting the loop count'); } diff --git a/src/Decoders/GifDataStreamDecoder.php b/src/Decoders/GifDataStreamDecoder.php index 41b2f86..634a0a9 100644 --- a/src/Decoders/GifDataStreamDecoder.php +++ b/src/Decoders/GifDataStreamDecoder.php @@ -41,7 +41,7 @@ public function decode(): GifDataStream ); } - while ($this->viewNextByte() != Trailer::MARKER) { + while ($this->viewNextByte() !== Trailer::MARKER) { match ($this->viewNextBytes(2)) { // trailing "global" comment blocks which are not part of "FrameBlock" AbstractExtension::MARKER . CommentExtension::LABEL diff --git a/src/Encoders/ApplicationExtensionEncoder.php b/src/Encoders/ApplicationExtensionEncoder.php index 0992334..142ce3c 100644 --- a/src/Encoders/ApplicationExtensionEncoder.php +++ b/src/Encoders/ApplicationExtensionEncoder.php @@ -5,6 +5,8 @@ namespace Intervention\Gif\Encoders; use Intervention\Gif\Blocks\ApplicationExtension; +use Intervention\Gif\Blocks\DataSubBlock; +use Intervention\Gif\Exceptions\EncoderException; class ApplicationExtensionEncoder extends AbstractEncoder { @@ -21,6 +23,7 @@ public function __construct(ApplicationExtension $source) /** * Encode current source * + * @throws EncoderException * @return string */ public function encode(): string @@ -30,9 +33,7 @@ public function encode(): string ApplicationExtension::LABEL, pack('C', $this->source->getBlockSize()), $this->source->getApplication(), - implode('', array_map(function ($block) { - return $block->encode(); - }, $this->source->getBlocks())), + implode('', array_map(fn(DataSubBlock $block): string => $block->encode(), $this->source->getBlocks())), ApplicationExtension::TERMINATOR, ]); } diff --git a/src/Encoders/ColorTableEncoder.php b/src/Encoders/ColorTableEncoder.php index a3c4bff..a54dcd1 100644 --- a/src/Encoders/ColorTableEncoder.php +++ b/src/Encoders/ColorTableEncoder.php @@ -4,7 +4,9 @@ namespace Intervention\Gif\Encoders; +use Intervention\Gif\Blocks\Color; use Intervention\Gif\Blocks\ColorTable; +use Intervention\Gif\Exceptions\EncoderException; class ColorTableEncoder extends AbstractEncoder { @@ -21,12 +23,14 @@ public function __construct(ColorTable $source) /** * Encode current source * + * @throws EncoderException * @return string */ public function encode(): string { - return implode('', array_map(function ($color) { - return $color->encode(); - }, $this->source->getColors())); + return implode('', array_map( + fn(Color $color): string => $color->encode(), + $this->source->getColors(), + )); } } diff --git a/src/Encoders/CommentExtensionEncoder.php b/src/Encoders/CommentExtensionEncoder.php index 4cbea5a..0bd5631 100644 --- a/src/Encoders/CommentExtensionEncoder.php +++ b/src/Encoders/CommentExtensionEncoder.php @@ -40,7 +40,7 @@ public function encode(): string */ protected function encodeComments(): string { - return implode('', array_map(function ($comment) { + return implode('', array_map(function (string $comment): string { return pack('C', strlen($comment)) . $comment; }, $this->source->getComments())); } diff --git a/src/Encoders/FrameBlockEncoder.php b/src/Encoders/FrameBlockEncoder.php index ef244a1..b74f1c3 100644 --- a/src/Encoders/FrameBlockEncoder.php +++ b/src/Encoders/FrameBlockEncoder.php @@ -4,7 +4,10 @@ namespace Intervention\Gif\Encoders; +use Intervention\Gif\Blocks\ApplicationExtension; +use Intervention\Gif\Blocks\CommentExtension; use Intervention\Gif\Blocks\FrameBlock; +use Intervention\Gif\Exceptions\EncoderException; class FrameBlockEncoder extends AbstractEncoder { @@ -18,6 +21,9 @@ public function __construct(FrameBlock $source) $this->source = $source; } + /** + * @throws EncoderException + */ public function encode(): string { $graphicControlExtension = $this->source->getGraphicControlExtension(); @@ -25,12 +31,14 @@ public function encode(): string $plainTextExtension = $this->source->getPlainTextExtension(); return implode('', [ - implode('', array_map(function ($extension) { - return $extension->encode(); - }, $this->source->getApplicationExtensions())), - implode('', array_map(function ($extension) { - return $extension->encode(); - }, $this->source->getCommentExtensions())), + implode('', array_map( + fn(ApplicationExtension $extension): string => $extension->encode(), + $this->source->getApplicationExtensions(), + )), + implode('', array_map( + fn(CommentExtension $extension): string => $extension->encode(), + $this->source->getCommentExtensions(), + )), $plainTextExtension ? $plainTextExtension->encode() : '', $graphicControlExtension ? $graphicControlExtension->encode() : '', $this->source->getImageDescriptor()->encode(), diff --git a/src/Encoders/GifDataStreamEncoder.php b/src/Encoders/GifDataStreamEncoder.php index 16098f1..4e69138 100644 --- a/src/Encoders/GifDataStreamEncoder.php +++ b/src/Encoders/GifDataStreamEncoder.php @@ -4,6 +4,9 @@ namespace Intervention\Gif\Encoders; +use Intervention\Gif\Blocks\CommentExtension; +use Intervention\Gif\Blocks\FrameBlock; +use Intervention\Gif\Exceptions\EncoderException; use Intervention\Gif\GifDataStream; class GifDataStreamEncoder extends AbstractEncoder @@ -21,6 +24,7 @@ public function __construct(GifDataStream $source) /** * Encode current source * + * @throws EncoderException * @return string */ public function encode(): string @@ -47,24 +51,28 @@ protected function maybeEncodeGlobalColorTable(): string /** * Encode data blocks of source * + * @throws EncoderException * @return string */ protected function encodeFrames(): string { - return implode('', array_map(function ($frame) { - return $frame->encode(); - }, $this->source->getFrames())); + return implode('', array_map( + fn(FrameBlock $frame): string => $frame->encode(), + $this->source->getFrames(), + )); } /** * Encode comment extension blocks of source * + * @throws EncoderException * @return string */ protected function encodeComments(): string { - return implode('', array_map(function ($commentExtension) { - return $commentExtension->encode(); - }, $this->source->getComments())); + return implode('', array_map( + fn(CommentExtension $commentExtension): string => $commentExtension->encode(), + $this->source->getComments() + )); } } diff --git a/src/Encoders/ImageDataEncoder.php b/src/Encoders/ImageDataEncoder.php index 8a564d4..f601911 100644 --- a/src/Encoders/ImageDataEncoder.php +++ b/src/Encoders/ImageDataEncoder.php @@ -5,6 +5,7 @@ namespace Intervention\Gif\Encoders; use Intervention\Gif\AbstractEntity; +use Intervention\Gif\Blocks\DataSubBlock; use Intervention\Gif\Exceptions\EncoderException; use Intervention\Gif\Blocks\ImageData; @@ -34,9 +35,10 @@ public function encode(): string return implode('', [ pack('C', $this->source->getLzwMinCodeSize()), - implode('', array_map(function ($block) { - return $block->encode(); - }, $this->source->getBlocks())), + implode('', array_map( + fn(DataSubBlock $block): string => $block->encode(), + $this->source->getBlocks(), + )), AbstractEntity::TERMINATOR, ]); } diff --git a/src/Encoders/NetscapeApplicationExtensionEncoder.php b/src/Encoders/NetscapeApplicationExtensionEncoder.php index dae1607..a280e18 100644 --- a/src/Encoders/NetscapeApplicationExtensionEncoder.php +++ b/src/Encoders/NetscapeApplicationExtensionEncoder.php @@ -5,6 +5,7 @@ namespace Intervention\Gif\Encoders; use Intervention\Gif\Blocks\ApplicationExtension; +use Intervention\Gif\Blocks\DataSubBlock; use Intervention\Gif\Blocks\NetscapeApplicationExtension; class NetscapeApplicationExtensionEncoder extends ApplicationExtensionEncoder @@ -31,9 +32,7 @@ public function encode(): string ApplicationExtension::LABEL, pack('C', $this->source->getBlockSize()), $this->source->getApplication(), - implode('', array_map(function ($block) { - return $block->encode(); - }, $this->source->getBlocks())), + implode('', array_map(fn(DataSubBlock $block): string => $block->encode(), $this->source->getBlocks())), ApplicationExtension::TERMINATOR, ]); } diff --git a/src/Encoders/PlainTextExtensionEncoder.php b/src/Encoders/PlainTextExtensionEncoder.php index 54d448a..9ddd6fb 100644 --- a/src/Encoders/PlainTextExtensionEncoder.php +++ b/src/Encoders/PlainTextExtensionEncoder.php @@ -55,8 +55,9 @@ protected function encodeHead(): string */ protected function encodeTexts(): string { - return implode('', array_map(function ($text) { - return pack('C', strlen($text)) . $text; - }, $this->source->getText())); + return implode('', array_map( + fn(string $text): string => pack('C', strlen($text)) . $text, + $this->source->getText(), + )); } } diff --git a/src/Splitter.php b/src/Splitter.php index 3c25e97..6377093 100644 --- a/src/Splitter.php +++ b/src/Splitter.php @@ -36,6 +36,7 @@ class Splitter implements IteratorAggregate */ public function __construct(protected GifDataStream $stream) { + // } /** @@ -157,12 +158,10 @@ public function toResources(): array $resources = []; foreach ($this->frames as $frame) { - if (is_a($frame, GifDataStream::class)) { - $resource = imagecreatefromstring($frame->encode()); - imagepalettetotruecolor($resource); - imagesavealpha($resource, true); - $resources[] = $resource; - } + $resource = imagecreatefromstring($frame->encode()); + imagepalettetotruecolor($resource); + imagesavealpha($resource, true); + $resources[] = $resource; } return $resources; diff --git a/src/Traits/CanHandleFiles.php b/src/Traits/CanHandleFiles.php index 53385eb..21ce0c8 100644 --- a/src/Traits/CanHandleFiles.php +++ b/src/Traits/CanHandleFiles.php @@ -25,7 +25,7 @@ private static function isFilePath(mixed $input): bool */ private static function hasNullBytes(string $string): bool { - return strpos($string, chr(0)) !== false; + return str_contains($string, chr(0)); } /**