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

Feature/code modernization #1412

Merged
merged 6 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 @@ -26,7 +26,7 @@
"require-dev": {
"phpunit/phpunit": "^10.0 || ^11.0",
"mockery/mockery": "^1.6",
"phpstan/phpstan": "^2",
"phpstan/phpstan": "^2.1",
"squizlabs/php_codesniffer": "^3.8",
"slevomat/coding-standard": "~8.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function get(int|string $query, $default = null): mixed
return $this->items[$query];
}

if (is_string($query) && strpos($query, '.') === false) {
if (is_string($query) && !str_contains($query, '.')) {
return array_key_exists($query, $this->items) ? $this->items[$query] : $default;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Colors/AbstractColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
use ReflectionClass;
use Stringable;

abstract class AbstractColor implements ColorInterface
abstract class AbstractColor implements ColorInterface, Stringable
{
/**
* Color channels
Expand Down Expand Up @@ -38,7 +39,7 @@ public function channel(string $classname): ColorChannelInterface
{
$channels = array_filter(
$this->channels(),
fn(ColorChannelInterface $channel) => $channel::class == $classname,
fn(ColorChannelInterface $channel): bool => $channel::class === $classname,
);

if (count($channels) == 0) {
Expand All @@ -56,7 +57,7 @@ public function channel(string $classname): ColorChannelInterface
public function normalize(): array
{
return array_map(
fn(ColorChannelInterface $channel) => $channel->normalize(),
fn(ColorChannelInterface $channel): float => $channel->normalize(),
$this->channels(),
);
}
Expand All @@ -69,7 +70,7 @@ public function normalize(): array
public function toArray(): array
{
return array_map(
fn(ColorChannelInterface $channel) => $channel->value(),
fn(ColorChannelInterface $channel): int => $channel->value(),
$this->channels()
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Colors/AbstractColorChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorChannelInterface;
use Stringable;

abstract class AbstractColorChannel implements ColorChannelInterface
abstract class AbstractColorChannel implements ColorChannelInterface, Stringable
{
protected int $value;

Expand Down
2 changes: 1 addition & 1 deletion src/Colors/Cmyk/Decoders/StringColorDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function decode(mixed $input): ImageInterface|ColorInterface
throw new DecoderException('Unable to decode input');
}

$values = array_map(function ($value) {
$values = array_map(function (string $value): int {
return intval(round(floatval(trim(str_replace('%', '', $value)))));
}, [$matches['c'], $matches['m'], $matches['y'], $matches['k']]);

Expand Down
8 changes: 4 additions & 4 deletions src/Colors/Hsl/Colorspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function importRgbColor(ColorInterface $color): ColorInterface

// normalized values of rgb channels
$values = array_map(
fn(ColorChannelInterface $channel) => $channel->normalize(),
fn(ColorChannelInterface $channel): float => $channel->normalize(),
$color->channels(),
);

Expand All @@ -88,7 +88,7 @@ protected function importRgbColor(ColorInterface $color): ColorInterface
};

// calculate hue
list($r, $g, $b) = $values;
[$r, $g, $b] = $values;
$hue = match (true) {
($delta == 0) => 0,
($max == $r) => 60 * fmod((($g - $b) / $delta), 6),
Expand Down Expand Up @@ -118,8 +118,8 @@ protected function importHsvColor(ColorInterface $color): ColorInterface
}

// normalized values of hsv channels
list($h, $s, $v) = array_map(
fn(ColorChannelInterface $channel) => $channel->normalize(),
[$h, $s, $v] = array_map(
fn(ColorChannelInterface $channel): float => $channel->normalize(),
$color->channels(),
);

Expand Down
2 changes: 1 addition & 1 deletion src/Colors/Hsl/Decoders/StringColorDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function decode(mixed $input): ImageInterface|ColorInterface
throw new DecoderException('Unable to decode input');
}

$values = array_map(function ($value) {
$values = array_map(function (string $value): int {
return match (strpos($value, '%')) {
false => intval(trim($value)),
default => intval(trim(str_replace('%', '', $value))),
Expand Down
9 changes: 6 additions & 3 deletions src/Colors/Hsv/Colorspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function importRgbColor(ColorInterface $color): ColorInterface
}

// normalized values of rgb channels
$values = array_map(fn(ColorChannelInterface $channel) => $channel->normalize(), $color->channels());
$values = array_map(fn(ColorChannelInterface $channel): float => $channel->normalize(), $color->channels());

// take only RGB
$values = array_slice($values, 0, 3);
Expand All @@ -89,7 +89,7 @@ protected function importRgbColor(ColorInterface $color): ColorInterface
$s = 100 * ($chroma / $max);

// calculate hue
list($r, $g, $b) = $values;
[$r, $g, $b] = $values;
$h = match (true) {
($r == $min) => 3 - (($g - $b) / $chroma),
($b == $min) => 1 - (($r - $g) / $chroma),
Expand All @@ -115,7 +115,10 @@ protected function importHslColor(ColorInterface $color): ColorInterface
}

// normalized values of hsl channels
list($h, $s, $l) = array_map(fn(ColorChannelInterface $channel) => $channel->normalize(), $color->channels());
[$h, $s, $l] = array_map(
fn(ColorChannelInterface $channel): float => $channel->normalize(),
$color->channels()
);

$v = $l + $s * min($l, 1 - $l);
$s = ($v == 0) ? 0 : 2 * (1 - $l / $v);
Expand Down
2 changes: 1 addition & 1 deletion src/Colors/Hsv/Decoders/StringColorDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function decode(mixed $input): ImageInterface|ColorInterface
throw new DecoderException('Unable to decode input');
}

$values = array_map(function ($value) {
$values = array_map(function (string $value): int {
return match (strpos($value, '%')) {
false => intval(trim($value)),
default => intval(trim(str_replace('%', '', $value))),
Expand Down
14 changes: 6 additions & 8 deletions src/Colors/Rgb/Colorspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ protected function importHsvColor(ColorInterface $color): ColorInterface
};

// add to each value
$values = array_map(fn($value) => $value + $color->value()->normalize() - $chroma, $values);

array_push($values, 1); // append alpha channel value
$values = array_map(fn(float|int $value): float => $value + $color->value()->normalize() - $chroma, $values);
$values[] = 1; // append alpha channel value

return $this->colorFromNormalized($values);
}
Expand All @@ -118,8 +117,8 @@ protected function importHslColor(ColorInterface $color): ColorInterface
}

// normalized values of hsl channels
list($h, $s, $l) = array_map(
fn(ColorChannelInterface $channel) => $channel->normalize(),
[$h, $s, $l] = array_map(
fn(ColorChannelInterface $channel): float => $channel->normalize(),
$color->channels()
);

Expand All @@ -136,9 +135,8 @@ protected function importHslColor(ColorInterface $color): ColorInterface
default => [$c, 0, $x],
};

$values = array_map(fn($value) => $value + $m, $values);

array_push($values, 1); // append alpha channel value
$values = array_map(fn(float|int $value): float => $value + $m, $values);
$values[] = 1; // append alpha channel value

return $this->colorFromNormalized($values);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Colors/Rgb/Decoders/HexColorDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ public function decode(mixed $input): ImageInterface|ColorInterface
throw new DecoderException('Unable to decode input');
}

$values = str_split($matches['hex']);
$values = match (strlen($matches['hex'])) {
3, 4 => str_split($matches['hex']),
6, 8 => str_split($matches['hex'], 2),
default => throw new DecoderException('Unable to decode input'),
};

$values = array_map(function ($value) {
$values = array_map(function (string $value): float|int {
return match (strlen($value)) {
1 => hexdec($value . $value),
2 => hexdec($value),
Expand Down
2 changes: 1 addition & 1 deletion src/Colors/Rgb/Decoders/StringColorDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function decode(mixed $input): ImageInterface|ColorInterface
}

// rgb values
$values = array_map(function ($value) {
$values = array_map(function (string $value): int {
return match (strpos($value, '%')) {
false => intval(trim($value)),
default => intval(round(floatval(trim(str_replace('%', '', $value))) / 100 * 255)),
Expand Down
2 changes: 1 addition & 1 deletion src/Colors/Rgb/Decoders/TransparentColorDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function decode(mixed $input): ImageInterface|ColorInterface
throw new DecoderException('Unable to decode input');
}

if (strtolower($input) != 'transparent') {
if (strtolower($input) !== 'transparent') {
throw new DecoderException('Unable to decode input');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function setOptions(mixed ...$options): self
*/
private function prepareOptions(array $options): array
{
if (count($options) === 0) {
if ($options === []) {
return $options;
}

Expand Down
19 changes: 4 additions & 15 deletions src/Drivers/AbstractDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,18 @@ protected function parseDataUri(mixed $input): object
$pattern = "/^data:(?P<mediatype>\w+\/[-+.\w]+)?" .
"(?P<parameters>(;[-\w]+=[-\w]+)*)(?P<base64>;base64)?,(?P<data>.*)/";

$result = preg_match($pattern, $input, $matches);
$result = preg_match($pattern, (string) $input, $matches);

return new class ($matches, $result)
{
/**
* @var array<mixed>
*/
private array $matches;
private int|false $result;

/**
* @param array<mixed> $matches
* @param int|false $result
* @return void
*/
public function __construct(array $matches, int|false $result)
public function __construct(private array $matches, private int|false $result)
{
$this->matches = $matches;
$this->result = $result;
//
}

public function isValid(): bool
Expand All @@ -154,11 +147,7 @@ public function hasMediaType(): bool

public function isBase64Encoded(): bool
{
if (isset($this->matches['base64']) && $this->matches['base64'] === ';base64') {
return true;
}

return false;
return isset($this->matches['base64']) && $this->matches['base64'] === ';base64';
}

public function data(): ?string
Expand Down
19 changes: 11 additions & 8 deletions src/Drivers/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,16 @@ public function specialize(
*/
public function specializeMultiple(array $objects): array
{
return array_map(function ($object) {
return $this->specialize(
match (true) {
is_string($object) => new $object(),
is_object($object) => $object,
}
);
}, $objects);
return array_map(
function (string|object $object): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface {
return $this->specialize(
match (true) {
is_string($object) => new $object(),
is_object($object) => $object,
}
);
},
$objects
);
}
}
10 changes: 5 additions & 5 deletions src/Drivers/AbstractFontProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function textBlock(string $text, FontInterface $font, PointInterface $pos
foreach ($lines as $line) {
$lineBoxSize = $this->boxSize((string) $line, $font);
$lineWidth = $lineBoxSize->width() + $lineBoxSize->pivot()->x();
$xAdjustment = $font->alignment() == 'left' ? 0 : $blockWidth - $lineWidth;
$xAdjustment = $font->alignment() == 'right' ? intval(round($xAdjustment)) : $xAdjustment;
$xAdjustment = $font->alignment() == 'center' ? intval(round($xAdjustment / 2)) : $xAdjustment;
$xAdjustment = $font->alignment() === 'left' ? 0 : $blockWidth - $lineWidth;
$xAdjustment = $font->alignment() === 'right' ? intval(round($xAdjustment)) : $xAdjustment;
$xAdjustment = $font->alignment() === 'center' ? intval(round($xAdjustment / 2)) : $xAdjustment;
$position = new Point($x + $xAdjustment, $y);
$position->rotate($font->angle(), $pivot);
$line->setPosition($position);
Expand Down Expand Up @@ -132,14 +132,14 @@ protected function wrapLine(Line $line, FontInterface $font): array
// calculate width of newly formatted line
$lineWidth = $this->boxSize(match ($formattedLine->count()) {
0 => $word,
default => (string) $formattedLine . ' ' . $word,
default => $formattedLine . ' ' . $word,
}, $font)->width();

// decide if word fits on current line or a new line must be created
if ($line->count() === 1 || $lineWidth <= $font->wrapWidth()) {
$formattedLine->add($word);
} else {
if ($formattedLine->count()) {
if ($formattedLine->count() !== 0) {
$wrapped[] = $formattedLine;
}
$formattedLine = new Line($word);
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Decoders/Base64ImageDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function decode(mixed $input): ImageInterface|ColorInterface
throw new DecoderException('Unable to decode input');
}

return parent::decode(base64_decode($input));
return parent::decode(base64_decode((string) $input));
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/AvifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AvifEncoder extends GenericAvifEncoder implements SpecializedInterface
*/
public function encode(ImageInterface $image): EncodedImage
{
return $this->createEncodedImage(function ($pointer) use ($image) {
return $this->createEncodedImage(function ($pointer) use ($image): void {
imageavif($image->core()->native(), $pointer, $this->quality);
}, 'image/avif');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/BmpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BmpEncoder extends GenericBmpEncoder implements SpecializedInterface
*/
public function encode(ImageInterface $image): EncodedImage
{
return $this->createEncodedImage(function ($pointer) use ($image) {
return $this->createEncodedImage(function ($pointer) use ($image): void {
imagebmp($image->core()->native(), $pointer, false);
}, 'image/bmp');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/GifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function encode(ImageInterface $image): EncodedImage

$gd = Cloner::clone($image->core()->native());

return $this->createEncodedImage(function ($pointer) use ($gd) {
return $this->createEncodedImage(function ($pointer) use ($gd): void {
imageinterlace($gd, $this->interlaced);
imagegif($gd, $pointer);
}, 'image/gif');
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/JpegEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function encode(ImageInterface $image): EncodedImage
background: $blendingColor
);

return $this->createEncodedImage(function ($pointer) use ($output) {
return $this->createEncodedImage(function ($pointer) use ($output): void {
imageinterlace($output, $this->progressive);
imagejpeg($output, $pointer, $this->quality);
}, 'image/jpeg');
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function encode(ImageInterface $image): EncodedImage
{
$output = $this->prepareOutput($image);

return $this->createEncodedImage(function ($pointer) use ($output) {
return $this->createEncodedImage(function ($pointer) use ($output): void {
imageinterlace($output, $this->interlaced);
imagepng($output, $pointer, -1);
}, 'image/png');
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Encoders/WebpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function encode(ImageInterface $image): EncodedImage
{
$quality = $this->quality === 100 ? IMG_WEBP_LOSSLESS : $this->quality;

return $this->createEncodedImage(function ($pointer) use ($image, $quality) {
return $this->createEncodedImage(function ($pointer) use ($image, $quality): void {
imagewebp($image->core()->native(), $pointer, $quality);
}, 'image/webp');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Modifiers/ColorspaceModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ColorspaceModifier extends GenericColorspaceModifier implements Specialize
*/
public function apply(ImageInterface $image): ImageInterface
{
if (!is_a($this->targetColorspace(), RgbColorspace::class)) {
if (!($this->targetColorspace() instanceof RgbColorspace)) {
throw new NotSupportedException(
'Only RGB colorspace is supported by GD driver.'
);
Expand Down
Loading
Loading