Skip to content

Commit 2dbfb53

Browse files
authored
Update code for phpstan level 6 (#1342)
* Update phpstan checks to level 6 * Add more details doc blocks to meet phpstan level 6 * Fix bug in building decoder chain * Fix type hints
1 parent 449d074 commit 2dbfb53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+205
-56
lines changed

phpstan.dist.neon

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

src/Collection.php

+30-5
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,37 @@
1010
use Traversable;
1111
use IteratorAggregate;
1212

13+
/**
14+
* @implements IteratorAggregate<int|string, mixed>
15+
*/
1316
class Collection implements CollectionInterface, IteratorAggregate, Countable
1417
{
18+
/**
19+
* Create new collection object
20+
*
21+
* @param array<int|string, mixed> $items
22+
* @return void
23+
*/
1524
public function __construct(protected array $items = [])
1625
{
1726
}
1827

1928
/**
2029
* Static constructor
2130
*
22-
* @param array $items
23-
* @return self
31+
* @param array<int|string, mixed> $items
32+
* @return self<int|string, mixed>
2433
*/
2534
public static function create(array $items = []): self
2635
{
2736
return new self($items);
2837
}
2938

39+
/**
40+
* {@inheritdoc}
41+
*
42+
* @see CollectionInterface::has()
43+
*/
3044
public function has(int|string $key): bool
3145
{
3246
return array_key_exists($key, $this->items);
@@ -35,13 +49,18 @@ public function has(int|string $key): bool
3549
/**
3650
* Returns Iterator
3751
*
38-
* @return Traversable
52+
* @return Traversable<int|string, mixed>
3953
*/
4054
public function getIterator(): Traversable
4155
{
4256
return new ArrayIterator($this->items);
4357
}
4458

59+
/**
60+
* {@inheritdoc}
61+
*
62+
* @see CollectionInterface::toArray()
63+
*/
4564
public function toArray(): array
4665
{
4766
return $this->items;
@@ -61,7 +80,7 @@ public function count(): int
6180
* Append new item to collection
6281
*
6382
* @param mixed $item
64-
* @return CollectionInterface
83+
* @return CollectionInterface<int|string, mixed>
6584
*/
6685
public function push($item): CollectionInterface
6786
{
@@ -154,6 +173,12 @@ public function get(int|string $query, $default = null): mixed
154173
return $result;
155174
}
156175

176+
/**
177+
* Map each item of collection by given callback
178+
*
179+
* @param callable $callback
180+
* @return self
181+
*/
157182
public function map(callable $callback): self
158183
{
159184
$items = array_map(function ($item) use ($callback) {
@@ -167,7 +192,7 @@ public function map(callable $callback): self
167192
* Run callback on each item of the collection an remove it if it does not return true
168193
*
169194
* @param callable $callback
170-
* @return Collection
195+
* @return self
171196
*/
172197
public function filter(callable $callback): self
173198
{

src/Colors/AbstractColor.php

+17
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,26 @@ abstract class AbstractColor implements ColorInterface
1313
{
1414
/**
1515
* Color channels
16+
*
17+
* @var array<ColorChannelInterface>
1618
*/
1719
protected array $channels;
1820

21+
/**
22+
* {@inheritdoc}
23+
*
24+
* @see ColorInterface::channels()
25+
*/
1926
public function channels(): array
2027
{
2128
return $this->channels;
2229
}
2330

31+
/**
32+
* {@inheritdoc}
33+
*
34+
* @see ColorInterface::channel()
35+
*/
2436
public function channel(string $classname): ColorChannelInterface
2537
{
2638
$channels = array_filter($this->channels(), function (ColorChannelInterface $channel) use ($classname) {
@@ -34,6 +46,11 @@ public function channel(string $classname): ColorChannelInterface
3446
return reset($channels);
3547
}
3648

49+
/**
50+
* {@inheritdoc}
51+
*
52+
* @see ColorInterface::normalize()
53+
*/
3754
public function normalize(): array
3855
{
3956
return array_map(function (ColorChannelInterface $channel) {

src/Colors/Cmyk/Colorspace.php

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616
class Colorspace implements ColorspaceInterface
1717
{
18+
/**
19+
* Channel class names of colorspace
20+
*
21+
* @var array<string>
22+
*/
1823
public static array $channels = [
1924
Channels\Cyan::class,
2025
Channels\Magenta::class,

src/Colors/Hsl/Colorspace.php

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
class Colorspace implements ColorspaceInterface
1616
{
17+
/**
18+
* Channel class names of colorspace
19+
*
20+
* @var array<string>
21+
*/
1722
public static array $channels = [
1823
Channels\Hue::class,
1924
Channels\Saturation::class,

src/Colors/Hsv/Colorspace.php

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
class Colorspace implements ColorspaceInterface
1616
{
17+
/**
18+
* Channel class names of colorspace
19+
*
20+
* @var array<string>
21+
*/
1722
public static array $channels = [
1823
Channels\Hue::class,
1924
Channels\Saturation::class,

src/Colors/Rgb/Colorspace.php

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
class Colorspace implements ColorspaceInterface
1515
{
16+
/**
17+
* Channel class names of colorspace
18+
*
19+
* @var array<string>
20+
*/
1621
public static array $channels = [
1722
Channels\Red::class,
1823
Channels\Green::class,

src/Colors/Rgb/Decoders/HtmlColornameDecoder.php

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
class HtmlColornameDecoder extends HexColorDecoder implements DecoderInterface
1313
{
14+
/**
15+
* Available color names and their corresponding hex codes
16+
*
17+
* @var array<string, string>
18+
*/
1419
protected static array $names = [
1520
'lightsalmon' => '#ffa07a',
1621
'salmon' => '#fa8072',

src/Drivers/AbstractDecoder.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected function isFile(mixed $input): bool
100100
* data or a file path.
101101
*
102102
* @param string $path_or_data
103-
* @return CollectionInterface
103+
* @return CollectionInterface<string, mixed>
104104
*/
105105
protected function extractExifData(string $path_or_data): CollectionInterface
106106
{
@@ -156,9 +156,17 @@ protected function parseDataUri(mixed $input): object
156156

157157
return new class ($matches, $result)
158158
{
159+
/**
160+
* @var array<mixed>
161+
*/
159162
private array $matches;
160163
private int|false $result;
161164

165+
/**
166+
* @param array<mixed> $matches
167+
* @param int|false $result
168+
* @return void
169+
*/
162170
public function __construct(array $matches, int|false $result)
163171
{
164172
$this->matches = $matches;

src/Drivers/AbstractDriver.php

-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Intervention\Image\Exceptions\DriverException;
88
use Intervention\Image\Exceptions\NotSupportedException;
9-
use Intervention\Image\Exceptions\RuntimeException;
109
use Intervention\Image\Interfaces\AnalyzerInterface;
1110
use Intervention\Image\Interfaces\DecoderInterface;
1211
use Intervention\Image\Interfaces\DriverInterface;
@@ -74,9 +73,6 @@ public function specializeMultiple(array $objects): array
7473
match (true) {
7574
is_string($object) => new $object(),
7675
is_object($object) => $object,
77-
default => throw new RuntimeException(
78-
'Specializable item must be either a class name or an object.'
79-
)
8076
}
8177
);
8278
}, $objects);

src/Drivers/AbstractFontProcessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected function wrapTextBlock(TextBlock $block, FontInterface $font): TextBlo
114114
* @param Line $line
115115
* @param FontInterface $font
116116
* @throws FontException
117-
* @return array
117+
* @return array<Line>
118118
*/
119119
protected function wrapLine(Line $line, FontInterface $font): array
120120
{

src/Drivers/AbstractInputHandler.php

+22-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,25 @@
66

77
use Intervention\Image\Exceptions\DecoderException;
88
use Intervention\Image\Interfaces\ColorInterface;
9+
use Intervention\Image\Interfaces\DecoderInterface;
910
use Intervention\Image\Interfaces\ImageInterface;
1011
use Intervention\Image\Interfaces\InputHandlerInterface;
1112

1213
abstract class AbstractInputHandler implements InputHandlerInterface
1314
{
15+
/**
16+
* Decoder classnames in hierarchical order
17+
*
18+
* @var array<string|DecoderInterface>
19+
*/
1420
protected array $decoders = [];
1521

22+
/**
23+
* Create new input handler instance with given decoder classnames
24+
*
25+
* @param array<string|DecoderInterface> $decoders
26+
* @return void
27+
*/
1628
public function __construct(array $decoders = [])
1729
{
1830
$this->decoders = count($decoders) ? $decoders : $this->decoders;
@@ -40,13 +52,18 @@ protected function chain(): AbstractDecoder
4052
throw new DecoderException('No decoders found in ' . $this::class);
4153
}
4254

43-
// get instance of last decoder in stack
44-
list($classname) = array_slice(array_reverse($this->decoders), 0, 1);
45-
$chain = new $classname();
55+
// get last decoder in stack
56+
list($decoder) = array_slice(array_reverse($this->decoders), 0, 1);
57+
$chain = ($decoder instanceof DecoderInterface) ? $decoder : new $decoder();
58+
59+
// only accept DecoderInterface
60+
if (!($chain instanceof DecoderInterface)) {
61+
throw new DecoderException('Decoder must implement in ' . DecoderInterface::class);
62+
}
4663

4764
// build decoder chain
48-
foreach (array_slice(array_reverse($this->decoders), 1) as $classname) {
49-
$chain = new $classname($chain);
65+
foreach (array_slice(array_reverse($this->decoders), 1) as $decoder) {
66+
$chain = ($decoder instanceof DecoderInterface) ? new ($decoder::class)($chain) : new $decoder($chain);
5067
}
5168

5269
return $chain;

src/Drivers/Gd/Driver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function __construct(
8787
/**
8888
* @throws RuntimeException
8989
*/
90-
public function add($source, float $delay = 1): self
90+
public function add(mixed $source, float $delay = 1): self
9191
{
9292
$this->core->add(
9393
$this->driver->handleInput($source)->core()->first()->setDelay($delay)

src/Drivers/Gd/InputHandler.php

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424

2525
class InputHandler extends AbstractInputHandler
2626
{
27+
/**
28+
* Decoders in hierarchical order
29+
*
30+
* @var array<string>
31+
*/
2732
protected array $decoders = [
2833
NativeObjectDecoder::class,
2934
ImageObjectDecoder::class,

src/Drivers/Gd/Modifiers/FillModifier.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Intervention\Image\Drivers\Gd\Modifiers;
66

7-
use Intervention\Image\Drivers\Gd\Frame;
87
use Intervention\Image\Exceptions\RuntimeException;
8+
use Intervention\Image\Interfaces\FrameInterface;
99
use Intervention\Image\Interfaces\ImageInterface;
1010
use Intervention\Image\Interfaces\SpecializedInterface;
1111
use Intervention\Image\Modifiers\FillModifier as GenericFillModifier;
@@ -37,7 +37,7 @@ private function color(ImageInterface $image): int
3737
);
3838
}
3939

40-
private function floodFillWithColor(Frame $frame, int $color): void
40+
private function floodFillWithColor(FrameInterface $frame, int $color): void
4141
{
4242
imagefill(
4343
$frame->native(),
@@ -47,7 +47,7 @@ private function floodFillWithColor(Frame $frame, int $color): void
4747
);
4848
}
4949

50-
private function fillAllWithColor(Frame $frame, int $color): void
50+
private function fillAllWithColor(FrameInterface $frame, int $color): void
5151
{
5252
imagealphablending($frame->native(), true);
5353
imagefilledrectangle(

src/Drivers/Gd/Modifiers/SharpenModifier.php

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public function apply(ImageInterface $image): ImageInterface
2020
return $image;
2121
}
2222

23+
/**
24+
* Create matrix to be used by imageconvolution()
25+
*
26+
* @return array<array<float>>
27+
*/
2328
private function matrix(): array
2429
{
2530
$min = $this->amount >= 10 ? $this->amount * -0.01 : 0;

src/Drivers/Imagick/Core.php

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use Intervention\Image\Interfaces\CollectionInterface;
1313
use Intervention\Image\Interfaces\FrameInterface;
1414

15+
/**
16+
* @implements Iterator<FrameInterface>
17+
*/
1518
class Core implements CoreInterface, Iterator
1619
{
1720
protected int $iteratorIndex = 0;

src/Drivers/Imagick/Driver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function __construct(
8989
/**
9090
* @throws RuntimeException
9191
*/
92-
public function add($source, float $delay = 1): self
92+
public function add(mixed $source, float $delay = 1): self
9393
{
9494
$native = $this->driver->handleInput($source)->core()->native();
9595
$native->setImageDelay(intval(round($delay * 100)));

src/Drivers/Imagick/InputHandler.php

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424

2525
class InputHandler extends AbstractInputHandler
2626
{
27+
/**
28+
* Decoders in hierarchical order
29+
*
30+
* @var array<string>
31+
*/
2732
protected array $decoders = [
2833
NativeObjectDecoder::class,
2934
ImageObjectDecoder::class,

0 commit comments

Comments
 (0)