From 51251be24fe6415ef3275a7d47ab5c44c8b5674b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Sun, 17 Mar 2024 18:47:21 +0100 Subject: [PATCH] fix: Leverage PHP 7.4 syntax (#226) --- src/Fonts/FontFile.php | 2 +- src/Fonts/TrueTypeFontFile.php | 6 ++--- src/Nodes/SVGGenericNodeType.php | 2 +- src/Nodes/SVGNode.php | 11 +++++---- src/Nodes/SVGNodeContainer.php | 4 ++-- src/Nodes/Shapes/SVGPath.php | 7 +++--- src/Nodes/Structures/SVGDocumentFragment.php | 2 +- src/Rasterization/Path/PathApproximator.php | 12 +++++----- src/Rasterization/Path/PathParser.php | 2 +- src/Rasterization/Path/PolygonBuilder.php | 6 ++--- .../Renderers/PathRendererEdge.php | 24 ++++++------------- src/Rasterization/SVGRasterizer.php | 4 ++-- src/Rasterization/Transform/Transform.php | 2 +- src/Reading/AttributeRegistry.php | 4 ++-- src/Reading/LengthAttributeConverter.php | 5 ++-- src/Reading/NodeRegistry.php | 2 +- src/SVG.php | 2 +- src/Shims/Str.php | 2 +- src/Utilities/Colors/ColorLookup.php | 8 ++----- src/Writing/SVGWriter.php | 2 +- 20 files changed, 47 insertions(+), 62 deletions(-) diff --git a/src/Fonts/FontFile.php b/src/Fonts/FontFile.php index a1cc33d0..54d5894e 100644 --- a/src/Fonts/FontFile.php +++ b/src/Fonts/FontFile.php @@ -7,7 +7,7 @@ */ abstract class FontFile { - private $path; + private string $path; public function __construct(string $path) { diff --git a/src/Fonts/TrueTypeFontFile.php b/src/Fonts/TrueTypeFontFile.php index 1ffa760c..93586a3f 100644 --- a/src/Fonts/TrueTypeFontFile.php +++ b/src/Fonts/TrueTypeFontFile.php @@ -24,9 +24,9 @@ class TrueTypeFontFile extends FontFile private const PLATFORM_ID_MACINTOSH = 1; private const PLATFORM_ID_WINDOWS = 3; - private $family; - private $subfamily; - private $weightClass; + private string $family; + private string $subfamily; + private ?int $weightClass; public function __construct(string $path, string $family, string $subfamily, ?int $weightClass) { diff --git a/src/Nodes/SVGGenericNodeType.php b/src/Nodes/SVGGenericNodeType.php index 26cf3dec..1543c109 100644 --- a/src/Nodes/SVGGenericNodeType.php +++ b/src/Nodes/SVGGenericNodeType.php @@ -10,7 +10,7 @@ */ class SVGGenericNodeType extends SVGNodeContainer { - private $tagName; + private string $tagName; public function __construct(string $tagName) { diff --git a/src/Nodes/SVGNode.php b/src/Nodes/SVGNode.php index 7120d334..f02c400a 100644 --- a/src/Nodes/SVGNode.php +++ b/src/Nodes/SVGNode.php @@ -12,15 +12,15 @@ abstract class SVGNode { /** @var SVGNodeContainer $parent The parent node. */ - protected $parent; + protected ?SVGNodeContainer $parent; /** @var string[] $namespaces A map of custom namespaces to their URIs. */ - private $namespaces; + private array $namespaces; /** @var string[] $attributes This node's set of attributes. */ - protected $attributes; + protected array $attributes; /** @var string[] $styles This node's set of explicit style declarations. */ - protected $styles; + protected array $styles; /** @var string $value This node's value */ - protected $value; + protected string $value; public function __construct() { @@ -28,6 +28,7 @@ public function __construct() $this->attributes = []; $this->styles = []; $this->value = ''; + $this->parent = null; } /** diff --git a/src/Nodes/SVGNodeContainer.php b/src/Nodes/SVGNodeContainer.php index 7ca6ee9a..e4c51a54 100644 --- a/src/Nodes/SVGNodeContainer.php +++ b/src/Nodes/SVGNodeContainer.php @@ -14,12 +14,12 @@ abstract class SVGNodeContainer extends SVGNode { /** @var SVGNode[] $children This node's child nodes. */ - protected $children; + protected array $children; /** * @var string[] $globalStyles A 2D array mapping CSS selectors to values. */ - protected $containerStyles; + protected array $containerStyles; public function __construct() { diff --git a/src/Nodes/Shapes/SVGPath.php b/src/Nodes/Shapes/SVGPath.php index ba1cc955..1f7560b0 100644 --- a/src/Nodes/Shapes/SVGPath.php +++ b/src/Nodes/Shapes/SVGPath.php @@ -14,7 +14,7 @@ class SVGPath extends SVGNodeContainer { public const TAG_NAME = 'path'; - private static $pathParser; + private static PathParser $pathParser; /** * @param string|null $d The path description. @@ -79,9 +79,8 @@ public function rasterize(SVGRasterizer $rasterizer): void private static function getPathParser(): PathParser { - if (!isset(self::$pathParser)) { - self::$pathParser = new PathParser(); - } + self::$pathParser ??= new PathParser(); + return self::$pathParser; } } diff --git a/src/Nodes/Structures/SVGDocumentFragment.php b/src/Nodes/Structures/SVGDocumentFragment.php index b8404e03..ab58231c 100644 --- a/src/Nodes/Structures/SVGDocumentFragment.php +++ b/src/Nodes/Structures/SVGDocumentFragment.php @@ -16,7 +16,7 @@ class SVGDocumentFragment extends SVGNodeContainer public const TAG_NAME = 'svg'; /** @var array $initialStyles A map of style keys to their defaults. */ - private static $initialStyles = [ + private static array $initialStyles = [ 'fill' => '#000000', 'stroke' => 'none', 'stroke-width' => '1', diff --git a/src/Rasterization/Path/PathApproximator.php b/src/Rasterization/Path/PathApproximator.php index 6fa18e1f..2cd93a36 100644 --- a/src/Rasterization/Path/PathApproximator.php +++ b/src/Rasterization/Path/PathApproximator.php @@ -19,7 +19,7 @@ class PathApproximator /** * @var string[] $commands A map of command ids to approximation functions. */ - private static $commands = [ + private static array $commands = [ 'M' => 'moveTo', 'm' => 'moveTo', 'L' => 'lineTo', 'l' => 'lineTo', 'H' => 'lineToHorizontal', 'h' => 'lineToHorizontal', @@ -35,26 +35,26 @@ class PathApproximator /** * @var BezierApproximator $bezier The singleton bezier approximator. */ - private static $bezier; + private static BezierApproximator $bezier; /** * @var ArcApproximator $arc The singleton arc approximator. */ - private static $arc; + private static ArcApproximator $arc; /** * @var Transform $transform The transform to use. */ - private $transform; + private Transform $transform; /** * @var float[][][] $subpaths The approximation result up until now. */ - private $subpaths = []; + private array $subpaths = []; /** * @var PolygonBuilder|null $builder The current subpath builder. */ - private $builder; + private ?PolygonBuilder $builder; // the start of the current subpath, in path coordinates private $firstX; diff --git a/src/Rasterization/Path/PathParser.php b/src/Rasterization/Path/PathParser.php index 7272e030..b62764ed 100644 --- a/src/Rasterization/Path/PathParser.php +++ b/src/Rasterization/Path/PathParser.php @@ -13,7 +13,7 @@ class PathParser /** * @var int[] $commandLengths A map of command ids to their argument counts. */ - private static $commandLengths = [ + private static array $commandLengths = [ 'M' => 2, 'm' => 2, // MoveTo 'L' => 2, 'l' => 2, // LineTo 'H' => 1, 'h' => 1, // LineToHorizontal diff --git a/src/Rasterization/Path/PolygonBuilder.php b/src/Rasterization/Path/PolygonBuilder.php index 93060329..b141e0f2 100644 --- a/src/Rasterization/Path/PolygonBuilder.php +++ b/src/Rasterization/Path/PolygonBuilder.php @@ -13,16 +13,16 @@ class PolygonBuilder /** * @var array[] $points The polygon being built (array of float 2-tuples). */ - private $points = []; + private array $points = []; /** * @var float $posX The current x position. */ - private $posX; + private float $posX; /** * @var float $posY The current y position. */ - private $posY; + private float $posY; /** * @param float $posX The starting x position. diff --git a/src/Rasterization/Renderers/PathRendererEdge.php b/src/Rasterization/Renderers/PathRendererEdge.php index 5656fe7d..f2e6a377 100644 --- a/src/Rasterization/Renderers/PathRendererEdge.php +++ b/src/Rasterization/Renderers/PathRendererEdge.php @@ -10,27 +10,27 @@ class PathRendererEdge /** * @var float The smaller of the two y values. */ - public $minY; + public float $minY; /** * @var float The larger of the two y values. */ - public $maxY; + public float $maxY; /** * @var int The vertical winding direction of this edge, 1 if top to bottom, -1 if bottom to top. */ - public $direction; + public int $direction; /** * @var float Delta x over delta y of this edge, or 0 if the edge is fully horizontal (dy === 0). */ - public $inverseSlope; + public float $inverseSlope; /** * @var float Initially, the x coordinate belonging to the maxY value, but slides upwards during scanning. */ - public $x; + public float $x; /** * Construct a new edge object from the two end points. The order of points is important here, @@ -61,12 +61,7 @@ public function __construct(float $x1, float $y1, float $x2, float $y2) */ public static function compareMaxY(self $a, self $b): int { - if ($a->maxY < $b->maxY) { - return 1; - } elseif ($a->maxY > $b->maxY) { - return -1; - } - return 0; + return $b->maxY <=> $a->maxY; } /** @@ -78,11 +73,6 @@ public static function compareMaxY(self $a, self $b): int */ public static function compareX(self $a, self $b): int { - if ($a->x < $b->x) { - return 1; - } elseif ($a->x > $b->x) { - return -1; - } - return 0; + return $b->x <=> $a->x; } } diff --git a/src/Rasterization/SVGRasterizer.php b/src/Rasterization/SVGRasterizer.php index d64ec1fe..b6d7b74b 100644 --- a/src/Rasterization/SVGRasterizer.php +++ b/src/Rasterization/SVGRasterizer.php @@ -38,11 +38,11 @@ class SVGRasterizer /** * @var int $width The output image width, in pixels. */ - private $width; + private int $width; /** * @var int $height The output image height, in pixels. */ - private $height; + private int $height; /** @var resource $outImage The output image as a GD resource. */ private $outImage; diff --git a/src/Rasterization/Transform/Transform.php b/src/Rasterization/Transform/Transform.php index f402a577..e92af4a5 100644 --- a/src/Rasterization/Transform/Transform.php +++ b/src/Rasterization/Transform/Transform.php @@ -19,7 +19,7 @@ */ class Transform { - private $matrix; + private array $matrix; /** * Create a transform from the given matrix. The entries [a, b, c, d, e, f] represent the following matrix: diff --git a/src/Reading/AttributeRegistry.php b/src/Reading/AttributeRegistry.php index 9dc3c189..0b6675fd 100644 --- a/src/Reading/AttributeRegistry.php +++ b/src/Reading/AttributeRegistry.php @@ -12,7 +12,7 @@ class AttributeRegistry * @var string[] @styleAttributes Attributes to be interpreted as styles. * List comes from https://www.w3.org/TR/SVG/styling.html. */ - private static $styleAttributes = [ + private static array $styleAttributes = [ // DEFINED IN BOTH CSS2 AND SVG // font properties 'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', @@ -49,7 +49,7 @@ class AttributeRegistry * @var string[] $styleConverters Map of style attributes to class names * for SVG attribute to CSS property conversion. */ - private static $styleConverters = [ + private static array $styleConverters = [ 'font-size' => LengthAttributeConverter::class, 'letter-spacing' => LengthAttributeConverter::class, 'word-spacing' => LengthAttributeConverter::class, diff --git a/src/Reading/LengthAttributeConverter.php b/src/Reading/LengthAttributeConverter.php index 26ec7d37..ce5a1dca 100644 --- a/src/Reading/LengthAttributeConverter.php +++ b/src/Reading/LengthAttributeConverter.php @@ -30,9 +30,8 @@ private function __construct() */ public static function getInstance(): self { - if (!isset(self::$instance)) { - self::$instance = new self(); - } + self::$instance ??= new self(); + return self::$instance; } diff --git a/src/Reading/NodeRegistry.php b/src/Reading/NodeRegistry.php index c9f0b2f3..9218c86c 100644 --- a/src/Reading/NodeRegistry.php +++ b/src/Reading/NodeRegistry.php @@ -77,7 +77,7 @@ class NodeRegistry /** * @var string[] $nodeTypes Map of tag names to fully-qualified class names. */ - private static $nodeTypes = [ + private static array $nodeTypes = [ 'foreignObject' => SVGForeignObject::class, 'image' => SVGImage::class, diff --git a/src/SVG.php b/src/SVG.php index 13b1bae9..3eb9d0ca 100644 --- a/src/SVG.php +++ b/src/SVG.php @@ -20,7 +20,7 @@ class SVG private static $fontRegistry; /** @var SVGDocumentFragment $document This image's root `svg` node/tag. */ - private $document; + private SVGDocumentFragment $document; /** * @param mixed $width The image's width (any CSS length). diff --git a/src/Shims/Str.php b/src/Shims/Str.php index 94b7f7cc..4e887fb0 100644 --- a/src/Shims/Str.php +++ b/src/Shims/Str.php @@ -18,7 +18,7 @@ class Str public static function trim(?string $string, string $characters = " \n\r\t\v\x00"): string { if ($string === null) { - $string = (string)$string; + return ''; } return trim($string, $characters); diff --git a/src/Utilities/Colors/ColorLookup.php b/src/Utilities/Colors/ColorLookup.php index 9e81cc92..8a93bc1d 100644 --- a/src/Utilities/Colors/ColorLookup.php +++ b/src/Utilities/Colors/ColorLookup.php @@ -16,18 +16,14 @@ public static function get(string $keyword): ?array { $keywordLower = strtolower($keyword); - if (!isset(self::$values[$keywordLower])) { - return null; - } - - return self::$values[$keywordLower]; + return self::$values[$keywordLower] ?? null; } /** * @var array[] $values A map of color names to their RGBA arrays. * @see https://www.w3.org/TR/SVG11/types.html#ColorKeywords For the source. */ - private static $values = [ + private static array $values = [ 'transparent' => [ 0, 0, 0, 0], 'aliceblue' => [240, 248, 255, 255], 'antiquewhite' => [250, 235, 215, 255], diff --git a/src/Writing/SVGWriter.php b/src/Writing/SVGWriter.php index 87d76a93..d0951de2 100644 --- a/src/Writing/SVGWriter.php +++ b/src/Writing/SVGWriter.php @@ -14,7 +14,7 @@ class SVGWriter { /** @var string $outString The XML output string being written */ - private $outString = ''; + private string $outString = ''; public function __construct(bool $isStandalone = true) {