Skip to content

Commit

Permalink
fix: Leverage PHP 7.4 syntax (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
smnandre authored Mar 17, 2024
1 parent 8d4886e commit 51251be
Show file tree
Hide file tree
Showing 20 changed files with 47 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/Fonts/FontFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
abstract class FontFile
{
private $path;
private string $path;

public function __construct(string $path)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Fonts/TrueTypeFontFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nodes/SVGGenericNodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class SVGGenericNodeType extends SVGNodeContainer
{
private $tagName;
private string $tagName;

public function __construct(string $tagName)
{
Expand Down
11 changes: 6 additions & 5 deletions src/Nodes/SVGNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@
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()
{
$this->namespaces = [];
$this->attributes = [];
$this->styles = [];
$this->value = '';
$this->parent = null;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Nodes/SVGNodeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
7 changes: 3 additions & 4 deletions src/Nodes/Shapes/SVGPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Nodes/Structures/SVGDocumentFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 6 additions & 6 deletions src/Rasterization/Path/PathApproximator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Rasterization/Path/PathParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Rasterization/Path/PolygonBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 7 additions & 17 deletions src/Rasterization/Renderers/PathRendererEdge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/Rasterization/SVGRasterizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Rasterization/Transform/Transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/Reading/AttributeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions src/Reading/LengthAttributeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Reading/NodeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
2 changes: 1 addition & 1 deletion src/SVG.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion src/Shims/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions src/Utilities/Colors/ColorLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion src/Writing/SVGWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 51251be

Please sign in to comment.