From c837fc9ffe8399fcdf283ee9a4335c20cde705e7 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 5 Jan 2025 10:43:14 +0100 Subject: [PATCH] Feature/code modernization (#1412) * Update phpstan dependency version * Remove unused code * Optimize code * Add type hints * Change property visibility on final classes * Modernize code --- composer.json | 2 +- src/Collection.php | 2 +- src/Colors/AbstractColor.php | 9 +++--- src/Colors/AbstractColorChannel.php | 3 +- .../Cmyk/Decoders/StringColorDecoder.php | 2 +- src/Colors/Hsl/Colorspace.php | 8 +++--- .../Hsl/Decoders/StringColorDecoder.php | 2 +- src/Colors/Hsv/Colorspace.php | 9 ++++-- .../Hsv/Decoders/StringColorDecoder.php | 2 +- src/Colors/Rgb/Colorspace.php | 14 ++++------ src/Colors/Rgb/Decoders/HexColorDecoder.php | 3 +- .../Rgb/Decoders/StringColorDecoder.php | 2 +- .../Rgb/Decoders/TransparentColorDecoder.php | 2 +- src/Config.php | 2 +- src/Drivers/AbstractDecoder.php | 19 +++---------- src/Drivers/AbstractDriver.php | 19 +++++++------ src/Drivers/AbstractFontProcessor.php | 10 +++---- .../Gd/Decoders/Base64ImageDecoder.php | 2 +- src/Drivers/Gd/Encoders/AvifEncoder.php | 2 +- src/Drivers/Gd/Encoders/BmpEncoder.php | 2 +- src/Drivers/Gd/Encoders/GifEncoder.php | 2 +- src/Drivers/Gd/Encoders/JpegEncoder.php | 2 +- src/Drivers/Gd/Encoders/PngEncoder.php | 2 +- src/Drivers/Gd/Encoders/WebpEncoder.php | 2 +- .../Gd/Modifiers/ColorspaceModifier.php | 2 +- .../Gd/Modifiers/DrawBezierModifier.php | 9 +++--- src/Drivers/Imagick/Core.php | 4 +-- .../Imagick/Decoders/Base64ImageDecoder.php | 2 +- .../Imagick/Decoders/NativeObjectDecoder.php | 2 +- src/Drivers/Imagick/FontProcessor.php | 2 +- .../Imagick/Modifiers/BlurModifier.php | 2 +- .../Imagick/Modifiers/TextModifier.php | 2 +- src/File.php | 7 +++-- src/Format.php | 8 +++--- src/Geometry/Polygon.php | 28 ++++++------------- src/Geometry/Rectangle.php | 12 ++++---- src/Geometry/Tools/RectangleResizer.php | 4 +-- src/Image.php | 8 +++--- src/ImageManager.php | 2 +- src/Modifiers/FillModifier.php | 2 +- src/Modifiers/RemoveAnimationModifier.php | 3 +- src/Resolution.php | 3 +- src/Typography/Line.php | 3 +- src/Typography/TextBlock.php | 2 +- 44 files changed, 108 insertions(+), 123 deletions(-) diff --git a/composer.json b/composer.json index 04090e76..cc42f8f6 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/src/Collection.php b/src/Collection.php index ad49637f..539855a8 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -153,7 +153,7 @@ class Collection implements CollectionInterface, IteratorAggregate, Countable 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; } diff --git a/src/Colors/AbstractColor.php b/src/Colors/AbstractColor.php index bcb7e269..9628795e 100644 --- a/src/Colors/AbstractColor.php +++ b/src/Colors/AbstractColor.php @@ -9,8 +9,9 @@ use Intervention\Image\Interfaces\ColorChannelInterface; 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 @@ -38,7 +39,7 @@ abstract class AbstractColor implements ColorInterface { $channels = array_filter( $this->channels(), - fn(ColorChannelInterface $channel) => $channel::class == $classname, + fn(ColorChannelInterface $channel): bool => $channel::class === $classname, ); if (count($channels) == 0) { @@ -56,7 +57,7 @@ abstract class AbstractColor implements ColorInterface public function normalize(): array { return array_map( - fn(ColorChannelInterface $channel) => $channel->normalize(), + fn(ColorChannelInterface $channel): float => $channel->normalize(), $this->channels(), ); } @@ -69,7 +70,7 @@ abstract class AbstractColor implements ColorInterface public function toArray(): array { return array_map( - fn(ColorChannelInterface $channel) => $channel->value(), + fn(ColorChannelInterface $channel): int => $channel->value(), $this->channels() ); } diff --git a/src/Colors/AbstractColorChannel.php b/src/Colors/AbstractColorChannel.php index b883db2b..d415f7b6 100644 --- a/src/Colors/AbstractColorChannel.php +++ b/src/Colors/AbstractColorChannel.php @@ -6,8 +6,9 @@ namespace Intervention\Image\Colors; 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; diff --git a/src/Colors/Cmyk/Decoders/StringColorDecoder.php b/src/Colors/Cmyk/Decoders/StringColorDecoder.php index 2775f0e5..f0a67c7a 100644 --- a/src/Colors/Cmyk/Decoders/StringColorDecoder.php +++ b/src/Colors/Cmyk/Decoders/StringColorDecoder.php @@ -30,7 +30,7 @@ class StringColorDecoder extends AbstractDecoder implements DecoderInterface 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']]); diff --git a/src/Colors/Hsl/Colorspace.php b/src/Colors/Hsl/Colorspace.php index 84914736..431f3980 100644 --- a/src/Colors/Hsl/Colorspace.php +++ b/src/Colors/Hsl/Colorspace.php @@ -68,7 +68,7 @@ class Colorspace implements ColorspaceInterface // normalized values of rgb channels $values = array_map( - fn(ColorChannelInterface $channel) => $channel->normalize(), + fn(ColorChannelInterface $channel): float => $channel->normalize(), $color->channels(), ); @@ -88,7 +88,7 @@ class Colorspace implements ColorspaceInterface }; // 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), @@ -118,8 +118,8 @@ class Colorspace implements ColorspaceInterface } // 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(), ); diff --git a/src/Colors/Hsl/Decoders/StringColorDecoder.php b/src/Colors/Hsl/Decoders/StringColorDecoder.php index 779df96f..21bb96aa 100644 --- a/src/Colors/Hsl/Decoders/StringColorDecoder.php +++ b/src/Colors/Hsl/Decoders/StringColorDecoder.php @@ -30,7 +30,7 @@ class StringColorDecoder extends AbstractDecoder implements DecoderInterface 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))), diff --git a/src/Colors/Hsv/Colorspace.php b/src/Colors/Hsv/Colorspace.php index 88c8f221..132c88a9 100644 --- a/src/Colors/Hsv/Colorspace.php +++ b/src/Colors/Hsv/Colorspace.php @@ -67,7 +67,7 @@ class Colorspace implements ColorspaceInterface } // 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); @@ -89,7 +89,7 @@ class Colorspace implements ColorspaceInterface $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), @@ -115,7 +115,10 @@ class Colorspace implements ColorspaceInterface } // 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); diff --git a/src/Colors/Hsv/Decoders/StringColorDecoder.php b/src/Colors/Hsv/Decoders/StringColorDecoder.php index 9a4d2645..aad50eec 100644 --- a/src/Colors/Hsv/Decoders/StringColorDecoder.php +++ b/src/Colors/Hsv/Decoders/StringColorDecoder.php @@ -30,7 +30,7 @@ class StringColorDecoder extends AbstractDecoder implements DecoderInterface 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))), diff --git a/src/Colors/Rgb/Colorspace.php b/src/Colors/Rgb/Colorspace.php index bf1ff2d9..9d963ecb 100644 --- a/src/Colors/Rgb/Colorspace.php +++ b/src/Colors/Rgb/Colorspace.php @@ -99,9 +99,8 @@ class Colorspace implements ColorspaceInterface }; // 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); } @@ -118,8 +117,8 @@ class Colorspace implements ColorspaceInterface } // 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() ); @@ -136,9 +135,8 @@ class Colorspace implements ColorspaceInterface 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); } diff --git a/src/Colors/Rgb/Decoders/HexColorDecoder.php b/src/Colors/Rgb/Decoders/HexColorDecoder.php index 36bf62c5..2900bc57 100644 --- a/src/Colors/Rgb/Decoders/HexColorDecoder.php +++ b/src/Colors/Rgb/Decoders/HexColorDecoder.php @@ -30,14 +30,13 @@ class HexColorDecoder extends AbstractDecoder implements DecoderInterface 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), diff --git a/src/Colors/Rgb/Decoders/StringColorDecoder.php b/src/Colors/Rgb/Decoders/StringColorDecoder.php index 38f977d1..b28ee3bd 100644 --- a/src/Colors/Rgb/Decoders/StringColorDecoder.php +++ b/src/Colors/Rgb/Decoders/StringColorDecoder.php @@ -32,7 +32,7 @@ class StringColorDecoder extends AbstractDecoder implements DecoderInterface } // 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)), diff --git a/src/Colors/Rgb/Decoders/TransparentColorDecoder.php b/src/Colors/Rgb/Decoders/TransparentColorDecoder.php index f818b790..8d968a66 100644 --- a/src/Colors/Rgb/Decoders/TransparentColorDecoder.php +++ b/src/Colors/Rgb/Decoders/TransparentColorDecoder.php @@ -21,7 +21,7 @@ class TransparentColorDecoder extends HexColorDecoder throw new DecoderException('Unable to decode input'); } - if (strtolower($input) != 'transparent') { + if (strtolower($input) !== 'transparent') { throw new DecoderException('Unable to decode input'); } diff --git a/src/Config.php b/src/Config.php index 4b69209e..da8edf01 100644 --- a/src/Config.php +++ b/src/Config.php @@ -52,7 +52,7 @@ class Config */ private function prepareOptions(array $options): array { - if (count($options) === 0) { + if ($options === []) { return $options; } diff --git a/src/Drivers/AbstractDecoder.php b/src/Drivers/AbstractDecoder.php index 27fce6e3..46567171 100644 --- a/src/Drivers/AbstractDecoder.php +++ b/src/Drivers/AbstractDecoder.php @@ -112,25 +112,18 @@ abstract class AbstractDecoder implements DecoderInterface $pattern = "/^data:(?P\w+\/[-+.\w]+)?" . "(?P(;[-\w]+=[-\w]+)*)(?P;base64)?,(?P.*)/"; - $result = preg_match($pattern, $input, $matches); + $result = preg_match($pattern, (string) $input, $matches); return new class ($matches, $result) { - /** - * @var array - */ - private array $matches; - private int|false $result; - /** * @param array $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 @@ -154,11 +147,7 @@ abstract class AbstractDecoder implements DecoderInterface 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 diff --git a/src/Drivers/AbstractDriver.php b/src/Drivers/AbstractDriver.php index 781446e2..17f2f530 100644 --- a/src/Drivers/AbstractDriver.php +++ b/src/Drivers/AbstractDriver.php @@ -103,13 +103,16 @@ abstract class AbstractDriver implements DriverInterface */ 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 + ); } } diff --git a/src/Drivers/AbstractFontProcessor.php b/src/Drivers/AbstractFontProcessor.php index 39203ed1..af984c95 100644 --- a/src/Drivers/AbstractFontProcessor.php +++ b/src/Drivers/AbstractFontProcessor.php @@ -36,9 +36,9 @@ abstract class AbstractFontProcessor implements FontProcessorInterface 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); @@ -132,14 +132,14 @@ abstract class AbstractFontProcessor implements FontProcessorInterface // 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); diff --git a/src/Drivers/Gd/Decoders/Base64ImageDecoder.php b/src/Drivers/Gd/Decoders/Base64ImageDecoder.php index 7b3686ad..2c1e053b 100644 --- a/src/Drivers/Gd/Decoders/Base64ImageDecoder.php +++ b/src/Drivers/Gd/Decoders/Base64ImageDecoder.php @@ -22,6 +22,6 @@ class Base64ImageDecoder extends BinaryImageDecoder implements DecoderInterface throw new DecoderException('Unable to decode input'); } - return parent::decode(base64_decode($input)); + return parent::decode(base64_decode((string) $input)); } } diff --git a/src/Drivers/Gd/Encoders/AvifEncoder.php b/src/Drivers/Gd/Encoders/AvifEncoder.php index f7106a7e..8b642719 100644 --- a/src/Drivers/Gd/Encoders/AvifEncoder.php +++ b/src/Drivers/Gd/Encoders/AvifEncoder.php @@ -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'); } diff --git a/src/Drivers/Gd/Encoders/BmpEncoder.php b/src/Drivers/Gd/Encoders/BmpEncoder.php index 7e9a52d7..1bb34c2c 100644 --- a/src/Drivers/Gd/Encoders/BmpEncoder.php +++ b/src/Drivers/Gd/Encoders/BmpEncoder.php @@ -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'); } diff --git a/src/Drivers/Gd/Encoders/GifEncoder.php b/src/Drivers/Gd/Encoders/GifEncoder.php index 73223091..29d97ecc 100644 --- a/src/Drivers/Gd/Encoders/GifEncoder.php +++ b/src/Drivers/Gd/Encoders/GifEncoder.php @@ -29,7 +29,7 @@ class GifEncoder extends GenericGifEncoder implements SpecializedInterface $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'); diff --git a/src/Drivers/Gd/Encoders/JpegEncoder.php b/src/Drivers/Gd/Encoders/JpegEncoder.php index b686a9cd..0e3ecb88 100644 --- a/src/Drivers/Gd/Encoders/JpegEncoder.php +++ b/src/Drivers/Gd/Encoders/JpegEncoder.php @@ -28,7 +28,7 @@ class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface 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'); diff --git a/src/Drivers/Gd/Encoders/PngEncoder.php b/src/Drivers/Gd/Encoders/PngEncoder.php index d6263b63..658ddb6a 100644 --- a/src/Drivers/Gd/Encoders/PngEncoder.php +++ b/src/Drivers/Gd/Encoders/PngEncoder.php @@ -25,7 +25,7 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface { $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'); diff --git a/src/Drivers/Gd/Encoders/WebpEncoder.php b/src/Drivers/Gd/Encoders/WebpEncoder.php index d0f7b876..e7d344a1 100644 --- a/src/Drivers/Gd/Encoders/WebpEncoder.php +++ b/src/Drivers/Gd/Encoders/WebpEncoder.php @@ -20,7 +20,7 @@ class WebpEncoder extends GenericWebpEncoder implements SpecializedInterface { $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'); } diff --git a/src/Drivers/Gd/Modifiers/ColorspaceModifier.php b/src/Drivers/Gd/Modifiers/ColorspaceModifier.php index a94f82dd..2065a155 100644 --- a/src/Drivers/Gd/Modifiers/ColorspaceModifier.php +++ b/src/Drivers/Gd/Modifiers/ColorspaceModifier.php @@ -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.' ); diff --git a/src/Drivers/Gd/Modifiers/DrawBezierModifier.php b/src/Drivers/Gd/Modifiers/DrawBezierModifier.php index d8da4937..4e3c49f6 100644 --- a/src/Drivers/Gd/Modifiers/DrawBezierModifier.php +++ b/src/Drivers/Gd/Modifiers/DrawBezierModifier.php @@ -26,7 +26,7 @@ class DrawBezierModifier extends ModifiersDrawBezierModifier implements Speciali throw new GeometryException('You must specify either 3 or 4 points to create a bezier curve'); } - list($polygon, $polygon_border_segments) = $this->calculateBezierPoints(); + [$polygon, $polygon_border_segments] = $this->calculateBezierPoints(); if ($this->drawable->hasBackgroundColor() || $this->drawable->hasBorder()) { imagealphablending($frame->native(), true); @@ -54,7 +54,8 @@ class DrawBezierModifier extends ModifiersDrawBezierModifier implements Speciali if ($this->drawable->borderSize() === 1) { imagesetthickness($frame->native(), $this->drawable->borderSize()); - for ($i = 0; $i < count($polygon); $i += 2) { + $count = count($polygon); + for ($i = 0; $i < $count; $i += 2) { if (array_key_exists($i + 2, $polygon) && array_key_exists($i + 3, $polygon)) { imageline( $frame->native(), @@ -160,11 +161,11 @@ class DrawBezierModifier extends ModifiersDrawBezierModifier implements Speciali $polygon_border_segments = []; // define ratio t; equivalent to 5 percent distance along edge - $t = (float) 0.05; + $t = 0.05; $polygon[] = $this->drawable->first()->x(); $polygon[] = $this->drawable->first()->y(); - for ($i = 0 + $t; $i < 1; $i += $t) { + for ($i = $t; $i < 1; $i += $t) { if ($this->drawable->count() === 3) { $ip = $this->calculateQuadraticBezierInterpolationPoint($i); } elseif ($this->drawable->count() === 4) { diff --git a/src/Drivers/Imagick/Core.php b/src/Drivers/Imagick/Core.php index e0bc94e9..c680c9eb 100644 --- a/src/Drivers/Imagick/Core.php +++ b/src/Drivers/Imagick/Core.php @@ -178,7 +178,7 @@ class Core implements CoreInterface, Iterator */ public function next(): void { - $this->iteratorIndex = $this->iteratorIndex + 1; + $this->iteratorIndex += 1; } /** @@ -247,7 +247,7 @@ class Core implements CoreInterface, Iterator public function frame(int $position): FrameInterface { foreach ($this->imagick as $core) { - if ($core->getIteratorIndex() == $position) { + if ($core->getIteratorIndex() === $position) { return new Frame($core); } } diff --git a/src/Drivers/Imagick/Decoders/Base64ImageDecoder.php b/src/Drivers/Imagick/Decoders/Base64ImageDecoder.php index 03fd07ba..ea150627 100644 --- a/src/Drivers/Imagick/Decoders/Base64ImageDecoder.php +++ b/src/Drivers/Imagick/Decoders/Base64ImageDecoder.php @@ -21,6 +21,6 @@ class Base64ImageDecoder extends BinaryImageDecoder throw new DecoderException('Unable to decode input'); } - return parent::decode(base64_decode($input)); + return parent::decode(base64_decode((string) $input)); } } diff --git a/src/Drivers/Imagick/Decoders/NativeObjectDecoder.php b/src/Drivers/Imagick/Decoders/NativeObjectDecoder.php index dec2d8fd..19563e84 100644 --- a/src/Drivers/Imagick/Decoders/NativeObjectDecoder.php +++ b/src/Drivers/Imagick/Decoders/NativeObjectDecoder.php @@ -35,7 +35,7 @@ class NativeObjectDecoder extends SpecializableDecoder implements SpecializedInt // For some JPEG formats, the "coalesceImages()" call leads to an image // completely filled with background color. The logic behind this is // incomprehensible for me; could be an imagick bug. - if ($input->getImageFormat() != 'JPEG') { + if ($input->getImageFormat() !== 'JPEG') { $input = $input->coalesceImages(); } diff --git a/src/Drivers/Imagick/FontProcessor.php b/src/Drivers/Imagick/FontProcessor.php index 66abe7c6..50f0630e 100644 --- a/src/Drivers/Imagick/FontProcessor.php +++ b/src/Drivers/Imagick/FontProcessor.php @@ -63,7 +63,7 @@ class FontProcessor extends AbstractFontProcessor $draw->setFontSize($this->nativeFontSize($font)); $draw->setTextAlignment(Imagick::ALIGN_LEFT); - if ($color) { + if ($color instanceof ImagickPixel) { $draw->setFillColor($color); } diff --git a/src/Drivers/Imagick/Modifiers/BlurModifier.php b/src/Drivers/Imagick/Modifiers/BlurModifier.php index d7ece822..94c52d1b 100644 --- a/src/Drivers/Imagick/Modifiers/BlurModifier.php +++ b/src/Drivers/Imagick/Modifiers/BlurModifier.php @@ -13,7 +13,7 @@ class BlurModifier extends GenericBlurModifier implements SpecializedInterface public function apply(ImageInterface $image): ImageInterface { foreach ($image as $frame) { - $frame->native()->blurImage(1 * $this->amount, 0.5 * $this->amount); + $frame->native()->blurImage($this->amount, 0.5 * $this->amount); } return $image; diff --git a/src/Drivers/Imagick/Modifiers/TextModifier.php b/src/Drivers/Imagick/Modifiers/TextModifier.php index 42cf7a07..fcf29cd5 100644 --- a/src/Drivers/Imagick/Modifiers/TextModifier.php +++ b/src/Drivers/Imagick/Modifiers/TextModifier.php @@ -122,7 +122,7 @@ class TextModifier extends GenericTextModifier implements SpecializedInterface ?ImagickDraw $draw = null, PointInterface $offset = new Point(), ): void { - if ($draw !== null) { + if ($draw instanceof ImagickDraw) { $frame->native()->annotateImage( $draw, $textline->position()->x() + $offset->x(), diff --git a/src/File.php b/src/File.php index 18cdb01f..187645e4 100644 --- a/src/File.php +++ b/src/File.php @@ -8,8 +8,9 @@ use Intervention\Image\Exceptions\NotWritableException; use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Interfaces\FileInterface; use Intervention\Image\Traits\CanBuildFilePointer; +use Stringable; -class File implements FileInterface +class File implements FileInterface, Stringable { use CanBuildFilePointer; @@ -64,7 +65,7 @@ class File implements FileInterface if (is_file($filepath) && !is_writable($filepath)) { throw new NotWritableException( - "Can't write image. Path ({$filepath}) is not writable." + sprintf("Can't write image. Path (%s) is not writable.", $filepath) ); } @@ -72,7 +73,7 @@ class File implements FileInterface $saved = @file_put_contents($filepath, $this->pointer); if ($saved === false) { throw new NotWritableException( - "Can't write image data to path ({$filepath})." + sprintf("Can't write image data to path (%s).", $filepath) ); } } diff --git a/src/Format.php b/src/Format.php index 87a64684..dc9be3e1 100644 --- a/src/Format.php +++ b/src/Format.php @@ -88,7 +88,7 @@ enum Format { return array_filter( MediaType::cases(), - fn(MediaType $mediaType) => $mediaType->format() === $this + fn(MediaType $mediaType): bool => $mediaType->format() === $this ); } @@ -113,7 +113,7 @@ enum Format { return array_filter( FileExtension::cases(), - fn(FileExtension $fileExtension) => $fileExtension->format() === $this + fn(FileExtension $fileExtension): bool => $fileExtension->format() === $this ); } @@ -155,7 +155,7 @@ enum Format $reflectionClass = new ReflectionClass($classname); if ($constructor = $reflectionClass->getConstructor()) { $parameters = array_map( - fn($parameter) => $parameter->getName(), + fn($parameter): string => $parameter->getName(), $constructor->getParameters(), ); } @@ -163,7 +163,7 @@ enum Format // filter out unavailable options of target encoder $options = array_filter( $options, - fn($key) => in_array($key, $parameters), + fn($key): bool => in_array($key, $parameters), ARRAY_FILTER_USE_KEY, ); diff --git a/src/Geometry/Polygon.php b/src/Geometry/Polygon.php index 4ec1ab88..55e3a412 100644 --- a/src/Geometry/Polygon.php +++ b/src/Geometry/Polygon.php @@ -214,12 +214,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte */ public function mostLeftPoint(): PointInterface { - $points = []; - foreach ($this->points as $point) { - $points[] = $point; - } + $points = $this->points; - usort($points, function ($a, $b) { + usort($points, function (PointInterface $a, PointInterface $b): int { if ($a->x() === $b->x()) { return 0; } @@ -236,12 +233,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte */ public function mostRightPoint(): PointInterface { - $points = []; - foreach ($this->points as $point) { - $points[] = $point; - } + $points = $this->points; - usort($points, function ($a, $b) { + usort($points, function (PointInterface $a, PointInterface $b): int { if ($a->x() === $b->x()) { return 0; } @@ -258,12 +252,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte */ public function mostTopPoint(): PointInterface { - $points = []; - foreach ($this->points as $point) { - $points[] = $point; - } + $points = $this->points; - usort($points, function ($a, $b) { + usort($points, function (PointInterface $a, PointInterface $b): int { if ($a->y() === $b->y()) { return 0; } @@ -280,12 +271,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte */ public function mostBottomPoint(): PointInterface { - $points = []; - foreach ($this->points as $point) { - $points[] = $point; - } + $points = $this->points; - usort($points, function ($a, $b) { + usort($points, function (PointInterface $a, PointInterface $b): int { if ($a->y() === $b->y()) { return 0; } diff --git a/src/Geometry/Rectangle.php b/src/Geometry/Rectangle.php index 66937cc9..22da194e 100644 --- a/src/Geometry/Rectangle.php +++ b/src/Geometry/Rectangle.php @@ -111,13 +111,13 @@ class Rectangle extends Polygon implements SizeInterface case 'center-top': case 'middle-top': $x = intval(round($this->width() / 2)) + $offset_x; - $y = 0 + $offset_y; + $y = $offset_y; break; case 'top-right': case 'right-top': $x = $this->width() - $offset_x; - $y = 0 + $offset_y; + $y = $offset_y; break; case 'left': @@ -125,7 +125,7 @@ class Rectangle extends Polygon implements SizeInterface case 'left-middle': case 'center-left': case 'middle-left': - $x = 0 + $offset_x; + $x = $offset_x; $y = intval(round($this->height() / 2)) + $offset_y; break; @@ -140,7 +140,7 @@ class Rectangle extends Polygon implements SizeInterface case 'bottom-left': case 'left-bottom': - $x = 0 + $offset_x; + $x = $offset_x; $y = $this->height() - $offset_y; break; @@ -170,8 +170,8 @@ class Rectangle extends Polygon implements SizeInterface default: case 'top-left': case 'left-top': - $x = 0 + $offset_x; - $y = 0 + $offset_y; + $x = $offset_x; + $y = $offset_y; break; } diff --git a/src/Geometry/Tools/RectangleResizer.php b/src/Geometry/Tools/RectangleResizer.php index 7f52f64e..705783d5 100644 --- a/src/Geometry/Tools/RectangleResizer.php +++ b/src/Geometry/Tools/RectangleResizer.php @@ -52,7 +52,7 @@ class RectangleResizer */ protected function hasTargetWidth(): bool { - return is_integer($this->width); + return is_int($this->width); } /** @@ -72,7 +72,7 @@ class RectangleResizer */ protected function hasTargetHeight(): bool { - return is_integer($this->height); + return is_int($this->height); } /** diff --git a/src/Image.php b/src/Image.php index 21199ddf..8777dc5d 100644 --- a/src/Image.php +++ b/src/Image.php @@ -107,7 +107,7 @@ final class Image implements ImageInterface * * @var Origin */ - protected Origin $origin; + private Origin $origin; /** * Create new instance @@ -119,9 +119,9 @@ final class Image implements ImageInterface * @return void */ public function __construct( - protected DriverInterface $driver, - protected CoreInterface $core, - protected CollectionInterface $exif = new Collection() + private DriverInterface $driver, + private CoreInterface $core, + private CollectionInterface $exif = new Collection() ) { $this->origin = new Origin(); } diff --git a/src/ImageManager.php b/src/ImageManager.php index 64c919a7..95272aa9 100644 --- a/src/ImageManager.php +++ b/src/ImageManager.php @@ -14,7 +14,7 @@ use Intervention\Image\Interfaces\ImageManagerInterface; final class ImageManager implements ImageManagerInterface { - protected DriverInterface $driver; + private DriverInterface $driver; /** * @link https://image.intervention.io/v3/basics/image-manager#create-a-new-image-manager-instance diff --git a/src/Modifiers/FillModifier.php b/src/Modifiers/FillModifier.php index 3bd69ac2..54d3cd23 100644 --- a/src/Modifiers/FillModifier.php +++ b/src/Modifiers/FillModifier.php @@ -17,6 +17,6 @@ class FillModifier extends SpecializableModifier public function hasPosition(): bool { - return !empty($this->position); + return $this->position instanceof PointInterface; } } diff --git a/src/Modifiers/RemoveAnimationModifier.php b/src/Modifiers/RemoveAnimationModifier.php index c20983ce..0493e139 100644 --- a/src/Modifiers/RemoveAnimationModifier.php +++ b/src/Modifiers/RemoveAnimationModifier.php @@ -50,8 +50,7 @@ class RemoveAnimationModifier extends SpecializableModifier $total = count($image); $position = intval(round($total / 100 * intval($matches['percent']))); - $position = $position == $total ? $position - 1 : $position; - return $position; + return $position == $total ? $position - 1 : $position; } } diff --git a/src/Resolution.php b/src/Resolution.php index dea71b13..6dd13077 100644 --- a/src/Resolution.php +++ b/src/Resolution.php @@ -5,8 +5,9 @@ declare(strict_types=1); namespace Intervention\Image; use Intervention\Image\Interfaces\ResolutionInterface; +use Stringable; -class Resolution implements ResolutionInterface +class Resolution implements ResolutionInterface, Stringable { public const PER_INCH = 1; public const PER_CM = 2; diff --git a/src/Typography/Line.php b/src/Typography/Line.php index 3bdeb02b..f9630cbc 100644 --- a/src/Typography/Line.php +++ b/src/Typography/Line.php @@ -9,12 +9,13 @@ use Countable; use Intervention\Image\Geometry\Point; use Intervention\Image\Interfaces\PointInterface; use IteratorAggregate; +use Stringable; use Traversable; /** * @implements IteratorAggregate */ -class Line implements IteratorAggregate, Countable +class Line implements IteratorAggregate, Countable, Stringable { /** * Segments (usually individual words including punctuation marks) of the line diff --git a/src/Typography/TextBlock.php b/src/Typography/TextBlock.php index 011b34e7..8a40887a 100644 --- a/src/Typography/TextBlock.php +++ b/src/Typography/TextBlock.php @@ -67,7 +67,7 @@ class TextBlock extends Collection public function longestLine(): Line { $lines = $this->lines(); - usort($lines, function (Line $a, Line $b) { + usort($lines, function (Line $a, Line $b): int { if ($a->length() === $b->length()) { return 0; }