From 27512eedb325313f3646d64605c23d7de9a46056 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 9 Jun 2024 11:59:28 +0200 Subject: [PATCH] Change typehints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change the typing. “callable” is too general, as classes are also included here. “Closure” is actually sufficient, but “callable” was left in the outer interface so as not to create a breaking change. In the underlying factories, however, only “Closure” is still possible. --- src/Geometry/Factories/BezierFactory.php | 7 ++--- src/Geometry/Factories/CircleFactory.php | 7 ++--- src/Geometry/Factories/EllipseFactory.php | 7 ++--- src/Geometry/Factories/LineFactory.php | 7 ++--- src/Geometry/Factories/PolygonFactory.php | 7 ++--- src/Geometry/Factories/RectangleFactory.php | 7 ++--- src/Image.php | 15 ++++++----- src/Interfaces/DrawableFactoryInterface.php | 6 +++-- src/Interfaces/ImageInterface.php | 29 +++++++++++---------- src/Typography/FontFactory.php | 5 ++-- 10 files changed, 54 insertions(+), 43 deletions(-) diff --git a/src/Geometry/Factories/BezierFactory.php b/src/Geometry/Factories/BezierFactory.php index 07450a45..cfbb5836 100644 --- a/src/Geometry/Factories/BezierFactory.php +++ b/src/Geometry/Factories/BezierFactory.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Intervention\Image\Geometry\Factories; +use Closure; use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Bezier; use Intervention\Image\Interfaces\DrawableFactoryInterface; @@ -16,10 +17,10 @@ class BezierFactory implements DrawableFactoryInterface /** * Create new factory instance * - * @param null|callable|Bezier $init + * @param null|Closure|Bezier $init * @return void */ - public function __construct(null|callable|Bezier $init = null) + public function __construct(null|Closure|Bezier $init = null) { $this->bezier = is_a($init, Bezier::class) ? $init : new Bezier([]); @@ -33,7 +34,7 @@ class BezierFactory implements DrawableFactoryInterface * * @see DrawableFactoryInterface::init() */ - public static function init(null|callable|DrawableInterface $init = null): self + public static function init(null|Closure|DrawableInterface $init = null): self { return new self($init); } diff --git a/src/Geometry/Factories/CircleFactory.php b/src/Geometry/Factories/CircleFactory.php index 9f52b37f..5ae252c0 100644 --- a/src/Geometry/Factories/CircleFactory.php +++ b/src/Geometry/Factories/CircleFactory.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Intervention\Image\Geometry\Factories; +use Closure; use Intervention\Image\Geometry\Circle; use Intervention\Image\Geometry\Point; use Intervention\Image\Interfaces\DrawableFactoryInterface; @@ -18,12 +19,12 @@ class CircleFactory implements DrawableFactoryInterface * Create new factory instance * * @param PointInterface $pivot - * @param null|callable|Circle $init + * @param null|Closure|Circle $init * @return void */ public function __construct( protected PointInterface $pivot = new Point(), - null|callable|Circle $init = null, + null|Closure|Circle $init = null, ) { $this->circle = is_a($init, Circle::class) ? $init : new Circle(0); $this->circle->setPosition($pivot); @@ -38,7 +39,7 @@ class CircleFactory implements DrawableFactoryInterface * * @see DrawableFactoryInterface::init() */ - public static function init(null|callable|DrawableInterface $init = null): self + public static function init(null|Closure|DrawableInterface $init = null): self { return new self(init: $init); } diff --git a/src/Geometry/Factories/EllipseFactory.php b/src/Geometry/Factories/EllipseFactory.php index 6337181c..2df3556b 100644 --- a/src/Geometry/Factories/EllipseFactory.php +++ b/src/Geometry/Factories/EllipseFactory.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Intervention\Image\Geometry\Factories; +use Closure; use Intervention\Image\Geometry\Ellipse; use Intervention\Image\Geometry\Point; use Intervention\Image\Interfaces\DrawableFactoryInterface; @@ -18,12 +19,12 @@ class EllipseFactory implements DrawableFactoryInterface * Create new factory instance * * @param PointInterface $pivot - * @param null|callable|Ellipse $init + * @param null|Closure|Ellipse $init * @return void */ public function __construct( protected PointInterface $pivot = new Point(), - null|callable|Ellipse $init = null, + null|Closure|Ellipse $init = null, ) { $this->ellipse = is_a($init, Ellipse::class) ? $init : new Ellipse(0, 0); $this->ellipse->setPosition($pivot); @@ -38,7 +39,7 @@ class EllipseFactory implements DrawableFactoryInterface * * @see DrawableFactoryInterface::init() */ - public static function init(null|callable|DrawableInterface $init = null): self + public static function init(null|Closure|DrawableInterface $init = null): self { return new self(init: $init); } diff --git a/src/Geometry/Factories/LineFactory.php b/src/Geometry/Factories/LineFactory.php index c2752096..e78f855a 100644 --- a/src/Geometry/Factories/LineFactory.php +++ b/src/Geometry/Factories/LineFactory.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Intervention\Image\Geometry\Factories; +use Closure; use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Line; use Intervention\Image\Interfaces\DrawableFactoryInterface; @@ -16,10 +17,10 @@ class LineFactory implements DrawableFactoryInterface /** * Create the factory instance * - * @param null|callable|Line $init + * @param null|Closure|Line $init * @return void */ - public function __construct(null|callable|Line $init = null) + public function __construct(null|Closure|Line $init = null) { $this->line = is_a($init, Line::class) ? $init : new Line(new Point(), new Point()); @@ -33,7 +34,7 @@ class LineFactory implements DrawableFactoryInterface * * @see DrawableFactoryInterface::init() */ - public static function init(null|callable|DrawableInterface $init = null): self + public static function init(null|Closure|DrawableInterface $init = null): self { return new self($init); } diff --git a/src/Geometry/Factories/PolygonFactory.php b/src/Geometry/Factories/PolygonFactory.php index 6ee80c4c..bc006f79 100644 --- a/src/Geometry/Factories/PolygonFactory.php +++ b/src/Geometry/Factories/PolygonFactory.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Intervention\Image\Geometry\Factories; +use Closure; use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Polygon; use Intervention\Image\Interfaces\DrawableFactoryInterface; @@ -16,10 +17,10 @@ class PolygonFactory implements DrawableFactoryInterface /** * Create new factory instance * - * @param null|callable|Polygon $init + * @param null|Closure|Polygon $init * @return void */ - public function __construct(null|callable|Polygon $init = null) + public function __construct(null|Closure|Polygon $init = null) { $this->polygon = is_a($init, Polygon::class) ? $init : new Polygon([]); @@ -33,7 +34,7 @@ class PolygonFactory implements DrawableFactoryInterface * * @see DrawableFactoryInterface::init() */ - public static function init(null|callable|DrawableInterface $init = null): self + public static function init(null|Closure|DrawableInterface $init = null): self { return new self($init); } diff --git a/src/Geometry/Factories/RectangleFactory.php b/src/Geometry/Factories/RectangleFactory.php index e8c3664a..29f5a3b4 100644 --- a/src/Geometry/Factories/RectangleFactory.php +++ b/src/Geometry/Factories/RectangleFactory.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Intervention\Image\Geometry\Factories; +use Closure; use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Interfaces\DrawableFactoryInterface; @@ -18,12 +19,12 @@ class RectangleFactory implements DrawableFactoryInterface * Create new instance * * @param PointInterface $pivot - * @param null|callable|Rectangle $init + * @param null|Closure|Rectangle $init * @return void */ public function __construct( protected PointInterface $pivot = new Point(), - null|callable|Rectangle $init = null, + null|Closure|Rectangle $init = null, ) { $this->rectangle = is_a($init, Rectangle::class) ? $init : new Rectangle(0, 0, $pivot); $this->rectangle->setPosition($pivot); @@ -38,7 +39,7 @@ class RectangleFactory implements DrawableFactoryInterface * * @see DrawableFactoryInterface::init() */ - public static function init(null|callable|DrawableInterface $init = null): self + public static function init(null|Closure|DrawableInterface $init = null): self { return new self(init: $init); } diff --git a/src/Image.php b/src/Image.php index 360c967e..126df170 100644 --- a/src/Image.php +++ b/src/Image.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Intervention\Image; +use Closure; use Intervention\Image\Exceptions\RuntimeException; use Traversable; use Intervention\Image\Analyzers\ColorspaceAnalyzer; @@ -618,7 +619,7 @@ final class Image implements ImageInterface * * @see ImageInterface::text() */ - public function text(string $text, int $x, int $y, callable|FontInterface $font): ImageInterface + public function text(string $text, int $x, int $y, callable|Closure|FontInterface $font): ImageInterface { return $this->modify( new TextModifier( @@ -816,7 +817,7 @@ final class Image implements ImageInterface * * @see ImageInterface::drawRectangle() */ - public function drawRectangle(int $x, int $y, callable|Rectangle $init): ImageInterface + public function drawRectangle(int $x, int $y, callable|Closure|Rectangle $init): ImageInterface { return $this->modify( new DrawRectangleModifier( @@ -830,7 +831,7 @@ final class Image implements ImageInterface * * @see ImageInterface::drawEllipse() */ - public function drawEllipse(int $x, int $y, callable|Ellipse $init): ImageInterface + public function drawEllipse(int $x, int $y, callable|Closure|Ellipse $init): ImageInterface { return $this->modify( new DrawEllipseModifier( @@ -844,7 +845,7 @@ final class Image implements ImageInterface * * @see ImageInterface::drawCircle() */ - public function drawCircle(int $x, int $y, callable|Circle $init): ImageInterface + public function drawCircle(int $x, int $y, callable|Closure|Circle $init): ImageInterface { return $this->modify( new DrawEllipseModifier( @@ -858,7 +859,7 @@ final class Image implements ImageInterface * * @see ImageInterface::drawPolygon() */ - public function drawPolygon(callable|Polygon $init): ImageInterface + public function drawPolygon(callable|Closure|Polygon $init): ImageInterface { return $this->modify( new DrawPolygonModifier( @@ -872,7 +873,7 @@ final class Image implements ImageInterface * * @see ImageInterface::drawLine() */ - public function drawLine(callable|Line $init): ImageInterface + public function drawLine(callable|Closure|Line $init): ImageInterface { return $this->modify( new DrawLineModifier( @@ -886,7 +887,7 @@ final class Image implements ImageInterface * * @see ImageInterface::drawBezier() */ - public function drawBezier(callable|Bezier $init): ImageInterface + public function drawBezier(callable|Closure|Bezier $init): ImageInterface { return $this->modify( new DrawBezierModifier( diff --git a/src/Interfaces/DrawableFactoryInterface.php b/src/Interfaces/DrawableFactoryInterface.php index f4de9d6b..dd22bdae 100644 --- a/src/Interfaces/DrawableFactoryInterface.php +++ b/src/Interfaces/DrawableFactoryInterface.php @@ -4,15 +4,17 @@ declare(strict_types=1); namespace Intervention\Image\Interfaces; +use Closure; + interface DrawableFactoryInterface { /** * Create a new factory instance statically * - * @param null|callable|DrawableInterface $init + * @param null|Closure|DrawableInterface $init * @return DrawableFactoryInterface */ - public static function init(null|callable|DrawableInterface $init = null): self; + public static function init(null|Closure|DrawableInterface $init = null): self; /** * Create the end product of the factory diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index 13a1a515..726c5207 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Intervention\Image\Interfaces; +use Closure; use Countable; use Intervention\Image\Encoders\AutoEncoder; use Intervention\Image\Exceptions\AnimationException; @@ -453,11 +454,11 @@ interface ImageInterface extends IteratorAggregate, Countable * @param string $text * @param int $x * @param int $y - * @param callable|FontInterface $font + * @param callable|Closure|FontInterface $font * @throws RuntimeException * @return ImageInterface */ - public function text(string $text, int $x, int $y, callable|FontInterface $font): self; + public function text(string $text, int $x, int $y, callable|Closure|FontInterface $font): self; /** * Resize image to the given width and/or height @@ -705,11 +706,11 @@ interface ImageInterface extends IteratorAggregate, Countable * @link https://image.intervention.io/v3/modifying/drawing#drawing-a-rectangle * @param int $x * @param int $y - * @param callable|Rectangle $init + * @param callable|Closure|Rectangle $init * @throws RuntimeException * @return ImageInterface */ - public function drawRectangle(int $x, int $y, callable|Rectangle $init): self; + public function drawRectangle(int $x, int $y, callable|Closure|Rectangle $init): self; /** * Draw ellipse on the current image @@ -717,11 +718,11 @@ interface ImageInterface extends IteratorAggregate, Countable * @link https://image.intervention.io/v3/modifying/drawing#drawing-ellipses * @param int $x * @param int $y - * @param callable|Ellipse $init + * @param callable|Closure|Ellipse $init * @throws RuntimeException * @return ImageInterface */ - public function drawEllipse(int $x, int $y, callable|Ellipse $init): self; + public function drawEllipse(int $x, int $y, callable|Closure|Ellipse $init): self; /** * Draw circle on the current image @@ -729,41 +730,41 @@ interface ImageInterface extends IteratorAggregate, Countable * @link https://image.intervention.io/v3/modifying/drawing#drawing-a-circle * @param int $x * @param int $y - * @param callable|Circle $init + * @param callable|Closure|Circle $init * @throws RuntimeException * @return ImageInterface */ - public function drawCircle(int $x, int $y, callable|Circle $init): self; + public function drawCircle(int $x, int $y, callable|Closure|Circle $init): self; /** * Draw a polygon on the current image * * @link https://image.intervention.io/v3/modifying/drawing#drawing-a-polygon - * @param callable|Polygon $init + * @param callable|Closure|Polygon $init * @throws RuntimeException * @return ImageInterface */ - public function drawPolygon(callable|Polygon $init): self; + public function drawPolygon(callable|Closure|Polygon $init): self; /** * Draw a line on the current image * * @link https://image.intervention.io/v3/modifying/drawing#drawing-a-line - * @param callable|Line $init + * @param callable|Closure|Line $init * @throws RuntimeException * @return ImageInterface */ - public function drawLine(callable|Line $init): self; + public function drawLine(callable|Closure|Line $init): self; /** * Draw a bezier curve on the current image * * @link https://image.intervention.io/v3/modifying/drawing#draw-bezier-curves - * @param callable|Bezier $init + * @param callable|Closure|Bezier $init * @throws RuntimeException * @return ImageInterface */ - public function drawBezier(callable|Bezier $init): self; + public function drawBezier(callable|Closure|Bezier $init): self; /** * Encode image to given media (mime) type. If no type is given the image diff --git a/src/Typography/FontFactory.php b/src/Typography/FontFactory.php index d6738f41..98d6dadd 100644 --- a/src/Typography/FontFactory.php +++ b/src/Typography/FontFactory.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Intervention\Image\Typography; +use Closure; use Intervention\Image\Exceptions\FontException; use Intervention\Image\Interfaces\FontInterface; @@ -14,11 +15,11 @@ class FontFactory /** * Create new instance * - * @param callable|FontInterface $init + * @param Closure|FontInterface $init * @throws FontException * @return void */ - public function __construct(callable|FontInterface $init) + public function __construct(callable|Closure|FontInterface $init) { $this->font = is_a($init, FontInterface::class) ? $init : new Font();