1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-11 00:14:03 +02:00

Refactor to meet PHPStan level 5 (#1334)

* Update phpstan level
* Refactor to meet phpstan level 5
This commit is contained in:
Oliver Vogel
2024-04-23 16:05:14 +02:00
committed by GitHub
parent e922730019
commit 5464ca5ec7
8 changed files with 105 additions and 24 deletions

View File

@@ -18,7 +18,7 @@ services:
analysis: analysis:
build: ./ build: ./
working_dir: /project working_dir: /project
command: bash -c "composer install && ./vendor/bin/phpstan analyze --memory-limit=512M --level=4 ./src" command: bash -c "composer install && ./vendor/bin/phpstan analyze --memory-limit=512M ./src"
volumes: volumes:
- ./:/project - ./:/project
standards: standards:

View File

@@ -1,5 +1,5 @@
parameters: parameters:
level: 4 level: 5
paths: paths:
- src - src
exceptions: exceptions:

View File

@@ -9,6 +9,7 @@ use Intervention\Image\Colors\Cmyk\Color as CmykColor;
use Intervention\Image\Colors\Hsv\Color as HsvColor; use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Hsl\Color as HslColor; use Intervention\Image\Colors\Hsl\Color as HslColor;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -36,9 +37,9 @@ class Colorspace implements ColorspaceInterface
} }
/** /**
* {@inheritdoc} * @param ColorInterface $color
* * @return ColorInterface
* @see ColorspaceInterface::importColor() * @throws ColorException
*/ */
public function importColor(ColorInterface $color): ColorInterface public function importColor(ColorInterface $color): ColorInterface
{ {
@@ -50,8 +51,17 @@ class Colorspace implements ColorspaceInterface
}; };
} }
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; $c = (255 - $color->red()->value()) / 255.0 * 100;
$m = (255 - $color->green()->value()) / 255.0 * 100; $m = (255 - $color->green()->value()) / 255.0 * 100;
$y = (255 - $color->blue()->value()) / 255.0 * 100; $y = (255 - $color->blue()->value()) / 255.0 * 100;

View File

