Skip to content

Commit

Permalink
Update code for phpstan level 6 (#1342)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
olivervogel committed May 2, 2024
1 parent 449d074 commit 2dbfb53
Show file tree
Hide file tree
Showing 41 changed files with 205 additions and 56 deletions.
2 changes: 1 addition & 1 deletion phpstan.dist.neon
@@ -1,5 +1,5 @@
parameters:
level: 5
level: 6
paths:
- src
exceptions:
Expand Down
35 changes: 30 additions & 5 deletions src/Collection.php
Expand Up @@ -10,23 +10,37 @@
use Traversable;
use IteratorAggregate;

/**
* @implements IteratorAggregate<int|string, mixed>
*/
class Collection implements CollectionInterface, IteratorAggregate, Countable
{
/**
* Create new collection object
*
* @param array<int|string, mixed> $items
* @return void
*/
public function __construct(protected array $items = [])
{
}

/**
* Static constructor
*
* @param array $items
* @return self
* @param array<int|string, mixed> $items
* @return self<int|string, mixed>
*/
public static function create(array $items = []): self
{
return new self($items);
}

/**
* {@inheritdoc}
*
* @see CollectionInterface::has()
*/
public function has(int|string $key): bool
{
return array_key_exists($key, $this->items);
Expand All @@ -35,13 +49,18 @@ public function has(int|string $key): bool
/**
* Returns Iterator
*
* @return Traversable
* @return Traversable<int|string, mixed>
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->items);
}

/**
* {@inheritdoc}
*
* @see CollectionInterface::toArray()
*/
public function toArray(): array
{
return $this->items;
Expand All @@ -61,7 +80,7 @@ public function count(): int
* Append new item to collection
*
* @param mixed $item
* @return CollectionInterface
* @return CollectionInterface<int|string, mixed>
*/
public function push($item): CollectionInterface
{
Expand Down Expand Up @@ -154,6 +173,12 @@ public function get(int|string $query, $default = null): mixed
return $result;
}

/**
* Map each item of collection by given callback
*
* @param callable $callback
* @return self
*/
public function map(callable $callback): self
{
$items = array_map(function ($item) use ($callback) {
Expand All @@ -167,7 +192,7 @@ public function map(callable $callback): self
* Run callback on each item of the collection an remove it if it does not return true
*
* @param callable $callback
* @return Collection
* @return self
*/
public function filter(callable $callback): self
{
Expand Down
17 changes: 17 additions & 0 deletions src/Colors/AbstractColor.php
Expand Up @@ -13,14 +13,26 @@ abstract class AbstractColor implements ColorInterface
{
/**
* Color channels
*
* @var array<ColorChannelInterface>
*/
protected array $channels;

/**
* {@inheritdoc}
*
* @see ColorInterface::channels()
*/
public function channels(): array
{
return $this->channels;
}

/**
* {@inheritdoc}
*
* @see ColorInterface::channel()
*/
public function channel(string $classname): ColorChannelInterface
{
$channels = array_filter($this->channels(), function (ColorChannelInterface $channel) use ($classname) {
Expand All @@ -34,6 +46,11 @@ public function channel(string $classname): ColorChannelInterface
return reset($channels);
}

/**
* {@inheritdoc}
*
* @see ColorInterface::normalize()
*/
public function normalize(): array
{
return array_map(function (ColorChannelInterface $channel) {
Expand Down
5 changes: 5 additions & 0 deletions src/Colors/Cmyk/Colorspace.php
Expand Up @@ -15,6 +15,11 @@

class Colorspace implements ColorspaceInterface
{
/**
* Channel class names of colorspace
*
* @var array<string>
*/
public static array $channels = [
Channels\Cyan::class,
Channels\Magenta::class,
Expand Down
5 changes: 5 additions & 0 deletions src/Colors/Hsl/Colorspace.php
Expand Up @@ -14,6 +14,11 @@

class Colorspace implements ColorspaceInterface
{
/**
* Channel class names of colorspace
*
* @var array<string>
*/
public static array $channels = [
Channels\Hue::class,
Channels\Saturation::class,
Expand Down
5 changes: 5 additions & 0 deletions src/Colors/Hsv/Colorspace.php
Expand Up @@ -14,6 +14,11 @@

class Colorspace implements ColorspaceInterface
{
/**
* Channel class names of colorspace
*
* @var array<string>
*/
public static array $channels = [
Channels\Hue::class,
Channels\Saturation::class,
Expand Down
5 changes: 5 additions & 0 deletions src/Colors/Rgb/Colorspace.php
Expand Up @@ -13,6 +13,11 @@

class Colorspace implements ColorspaceInterface
{
/**
* Channel class names of colorspace
*
* @var array<string>
*/
public static array $channels = [
Channels\Red::class,
Channels\Green::class,
Expand Down
5 changes: 5 additions & 0 deletions src/Colors/Rgb/Decoders/HtmlColornameDecoder.php
Expand Up @@ -11,6 +11,11 @@

class HtmlColornameDecoder extends HexColorDecoder implements DecoderInterface
{
/**
* Available color names and their corresponding hex codes
*
* @var array<string, string>
*/
protected static array $names = [
'lightsalmon' => '#ffa07a',
'salmon' => '#fa8072',
Expand Down
10 changes: 9 additions & 1 deletion src/Drivers/AbstractDecoder.php
Expand Up @@ -100,7 +100,7 @@ protected function isFile(mixed $input): bool
* data or a file path.
*
* @param string $path_or_data
* @return CollectionInterface
* @return CollectionInterface<string, mixed>
*/
protected function extractExifData(string $path_or_data): CollectionInterface
{
Expand Down Expand Up @@ -156,9 +156,17 @@ protected function parseDataUri(mixed $input): object

return new class ($matches, $result)
{
/**
* @var array<mixed>
*/
private array $matches;
private int|false $result;

/**
* @param array<mixed> $matches
* @param int|false $result
* @return void
*/
public function __construct(array $matches, int|false $result)
{
$this->matches = $matches;
Expand Down
4 changes: 0 additions & 4 deletions src/Drivers/AbstractDriver.php
Expand Up @@ -6,7 +6,6 @@

use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\AnalyzerInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\DriverInterface;
Expand Down Expand Up @@ -74,9 +73,6 @@ public function specializeMultiple(array $objects): array
match (true) {
is_string($object) => new $object(),
is_object($object) => $object,
default => throw new RuntimeException(
'Specializable item must be either a class name or an object.'
)
}
);
}, $objects);
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/AbstractFontProcessor.php
Expand Up @@ -114,7 +114,7 @@ protected function wrapTextBlock(TextBlock $block, FontInterface $font): TextBlo
* @param Line $line
* @param FontInterface $font
* @throws FontException
* @return array
* @return array<Line>
*/
protected function wrapLine(Line $line, FontInterface $font): array
{
Expand Down
27 changes: 22 additions & 5 deletions src/Drivers/AbstractInputHandler.php
Expand Up @@ -6,13 +6,25 @@

use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\InputHandlerInterface;

abstract class AbstractInputHandler implements InputHandlerInterface
{
/**
* Decoder classnames in hierarchical order
*
* @var array<string|DecoderInterface>
*/
protected array $decoders = [];

/**
* Create new input handler instance with given decoder classnames
*
* @param array<string|DecoderInterface> $decoders
* @return void
*/
public function __construct(array $decoders = [])
{
$this->decoders = count($decoders) ? $decoders : $this->decoders;
Expand Down Expand Up @@ -40,13 +52,18 @@ protected function chain(): AbstractDecoder
throw new DecoderException('No decoders found in ' . $this::class);
}

// get instance of last decoder in stack
list($classname) = array_slice(array_reverse($this->decoders), 0, 1);
$chain = new $classname();
// get last decoder in stack
list($decoder) = array_slice(array_reverse($this->decoders), 0, 1);
$chain = ($decoder instanceof DecoderInterface) ? $decoder : new $decoder();

// only accept DecoderInterface
if (!($chain instanceof DecoderInterface)) {
throw new DecoderException('Decoder must implement in ' . DecoderInterface::class);
}

// build decoder chain
foreach (array_slice(array_reverse($this->decoders), 1) as $classname) {
$chain = new $classname($chain);
foreach (array_slice(array_reverse($this->decoders), 1) as $decoder) {
$chain = ($decoder instanceof DecoderInterface) ? new ($decoder::class)($chain) : new $decoder($chain);
}

return $chain;
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Gd/Driver.php
Expand Up @@ -87,7 +87,7 @@ public function __construct(
/**
* @throws RuntimeException
*/
public function add($source, float $delay = 1): self
public function add(mixed $source, float $delay = 1): self
{
$this->core->add(
$this->driver->handleInput($source)->core()->first()->setDelay($delay)
Expand Down
5 changes: 5 additions & 0 deletions src/Drivers/Gd/InputHandler.php
Expand Up @@ -24,6 +24,11 @@

class InputHandler extends AbstractInputHandler
{
/**
* Decoders in hierarchical order
*
* @var array<string>
*/
protected array $decoders = [
NativeObjectDecoder::class,
ImageObjectDecoder::class,
Expand Down
6 changes: 3 additions & 3 deletions src/Drivers/Gd/Modifiers/FillModifier.php
Expand Up @@ -4,8 +4,8 @@

namespace Intervention\Image\Drivers\Gd\Modifiers;

use Intervention\Image\Drivers\Gd\Frame;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\FillModifier as GenericFillModifier;
Expand Down Expand Up @@ -37,7 +37,7 @@ private function color(ImageInterface $image): int
);
}

private function floodFillWithColor(Frame $frame, int $color): void
private function floodFillWithColor(FrameInterface $frame, int $color): void
{
imagefill(
$frame->native(),
Expand All @@ -47,7 +47,7 @@ private function floodFillWithColor(Frame $frame, int $color): void
);
}

private function fillAllWithColor(Frame $frame, int $color): void
private function fillAllWithColor(FrameInterface $frame, int $color): void
{
imagealphablending($frame->native(), true);
imagefilledrectangle(
Expand Down
5 changes: 5 additions & 0 deletions src/Drivers/Gd/Modifiers/SharpenModifier.php
Expand Up @@ -20,6 +20,11 @@ public function apply(ImageInterface $image): ImageInterface
return $image;
}

/**
* Create matrix to be used by imageconvolution()
*
* @return array<array<float>>
*/
private function matrix(): array
{
$min = $this->amount >= 10 ? $this->amount * -0.01 : 0;
Expand Down
3 changes: 3 additions & 0 deletions src/Drivers/Imagick/Core.php
Expand Up @@ -12,6 +12,9 @@
use Intervention\Image\Interfaces\CollectionInterface;
use Intervention\Image\Interfaces\FrameInterface;

/**
* @implements Iterator<FrameInterface>
*/
class Core implements CoreInterface, Iterator
{
protected int $iteratorIndex = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Driver.php
Expand Up @@ -89,7 +89,7 @@ public function __construct(
/**
* @throws RuntimeException
*/
public function add($source, float $delay = 1): self
public function add(mixed $source, float $delay = 1): self
{
$native = $this->driver->handleInput($source)->core()->native();
$native->setImageDelay(intval(round($delay * 100)));
Expand Down
5 changes: 5 additions & 0 deletions src/Drivers/Imagick/InputHandler.php
Expand Up @@ -24,6 +24,11 @@

class InputHandler extends AbstractInputHandler
{
/**
* Decoders in hierarchical order
*
* @var array<string>
*/
protected array $decoders = [
NativeObjectDecoder::class,
ImageObjectDecoder::class,
Expand Down

0 comments on commit 2dbfb53

Please sign in to comment.