Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize code #18

Merged
merged 4 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
3 changes: 2 additions & 1 deletion src/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Blocks/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
*
* @return int
*/
public function getRed()
public function getRed(): int
{
return $this->r;
}
Expand All @@ -49,7 +49,7 @@ public function setRed(int $value): self
*
* @return int
*/
public function getGreen()
public function getGreen(): int
{
return $this->g;
}
Expand All @@ -71,7 +71,7 @@ public function setGreen(int $value): self
*
* @return int
*/
public function getBlue()
public function getBlue(): int
{
return $this->b;
}
Expand Down
36 changes: 11 additions & 25 deletions src/Blocks/ColorTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ColorTable extends AbstractEntity
*/
public function __construct(protected array $colors = [])
{
//
}

/**
Expand Down Expand Up @@ -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,
};
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Blocks/CommentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class CommentExtension extends AbstractExtension
/**
* Get all or one comment
*
* @return mixed
* @return array<string>
*/
public function getComments()
public function getComments(): array
{
return $this->comments;
}
Expand Down
59 changes: 17 additions & 42 deletions src/Blocks/FrameBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Blocks/ImageDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function setPosition(int $left, int $top): self
*/
public function isInterlaced(): bool
{
return $this->interlaced === true;
return $this->interlaced;
}

/**
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/Blocks/LogicalScreenDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -235,7 +235,7 @@ public function setPixelAspectRatio(int $ratio): self
*
* @return int
*/
public function getBitsPerPixel()
public function getBitsPerPixel(): int
{
return $this->bitsPerPixel;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Blocks/PlainTextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ public function setText(array $text): self
*/
public function hasText(): bool
{
return count($this->text) > 0;
return $this->text !== [];
}
}
2 changes: 1 addition & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Decoders/GifDataStreamDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/Encoders/ApplicationExtensionEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -21,6 +23,7 @@ public function __construct(ApplicationExtension $source)
/**
* Encode current source
*
* @throws EncoderException
* @return string
*/
public function encode(): string
Expand All @@ -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,
]);
}
Expand Down
10 changes: 7 additions & 3 deletions src/Encoders/ColorTableEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(),
));
}
}
2 changes: 1 addition & 1 deletion src/Encoders/CommentExtensionEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
20 changes: 14 additions & 6 deletions src/Encoders/FrameBlockEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -18,19 +21,24 @@ public function __construct(FrameBlock $source)
$this->source = $source;
}

/**
* @throws EncoderException
*/
public function encode(): string
{
$graphicControlExtension = $this->source->getGraphicControlExtension();
$colorTable = $this->source->getColorTable();
$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(),
Expand Down
Loading
Loading