Skip to content

Commit

Permalink
Modernize code
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Jan 5, 2025
1 parent 4295eff commit 53656d2
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 94 deletions.
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
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
2 changes: 1 addition & 1 deletion src/Blocks/CommentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CommentExtension extends AbstractExtension
/**
* Get all or one comment
*
* @return array
* @return array<string>
*/
public function getComments(): array
{
Expand Down
56 changes: 17 additions & 39 deletions src/Blocks/FrameBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +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:
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 @@ -256,9 +233,10 @@ public function addCommentExtension(CommentExtension $extension): self
*/
public function getNetscapeExtension(): ?NetscapeApplicationExtension
{
$extensions = array_filter($this->applicationExtensions, function (ApplicationExtension $extension): bool {
return $extension instanceof NetscapeApplicationExtension;
});
$extensions = array_filter(
$this->applicationExtensions,
fn(ApplicationExtension $extension): bool => $extension instanceof NetscapeApplicationExtension,
);

return count($extensions) ? reset($extensions) : null;
}
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(),
));
}
}
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
20 changes: 14 additions & 6 deletions src/Encoders/GifDataStreamEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,6 +24,7 @@ public function __construct(GifDataStream $source)
/**
* Encode current source
*
* @throws EncoderException
* @return string
*/
public function encode(): string
Expand All @@ -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()
));
}
}
8 changes: 5 additions & 3 deletions src/Encoders/ImageDataEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
]);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Encoders/NetscapeApplicationExtensionEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
]);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Encoders/PlainTextExtensionEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ protected function encodeHead(): string
*/
protected function encodeTexts(): string
{
return implode('', array_map(function (string $text): string {
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(),
));
}
}
2 changes: 1 addition & 1 deletion src/Traits/CanHandleFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit 53656d2

Please sign in to comment.