Skip to content

Commit 5464ca5

Browse files
authored
Refactor to meet PHPStan level 5 (#1334)
* Update phpstan level * Refactor to meet phpstan level 5
1 parent e922730 commit 5464ca5

File tree

8 files changed

+105
-24
lines changed

8 files changed

+105
-24
lines changed

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818
analysis:
1919
build: ./
2020
working_dir: /project
21-
command: bash -c "composer install && ./vendor/bin/phpstan analyze --memory-limit=512M --level=4 ./src"
21+
command: bash -c "composer install && ./vendor/bin/phpstan analyze --memory-limit=512M ./src"
2222
volumes:
2323
- ./:/project
2424
standards:

phpstan.dist.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 4
2+
level: 5
33
paths:
44
- src
55
exceptions:

src/Colors/Cmyk/Colorspace.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Intervention\Image\Colors\Hsv\Color as HsvColor;
1010
use Intervention\Image\Colors\Hsl\Color as HslColor;
1111
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
12+
use Intervention\Image\Exceptions\ColorException;
1213
use Intervention\Image\Interfaces\ColorInterface;
1314
use Intervention\Image\Interfaces\ColorspaceInterface;
1415

@@ -36,9 +37,9 @@ public function colorFromNormalized(array $normalized): ColorInterface
3637
}
3738

3839
/**
39-
* {@inheritdoc}
40-
*
41-
* @see ColorspaceInterface::importColor()
40+
* @param ColorInterface $color
41+
* @return ColorInterface
42+
* @throws ColorException
4243
*/
4344
public function importColor(ColorInterface $color): ColorInterface
4445
{
@@ -50,8 +51,17 @@ public function importColor(ColorInterface $color): ColorInterface
5051
};
5152
}
5253

53-
protected function importRgbColor(RgbColor $color): CmykColor
54+
/**
55+
* @param ColorInterface $color
56+
* @return Color
57+
* @throws ColorException
58+
*/
59+
protected function importRgbColor(ColorInterface $color): CmykColor
5460
{
61+
if (!($color instanceof RgbColor)) {
62+
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
63+
}
64+
5565
$c = (255 - $color->red()->value()) / 255.0 * 100;
5666
$m = (255 - $color->green()->value()) / 255.0 * 100;
5767
$y = (255 - $color->blue()->value()) / 255.0 * 100;

src/Colors/Hsl/Colorspace.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Intervention\Image\Colors\Rgb\Color as RgbColor;
99
use Intervention\Image\Colors\Hsv\Color as HsvColor;
1010
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
11+
use Intervention\Image\Exceptions\ColorException;
1112
use Intervention\Image\Interfaces\ColorInterface;
1213
use Intervention\Image\Interfaces\ColorspaceInterface;
1314

@@ -33,6 +34,11 @@ public function colorFromNormalized(array $normalized): ColorInterface
3334
return new Color(...$values);
3435
}
3536

37+
/**
38+
* @param ColorInterface $color
39+
* @return ColorInterface
40+
* @throws ColorException
41+
*/
3642
public function importColor(ColorInterface $color): ColorInterface
3743
{
3844
return match ($color::class) {
@@ -43,8 +49,17 @@ public function importColor(ColorInterface $color): ColorInterface
4349
};
4450
}
4551