@@ -8,6 +8,7 @@ use Intervention\Image\Colors\Cmyk\Color as CmykColor;
use Intervention\Image\Colors\Rgb\Color as RgbColor; use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Colors\Hsv\Color as HsvColor; use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -33,6 +34,11 @@ class Colorspace implements ColorspaceInterface
return new Color(...$values); return new Color(...$values);
} }
/**
* @param ColorInterface $color
* @return ColorInterface
* @throws ColorException
*/
public function importColor(ColorInterface $color): ColorInterface public function importColor(ColorInterface $color): ColorInterface
{ {
return match ($color::class) { return match ($color::class) {
@@ -43,8 +49,17 @@ class Colorspace implements ColorspaceInterface
}; };
} }
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 // normalized values of rgb channels
$values = array_map(function ($channel) { $values = array_map(function ($channel) {
return $channel->normalize(); return $channel->normalize();
@@ -84,8 +99,17 @@ class Colorspace implements ColorspaceInterface
); );
} }
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 // normalized values of hsv channels
list($h, $s, $v) = array_map(function ($channel) { list($h, $s, $v) = array_map(function ($channel) {
return $channel->normalize(); return $channel->normalize();

View File

@@ -8,6 +8,7 @@ use Intervention\Image\Colors\Cmyk\Color as CmykColor;
use Intervention\Image\Colors\Rgb\Color as RgbColor; use Intervention\Image\Colors\Rgb\Color as RgbColor;
use Intervention\Image\Colors\Hsl\Color as HslColor; use Intervention\Image\Colors\Hsl\Color as HslColor;
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace; use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -34,9 +35,9 @@ class Colorspace implements ColorspaceInterface
} }
/** /**
* {@inheritdoc} * @param ColorInterface $color
* * @return ColorInterface
* @see ColorspaceInterface::importColor() * @throws ColorException
*/ */
public function importColor(ColorInterface $color): ColorInterface public function importColor(ColorInterface $color): ColorInterface
{ {
@@ -48,8 +49,17 @@ class Colorspace implements ColorspaceInterface
}; };
} }
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 // normalized values of rgb channels
$values = array_map(function ($channel) { $values = array_map(function ($channel) {
return $channel->normalize(); return $channel->normalize();
@@ -89,8 +99,17 @@ class Colorspace implements ColorspaceInterface
); );
} }
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 // normalized values of hsl channels
list($h, $s, $l) = array_map(function ($channel) { list($h, $s, $l) = array_map(function ($channel) {
return $channel->normalize(); return $channel->normalize();

View File

@@ -7,6 +7,7 @@ namespace Intervention\Image\Colors\Rgb;
use Intervention\Image\Colors\Hsv\Color as HsvColor; use Intervention\Image\Colors\Hsv\Color as HsvColor;
use Intervention\Image\Colors\Hsl\Color as HslColor; use Intervention\Image\Colors\Hsl\Color as HslColor;
use Intervention\Image\Colors\Cmyk\Color as CmykColor; use Intervention\Image\Colors\Cmyk\Color as CmykColor;
use Intervention\Image\Exceptions\ColorException;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\ColorspaceInterface;
@@ -34,9 +35,9 @@ class Colorspace implements ColorspaceInterface
} }
/** /**
* {@inheritdoc} * @param ColorInterface $color
* * @return ColorInterface
* @see ColorspaceInterface::importColor() * @throws ColorException
*/ */
public function importColor(ColorInterface $color): ColorInterface public function importColor(ColorInterface $color): ColorInterface
{ {
@@ -48,8 +49,17 @@ class Colorspace implements ColorspaceInterface
}; };
} }
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( return new Color(
(int) (255 * (1 - $color->cyan()->normalize()) * (1 - $color->key()->normalize())), (int) (255 * (1 - $color->cyan()->normalize()) * (1 - $color->key()->normalize())),
(int) (255 * (1 - $color->magenta()->normalize()) * (1 - $color->key()->normalize())), (int) (255 * (1 - $color->magenta()->normalize()) * (1 - $color->key()->normalize())),
@@ -57,8 +67,17 @@ class Colorspace implements ColorspaceInterface
); );
} }
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(); $chroma = $color->value()->normalize() * $color->saturation()->normalize();
$hue = $color->hue()->normalize() * 6; $hue = $color->hue()->normalize() * 6;
$x = $chroma * (1 - abs(fmod($hue, 2) - 1)); $x = $chroma * (1 - abs(fmod($hue, 2) - 1));
@@ -83,8 +102,17 @@ class Colorspace implements ColorspaceInterface
return $this->colorFromNormalized($values); 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 // normalized values of hsl channels
list($h, $s, $l) = array_map(function ($channel) { list($h, $s, $l) = array_map(function ($channel) {
return $channel->normalize(); return $channel->normalize();

View File

@@ -90,7 +90,7 @@ enum Format
/** /**
* Create an encoder instance that matches the format * Create an encoder instance that matches the format
* *
* @param array $options * @param mixed $options
* @return EncoderInterface * @return EncoderInterface
*/ */
public function encoder(mixed ...$options): EncoderInterface public function encoder(mixed ...$options): EncoderInterface

View File

@@ -113,17 +113,17 @@ class Point implements PointInterface
* Rotate point ccw around pivot * Rotate point ccw around pivot
* *
* @param float $angle * @param float $angle
* @param Point $pivot * @param PointInterface $pivot
* @return Point * @return Point
*/ */
public function rotate(float $angle, self $pivot): self public function rotate(float $angle, PointInterface $pivot): self
{ {
$sin = round(sin(deg2rad($angle)), 6); $sin = round(sin(deg2rad($angle)), 6);
$cos = round(cos(deg2rad($angle)), 6); $cos = round(cos(deg2rad($angle)), 6);
return $this->setPosition( return $this->setPosition(
intval($cos * ($this->x - $pivot->x) - $sin * ($this->y - $pivot->y) + $pivot->x), 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($sin * ($this->x() - $pivot->x()) + $cos * ($this->y() - $pivot->y()) + $pivot->y())
); );
} }
} }