From 9a5594b9b5fff1d3ef3b1c163fb84e10da07b24f Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 21 Apr 2024 16:01:37 +0200 Subject: [PATCH] Refactor to meet phpstan level 5 --- src/Colors/Cmyk/Colorspace.php | 18 +++++++++++---- src/Colors/Hsl/Colorspace.php | 28 ++++++++++++++++++++++-- src/Colors/Hsv/Colorspace.php | 29 +++++++++++++++++++----- src/Colors/Rgb/Colorspace.php | 40 +++++++++++++++++++++++++++++----- src/Geometry/Point.php | 8 +++---- 5 files changed, 102 insertions(+), 21 deletions(-) diff --git a/src/Colors/Cmyk/Colorspace.php b/src/Colors/Cmyk/Colorspace.php index 2b64030f..e7ab6aec 100644 --- a/src/Colors/Cmyk/Colorspace.php +++ b/src/Colors/Cmyk/Colorspace.php @@ -9,6 +9,7 @@ use Intervention\Image\Colors\Hsv\Color as HsvColor; use Intervention\Image\Colors\Hsl\Color as HslColor; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; @@ -36,9 +37,9 @@ public function colorFromNormalized(array $normalized): ColorInterface } /** - * {@inheritdoc} - * - * @see ColorspaceInterface::importColor() + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException */ public function importColor(ColorInterface $color): ColorInterface { @@ -50,8 +51,17 @@ public function importColor(ColorInterface $color): ColorInterface }; } - protected function importRgbColor(RgbColor $color): CmykColor + /** + * @param ColorInterface $color + * @return Color + * @throws ColorException + */ + protected function importRgbColor(ColorInterface $color): CmykColor { + if (!($color instanceof RgbColor)) { + throw new ColorException('Unabled to import color of type ' . $color::class . '.'); + } + $c = (255 - $color->red()->value()) / 255.0 * 100; $m = (255 - $color->green()->value()) / 255.0 * 100; $y = (255 - $color->blue()->value()) / 255.0 * 100; diff --git a/src/Colors/Hsl/Colorspace.php b/src/Colors/Hsl/Colorspace.php index c8baf320..e2b7d35d 100644 --- a/src/Colors/Hsl/Colorspace.php +++ b/src/Colors/Hsl/Colorspace.php @@ -8,6 +8,7 @@ use Intervention\Image\Colors\Rgb\Color as RgbColor; use Intervention\Image\Colors\Hsv\Color as HsvColor; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; @@ -33,6 +34,11 @@ public function colorFromNormalized(array $normalized): ColorInterface return new Color(...$values); } + /** + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException + */ public function importColor(ColorInterface $color): ColorInterface { return match ($color::class) { @@ -43,8 +49,17 @@ public function importColor(ColorInterface $color): ColorInterface }; } - protected function importRgbColor(RgbColor $color): ColorInterface + /** + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException + */ + protected function importRgbColor(ColorInterface $color): ColorInterface { + if (!($color instanceof RgbColor)) { + throw new ColorException('Unabled to import color of type ' . $color::class . '.'); + } + // normalized values of rgb channels $values = array_map(function ($channel) { return $channel->normalize(); @@ -84,8 +99,17 @@ protected function importRgbColor(RgbColor $color): ColorInterface ); } - protected function importHsvColor(HsvColor $color): ColorInterface + /** + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException + */ + protected function importHsvColor(ColorInterface $color): ColorInterface { + if (!($color instanceof HsvColor)) { + throw new ColorException('Unabled to import color of type ' . $color::class . '.'); + } + // normalized values of hsv channels list($h, $s, $v) = array_map(function ($channel) { return $channel->normalize(); diff --git a/src/Colors/Hsv/Colorspace.php b/src/Colors/Hsv/Colorspace.php index c6f2f545..ad390275 100644 --- a/src/Colors/Hsv/Colorspace.php +++ b/src/Colors/Hsv/Colorspace.php @@ -8,6 +8,7 @@ use Intervention\Image\Colors\Rgb\Color as RgbColor; use Intervention\Image\Colors\Hsl\Color as HslColor; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; @@ -34,9 +35,9 @@ public function colorFromNormalized(array $normalized): ColorInterface } /** - * {@inheritdoc} - * - * @see ColorspaceInterface::importColor() + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException */ public function importColor(ColorInterface $color): ColorInterface { @@ -48,8 +49,17 @@ public function importColor(ColorInterface $color): ColorInterface }; } - protected function importRgbColor(RgbColor $color): ColorInterface + /** + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException + */ + protected function importRgbColor(ColorInterface $color): ColorInterface { + if (!($color instanceof RgbColor)) { + throw new ColorException('Unabled to import color of type ' . $color::class . '.'); + } + // normalized values of rgb channels $values = array_map(function ($channel) { return $channel->normalize(); @@ -89,8 +99,17 @@ protected function importRgbColor(RgbColor $color): ColorInterface ); } - protected function importHslColor(HslColor $color): ColorInterface + /** + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException + */ + protected function importHslColor(ColorInterface $color): ColorInterface { + if (!($color instanceof HslColor)) { + throw new ColorException('Unabled to import color of type ' . $color::class . '.'); + } + // normalized values of hsl channels list($h, $s, $l) = array_map(function ($channel) { return $channel->normalize(); diff --git a/src/Colors/Rgb/Colorspace.php b/src/Colors/Rgb/Colorspace.php index 0335eabd..3c9b4787 100644 --- a/src/Colors/Rgb/Colorspace.php +++ b/src/Colors/Rgb/Colorspace.php @@ -7,6 +7,7 @@ use Intervention\Image\Colors\Hsv\Color as HsvColor; use Intervention\Image\Colors\Hsl\Color as HslColor; use Intervention\Image\Colors\Cmyk\Color as CmykColor; +use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; @@ -34,9 +35,9 @@ public function colorFromNormalized(array $normalized): ColorInterface } /** - * {@inheritdoc} - * - * @see ColorspaceInterface::importColor() + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException */ public function importColor(ColorInterface $color): ColorInterface { @@ -48,8 +49,17 @@ public function importColor(ColorInterface $color): ColorInterface }; } - protected function importCmykColor(CmykColor $color): ColorInterface + /** + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException + */ + protected function importCmykColor(ColorInterface $color): ColorInterface { + if (!($color instanceof CmykColor)) { + throw new ColorException('Unabled to import color of type ' . $color::class . '.'); + } + return new Color( (int) (255 * (1 - $color->cyan()->normalize()) * (1 - $color->key()->normalize())), (int) (255 * (1 - $color->magenta()->normalize()) * (1 - $color->key()->normalize())), @@ -57,8 +67,17 @@ protected function importCmykColor(CmykColor $color): ColorInterface ); } - protected function importHsvColor(HsvColor $color): ColorInterface + /** + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException + */ + protected function importHsvColor(ColorInterface $color): ColorInterface { + if (!($color instanceof HsvColor)) { + throw new ColorException('Unabled to import color of type ' . $color::class . '.'); + } + $chroma = $color->value()->normalize() * $color->saturation()->normalize(); $hue = $color->hue()->normalize() * 6; $x = $chroma * (1 - abs(fmod($hue, 2) - 1)); @@ -83,8 +102,17 @@ protected function importHsvColor(HsvColor $color): ColorInterface return $this->colorFromNormalized($values); } - protected function importHslColor(HslColor $color): ColorInterface + /** + * @param ColorInterface $color + * @return ColorInterface + * @throws ColorException + */ + protected function importHslColor(ColorInterface $color): ColorInterface { + if (!($color instanceof HslColor)) { + throw new ColorException('Unabled to import color of type ' . $color::class . '.'); + } + // normalized values of hsl channels list($h, $s, $l) = array_map(function ($channel) { return $channel->normalize(); diff --git a/src/Geometry/Point.php b/src/Geometry/Point.php index 29cb2d2e..7f524838 100644 --- a/src/Geometry/Point.php +++ b/src/Geometry/Point.php @@ -113,17 +113,17 @@ public function setPosition(int $x, int $y): self * Rotate point ccw around pivot * * @param float $angle - * @param Point $pivot + * @param PointInterface $pivot * @return Point */ - public function rotate(float $angle, self $pivot): self + public function rotate(float $angle, PointInterface $pivot): self { $sin = round(sin(deg2rad($angle)), 6); $cos = round(cos(deg2rad($angle)), 6); return $this->setPosition( - intval($cos * ($this->x - $pivot->x) - $sin * ($this->y - $pivot->y) + $pivot->x), - intval($sin * ($this->x - $pivot->x) + $cos * ($this->y - $pivot->y) + $pivot->y) + intval($cos * ($this->x() - $pivot->x()) - $sin * ($this->y() - $pivot->y()) + $pivot->x()), + intval($sin * ($this->x() - $pivot->x()) + $cos * ($this->y() - $pivot->y()) + $pivot->y()) ); } }