46-
protected function importRgbColor(RgbColor $color): ColorInterface
52+
/**
53+
* @param ColorInterface $color
54+
* @return ColorInterface
55+
* @throws ColorException
56+
*/
57+
protected function importRgbColor(ColorInterface $color): ColorInterface
4758
{
59+
if (!($color instanceof RgbColor)) {
60+
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
61+
}
62+
4863
// normalized values of rgb channels
4964
$values = array_map(function ($channel) {
5065
return $channel->normalize();
@@ -84,8 +99,17 @@ protected function importRgbColor(RgbColor $color): ColorInterface
8499
);
85100
}
86101

87-
protected function importHsvColor(HsvColor $color): ColorInterface
102+
/**
103+
* @param ColorInterface $color
104+
* @return ColorInterface
105+
* @throws ColorException
106+
*/
107+
protected function importHsvColor(ColorInterface $color): ColorInterface
88108
{
109+
if (!($color instanceof HsvColor)) {
110+
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
111+
}
112+
89113
// normalized values of hsv channels
90114
list($h, $s, $v) = array_map(function ($channel) {
91115
return $channel->normalize();

src/Colors/Hsv/Colorspace.php

+24-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Intervention\Image\Colors\Rgb\Color as RgbColor;
99
use Intervention\Image\Colors\Hsl\Color as HslColor;
1010
use Intervention\Image\Colors\Rgb\Colorspace as RgbColorspace;
11+
use Intervention\Image\Exceptions\ColorException;
1112
use Intervention\Image\Interfaces\ColorInterface;
1213
use Intervention\Image\Interfaces\ColorspaceInterface;
1314

@@ -34,9 +35,9 @@ public function colorFromNormalized(array $normalized): ColorInterface
3435
}
3536

3637
/**
37-
* {@inheritdoc}
38-
*
39-
* @see ColorspaceInterface::importColor()
38+
* @param ColorInterface $color
39+
* @return ColorInterface
40+
* @throws ColorException
4041
*/
4142
public function importColor(ColorInterface $color): ColorInterface
4243
{
@@ -48,8 +49,17 @@ public function importColor(ColorInterface $color): ColorInterface
4849
};
4950
}
5051

51-
protected function importRgbColor(RgbColor $color): ColorInterface
52+
/**
53+
* @param ColorInterface $color
54+
* @return ColorInterface
55+
* @throws ColorException
56+
*/
57+
protected function importRgbColor(ColorInterface $color): ColorInterface
5258
{
59+
if (!($color instanceof RgbColor)) {
60+
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
61+
}
62+
5363
// normalized values of rgb channels
5464
$values = array_map(function ($channel) {
5565
return $channel->normalize();
@@ -89,8 +99,17 @@ protected function importRgbColor(RgbColor $color): ColorInterface
8999
);
90100
}
91101

92-
protected function importHslColor(HslColor $color): ColorInterface
102+
/**
103+
* @param ColorInterface $color
104+
* @return ColorInterface
105+
* @throws ColorException
106+
*/
107+
protected function importHslColor(ColorInterface $color): ColorInterface
93108
{
109+
if (!($color instanceof HslColor)) {
110+
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
111+
}
112+
94113
// normalized values of hsl channels
95114
list($h, $s, $l) = array_map(function ($channel) {
96115
return $channel->normalize();

src/Colors/Rgb/Colorspace.php

+34-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Intervention\Image\Colors\Hsv\Color as HsvColor;
88
use Intervention\Image\Colors\Hsl\Color as HslColor;
99
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
10+
use Intervention\Image\Exceptions\ColorException;
1011
use Intervention\Image\Interfaces\ColorInterface;
1112
use Intervention\Image\Interfaces\ColorspaceInterface;
1213

@@ -34,9 +35,9 @@ public function colorFromNormalized(array $normalized): ColorInterface
3435
}
3536

3637
/**
37-
* {@inheritdoc}
38-
*
39-
* @see ColorspaceInterface::importColor()
38+
* @param ColorInterface $color
39+
* @return ColorInterface
40+
* @throws ColorException
4041
*/
4142
public function importColor(ColorInterface $color): ColorInterface
4243
{
@@ -48,17 +49,35 @@ public function importColor(ColorInterface $color): ColorInterface
4849
};
4950
}
5051

51-
protected function importCmykColor(CmykColor $color): ColorInterface
52+
/**
53+
* @param ColorInterface $color
54+
* @return ColorInterface
55+
* @throws ColorException
56+
*/
57+
protected function importCmykColor(ColorInterface $color): ColorInterface
5258
{
59+
if (!($color instanceof CmykColor)) {
60+
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
61+
}
62+
5363
return new Color(
5464
(int) (255 * (1 - $color->cyan()->normalize()) * (1 - $color->key()->normalize())),
5565
(int) (255 * (1 - $color->magenta()->normalize()) * (1 - $color->key()->normalize())),
5666
(int) (255 * (1 - $color->yellow()->normalize()) * (1 - $color->key()->normalize())),
5767
);
5868
}
5969

60-
protected function importHsvColor(HsvColor $color): ColorInterface
70+
/**
71+
* @param ColorInterface $color
72+
* @return ColorInterface
73+
* @throws ColorException
74+
*/
75+
protected function importHsvColor(ColorInterface $color): ColorInterface
6176
{
77+
if (!($color instanceof HsvColor)) {
78+
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
79+
}
80+
6281
$chroma = $color->value()->normalize() * $color->saturation()->normalize();
6382
$hue = $color->hue()->normalize() * 6;
6483
$x = $chroma * (1 - abs(fmod($hue, 2) - 1));
@@ -83,8 +102,17 @@ protected function importHsvColor(HsvColor $color): ColorInterface
83102
return $this->colorFromNormalized($values);
84103
}
85104

86-
protected function importHslColor(HslColor $color): ColorInterface
105+
/**
106+
* @param ColorInterface $color
107+
* @return ColorInterface
108+
* @throws ColorException
109+
*/
110+
protected function importHslColor(ColorInterface $color): ColorInterface
87111
{
112+
if (!($color instanceof HslColor)) {
113+
throw new ColorException('Unabled to import color of type ' . $color::class . '.');
114+
}
115+
88116
// normalized values of hsl channels
89117
list($h, $s, $l) = array_map(function ($channel) {
90118
return $channel->normalize();

src/Format.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function fileExtensions(): array
9090
/**
9191
* Create an encoder instance that matches the format
9292
*
93-
* @param array $options
93+
* @param mixed $options
9494
* @return EncoderInterface
9595
*/
9696
public function encoder(mixed ...$options): EncoderInterface

src/Geometry/Point.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ public function setPosition(int $x, int $y): self
113113
* Rotate point ccw around pivot
114114
*
115115
* @param float $angle
116-
* @param Point $pivot
116+
* @param PointInterface $pivot
117117
* @return Point
118118
*/
119-
public function rotate(float $angle, self $pivot): self
119+
public function rotate(float $angle, PointInterface $pivot): self
120120
{
121121
$sin = round(sin(deg2rad($angle)), 6);
122122
$cos = round(cos(deg2rad($angle)), 6);
123123

124124
return $this->setPosition(
125-
intval($cos * ($this->x - $pivot->x) - $sin * ($this->y - $pivot->y) + $pivot->x),
126-
intval($sin * ($this->x - $pivot->x) + $cos * ($this->y - $pivot->y) + $pivot->y)
125+
intval($cos * ($this->x() - $pivot->x()) - $sin * ($this->y() - $pivot->y()) + $pivot->x()),
126+
intval($sin * ($this->x() - $pivot->x()) + $cos * ($this->y() - $pivot->y()) + $pivot->y())
127127
);
128128
}
129129
}

0 commit comments

Comments
 (0)