From 4f0aadcfc9519fdf7d4410d48ba80848d453ccce Mon Sep 17 00:00:00 2001 From: Niels Vanpachtenbeke <10651054+Nielsvanpach@users.noreply.github.com> Date: Fri, 1 Dec 2023 15:06:38 +0100 Subject: [PATCH] Phpstan fixes (#206) * remove positive_int annotations will fail on a 0 value * alpha values can be floats * increase phpstan level + fixes * Fix styling * exclude relevant phpstan issues from baseline --------- Co-authored-by: Nielsvanpach --- phpstan-baseline.neon | 133 ++++++++++++++---- phpstan.neon.dist | 2 +- src/Drivers/Color.php | 6 +- .../Concerns/CalculatesCropOffsets.php | 2 +- .../CalculatesFocalCropCoordinates.php | 4 +- src/Drivers/Gd/GdDriver.php | 3 +- src/Enums/CropPosition.php | 2 +- src/Size.php | 7 +- 8 files changed, 123 insertions(+), 36 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 244592ee..c6669e64 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,86 +1,171 @@ parameters: ignoreErrors: - - message: "#^Access to an undefined property Spatie\\\\Image\\\\Drivers\\\\Color\\:\\:\\$alpha\\.$#" + message: "#^Parameter \\#1 \\$value of method Spatie\\\\Image\\\\Drivers\\\\Color\\:\\:initFromObject\\(\\) expects ImagickPixel, object given\\.$#" count: 1 - path: src/Drivers/Gd/GdColor.php + path: src/Drivers/Color.php - - message: "#^Access to an undefined property Spatie\\\\Image\\\\Drivers\\\\Color\\:\\:\\$blue\\.$#" + message: "#^Method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:exif\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 - path: src/Drivers/Gd/GdColor.php + path: src/Drivers/Gd/GdDriver.php - - message: "#^Access to an undefined property Spatie\\\\Image\\\\Drivers\\\\Color\\:\\:\\$green\\.$#" + message: "#^Method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:pngCompression\\(\\) should return int\\<\\-1, 9\\> but returns int\\.$#" count: 1 - path: src/Drivers/Gd/GdColor.php + path: src/Drivers/Gd/GdDriver.php - - message: "#^Access to an undefined property Spatie\\\\Image\\\\Drivers\\\\Color\\:\\:\\$red\\.$#" + message: "#^Parameter \\#1 \\$data of function imagecreatefromstring expects string, string\\|false given\\.$#" count: 1 - path: src/Drivers/Gd/GdColor.php + path: src/Drivers/Gd/GdDriver.php - - message: "#^Access to an undefined property Spatie\\\\Image\\\\Drivers\\\\ImageDriver\\:\\:\\$image\\.$#" + message: "#^Parameter \\#1 \\$dst_image of function imagecopyresampled expects GdImage, GdImage\\|false given\\.$#" count: 1 path: src/Drivers/Gd/GdDriver.php - - message: "#^Method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:exif\\(\\) return type has no value type specified in iterable type array\\.$#" + message: "#^Parameter \\#1 \\$image of function imagealphablending expects GdImage, GdImage\\|false given\\.$#" count: 1 path: src/Drivers/Gd/GdDriver.php - - message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:\\$exif type has no value type specified in iterable type array\\.$#" + message: "#^Parameter \\#1 \\$image of function imagecolorallocatealpha expects GdImage, GdImage\\|false given\\.$#" count: 1 path: src/Drivers/Gd/GdDriver.php - - message: "#^Unreachable statement \\- code above always terminates\\.$#" + message: "#^Parameter \\#1 \\$image of function imagecolorsforindex expects GdImage, GdImage\\|false given\\.$#" count: 1 path: src/Drivers/Gd/GdDriver.php - - message: "#^Method Spatie\\\\Image\\\\Drivers\\\\ImageDriver\\:\\:exif\\(\\) return type has no value type specified in iterable type array\\.$#" + message: "#^Parameter \\#1 \\$image of function imagecolortransparent expects GdImage, GdImage\\|false given\\.$#" count: 1 - path: src/Drivers/ImageDriver.php + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Parameter \\#1 \\$image of function imagefill expects GdImage, GdImage\\|false given\\.$#" + count: 2 + path: src/Drivers/Gd/GdDriver.php - - message: "#^Call to an undefined method Spatie\\\\Image\\\\Drivers\\\\Color\\:\\:getAlphaValue\\(\\)\\.$#" + message: "#^Parameter \\#1 \\$image of function imagesavealpha expects GdImage, GdImage\\|false given\\.$#" count: 1 - path: src/Drivers/Imagick/ImagickColor.php + path: src/Drivers/Gd/GdDriver.php - - message: "#^Call to an undefined method Spatie\\\\Image\\\\Drivers\\\\Color\\:\\:getBlueValue\\(\\)\\.$#" + message: "#^Parameter \\#1 \\$image of method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:setImage\\(\\) expects GdImage, GdImage\\|false given\\.$#" count: 1 - path: src/Drivers/Imagick/ImagickColor.php + path: src/Drivers/Gd/GdDriver.php - - message: "#^Call to an undefined method Spatie\\\\Image\\\\Drivers\\\\Color\\:\\:getGreenValue\\(\\)\\.$#" + message: "#^Parameter \\#1 \\$stream of function fclose expects resource, resource\\|false given\\.$#" count: 1 - path: src/Drivers/Imagick/ImagickColor.php + path: src/Drivers/Gd/GdDriver.php - - message: "#^Call to an undefined method Spatie\\\\Image\\\\Drivers\\\\Color\\:\\:getRedValue\\(\\)\\.$#" + message: "#^Parameter \\#1 \\$stream of function fread expects resource, resource\\|false given\\.$#" count: 1 - path: src/Drivers/Imagick/ImagickColor.php + path: src/Drivers/Gd/GdDriver.php - - message: "#^Access to an undefined property Spatie\\\\Image\\\\Drivers\\\\ImageDriver\\:\\:\\$image\\.$#" + message: "#^Parameter \\#1 \\$string of function base64_encode expects string, string\\|false given\\.$#" count: 2 - path: src/Drivers/Imagick/ImagickDriver.php + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Parameter \\#1 \\$width of method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:manualCrop\\(\\) expects int, int\\|null given\\.$#" + count: 1 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Parameter \\#2 \\$color of function imagecolorsforindex expects int, int\\|false given\\.$#" + count: 1 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Parameter \\#2 \\$color of function imagecolortransparent expects int\\|null, int\\|false given\\.$#" + count: 1 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Parameter \\#2 \\$height of method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:manualCrop\\(\\) expects int, int\\|null given\\.$#" + count: 1 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Parameter \\#2 \\$length of function fread expects int\\<0, max\\>, int\\<0, max\\>\\|false given\\.$#" + count: 1 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Parameter \\#4 \\$color of function imagefill expects int, int\\|false given\\.$#" + count: 1 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Parameter \\#6 \\$color of function imagefilledrectangle expects int, int\\|false given\\.$#" + count: 1 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:\\$exif type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:\\$image \\(GdImage\\) does not accept GdImage\\|false\\.$#" + count: 2 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:\\$image \\(GdImage\\) does not accept mixed\\.$#" + count: 1 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Unreachable statement \\- code above always terminates\\.$#" + count: 1 + path: src/Drivers/Gd/GdDriver.php + + - + message: "#^Method Spatie\\\\Image\\\\Drivers\\\\ImageDriver\\:\\:exif\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/Drivers/ImageDriver.php - message: "#^Method Spatie\\\\Image\\\\Drivers\\\\Imagick\\\\ImagickDriver\\:\\:exif\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 path: src/Drivers/Imagick/ImagickDriver.php + - + message: "#^Parameter \\#1 \\$width of method Spatie\\\\Image\\\\Drivers\\\\Imagick\\\\ImagickDriver\\:\\:manualCrop\\(\\) expects int, int\\|null given\\.$#" + count: 1 + path: src/Drivers/Imagick/ImagickDriver.php + + - + message: "#^Parameter \\#2 \\$height of method Spatie\\\\Image\\\\Drivers\\\\Imagick\\\\ImagickDriver\\:\\:manualCrop\\(\\) expects int, int\\|null given\\.$#" + count: 1 + path: src/Drivers/Imagick/ImagickDriver.php + - message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Imagick\\\\ImagickDriver\\:\\:\\$exif type has no value type specified in iterable type array\\.$#" count: 1 path: src/Drivers/Imagick/ImagickDriver.php + - + message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Imagick\\\\ImagickDriver\\:\\:\\$image \\(Imagick\\) does not accept mixed\\.$#" + count: 1 + path: src/Drivers/Imagick/ImagickDriver.php + - message: "#^Match expression does not handle remaining value\\: Spatie\\\\Image\\\\Enums\\\\Fit\\:\\:Crop$#" count: 1 path: src/Enums/Fit.php + + - + message: "#^Part \\$color \\(mixed\\) of encapsed string cannot be cast to string\\.$#" + count: 1 + path: src/Exceptions/InvalidColor.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index fc8bd5e0..56bdfe60 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,7 +2,7 @@ includes: - phpstan-baseline.neon parameters: - level: 6 + level: 9 paths: - src/ diff --git a/src/Drivers/Color.php b/src/Drivers/Color.php index b41b79c9..a54c519d 100644 --- a/src/Drivers/Color.php +++ b/src/Drivers/Color.php @@ -10,7 +10,7 @@ abstract class Color { abstract public function initFromInteger(int $value): self; - /** @param array $value */ + /** @param array $value */ abstract public function initFromArray(array $value): self; abstract public function initFromString(string $value): self; @@ -25,7 +25,7 @@ abstract public function getInt(): int; abstract public function getHex(string $prefix): string; - /** @return array */ + /** @return array */ abstract public function getArray(): array; abstract public function getRgba(): string; @@ -91,7 +91,7 @@ protected function rgbaFromString(string $colorValue): array $result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? (int) ($matches[1]) : 0; $result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? (int) ($matches[2]) : 0; $result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? (int) ($matches[3]) : 0; - $result[3] = ($matches[4] >= 0 && $matches[4] <= 1) ? $matches[4] : 0; + $result[3] = ($matches[4] >= 0 && $matches[4] <= 1) ? (int) ($matches[4]) : 0; } else { throw InvalidColor::make($colorValue); } diff --git a/src/Drivers/Concerns/CalculatesCropOffsets.php b/src/Drivers/Concerns/CalculatesCropOffsets.php index 913ddba3..f5266818 100644 --- a/src/Drivers/Concerns/CalculatesCropOffsets.php +++ b/src/Drivers/Concerns/CalculatesCropOffsets.php @@ -7,7 +7,7 @@ /** @mixin \Spatie\Image\Drivers\ImageDriver */ trait CalculatesCropOffsets { - /** @return array */ + /** @return array */ protected function calculateCropOffsets(int $width, int $height, CropPosition $position): array { [$offsetPercentageX, $offsetPercentageY] = $position->offsetPercentages(); diff --git a/src/Drivers/Concerns/CalculatesFocalCropCoordinates.php b/src/Drivers/Concerns/CalculatesFocalCropCoordinates.php index 7309e236..6451a0ef 100644 --- a/src/Drivers/Concerns/CalculatesFocalCropCoordinates.php +++ b/src/Drivers/Concerns/CalculatesFocalCropCoordinates.php @@ -5,8 +5,8 @@ /** @mixin \Spatie\Image\Drivers\ImageDriver */ trait CalculatesFocalCropCoordinates { - /** @return array */ - protected function calculateFocalCropCoordinates(int $width, int $height, int $cropCenterX, int $cropCenterY): array + /** @return array */ + protected function calculateFocalCropCoordinates(int $width, int $height, ?int $cropCenterX, ?int $cropCenterY): array { $width = min($width, $this->getWidth()); $height = min($height, $this->getHeight()); diff --git a/src/Drivers/Gd/GdDriver.php b/src/Drivers/Gd/GdDriver.php index f072fb37..8f852613 100644 --- a/src/Drivers/Gd/GdDriver.php +++ b/src/Drivers/Gd/GdDriver.php @@ -215,7 +215,7 @@ protected function modify( $transparentColorValue = imagecolortransparent($this->image); - if ($transparentColorValue != -1) { + if ($transparentColorValue !== -1) { $rgba = imagecolorsforindex($newImage, $transparentColorValue); $transparentColor = imagecolorallocatealpha( @@ -656,6 +656,7 @@ public function border(int $width, BorderType $type, string $color = '000000'): return $this; } + /** @param int<-1, 100> $quality */ public function quality(int $quality): self { $this->ensureNumberBetween($quality, -1, 100, 'quality'); diff --git a/src/Enums/CropPosition.php b/src/Enums/CropPosition.php index e3f6e075..e10180b3 100644 --- a/src/Enums/CropPosition.php +++ b/src/Enums/CropPosition.php @@ -14,7 +14,7 @@ enum CropPosition: string case Bottom = 'bottom'; case BottomRight = 'bottomRight'; - /** @return array */ + /** @return array */ public function offsetPercentages(): array { return match ($this) { diff --git a/src/Size.php b/src/Size.php index cf5e0de8..2daae0e7 100644 --- a/src/Size.php +++ b/src/Size.php @@ -9,8 +9,8 @@ class Size { public function __construct( - public int|float $width, - public int|float $height, + public int $width, + public int $height, public Point $pivot = new Point() ) { } @@ -42,7 +42,8 @@ public function resize( ->resizeWidth($desiredWidth, $constraints) ->resizeHeight($desiredHeight, $constraints); - return $dominantHeightSize->fitsInto(new Size($desiredWidth, $desiredHeight)) + // @todo desiredWidth and desiredHeight can still be null here, which will cause an error + return $dominantHeightSize->fitsInto(new self($desiredWidth, $desiredHeight)) ? $dominantHeightSize : $dominantWidthSize; }