Skip to content

Commit

Permalink
Refactor to meet PHPStan level 5 (#1334)
Browse files Browse the repository at this point in the history
* Update phpstan level
* Refactor to meet phpstan level 5
  • Loading branch information
olivervogel committed Apr 23, 2024
1 parent e922730 commit 5464ca5
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 24 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Expand Up @@ -18,7 +18,7 @@ services:
analysis:
build: ./
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:
- ./:/project
standards:
Expand Down
2 changes: 1 addition & 1 deletion phpstan.dist.neon
@@ -1,5 +1,5 @@
parameters:
level: 4
level: 5
paths:
- src
exceptions:
Expand Down
18 changes: 14 additions & 4 deletions src/Colors/Cmyk/Colorspace.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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
{
Expand All @@ -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;
Expand Down
28 changes: 26 additions & 2 deletions src/Colors/Hsl/Colorspace.php
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
29 changes: 24 additions & 5 deletions src/Colors/Hsv/Colorspace.php
Expand Up @@ -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;

Expand All @@ -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
{
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
40 changes: 34 additions & 6 deletions src/Colors/Rgb/Colorspace.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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
{
Expand All @@ -48,17 +49,35 @@ 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())),
(int) (255 * (1 - $color->yellow()->normalize()) * (1 - $color->key()->normalize())),
);
}

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));
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Format.php
Expand Up @@ -90,7 +90,7 @@ public function fileExtensions(): array
/**
* Create an encoder instance that matches the format
*
* @param array $options
* @param mixed $options
* @return EncoderInterface
*/
public function encoder(mixed ...$options): EncoderInterface
Expand Down
8 changes: 4 additions & 4 deletions src/Geometry/Point.php
Expand Up @@ -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())
);
}
}

0 comments on commit 5464ca5

Please sign in to comment.