From dc132d93595b32e9f210d78b3c8d43c662a5edbf Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 3 Aug 2023 13:56:33 +0100 Subject: [PATCH] Modernize code --- src/Axis.php | 11 ++++------- src/AxisAlignedBB.php | 6 +++--- src/Facing.php | 11 ++++------- src/Matrix.php | 24 ++++++++++++++---------- src/RayTraceResult.php | 14 +++++--------- src/Vector2.php | 11 ++++------- src/Vector3.php | 14 +++++--------- 7 files changed, 39 insertions(+), 52 deletions(-) diff --git a/src/Axis.php b/src/Axis.php index ae6f0a5..68f3198 100644 --- a/src/Axis.php +++ b/src/Axis.php @@ -36,14 +36,11 @@ private function __construct(){ * Returns a human-readable string representation of the given axis. */ public static function toString(int $axis) : string{ - $result = [ + return match($axis){ Axis::Y => "y", Axis::Z => "z", - Axis::X => "x" - ][$axis] ?? null; - if($result === null){ - throw new \InvalidArgumentException("Invalid axis $axis"); - } - return $result; + Axis::X => "x", + default => throw new \InvalidArgumentException("Invalid axis $axis") + }; } } diff --git a/src/AxisAlignedBB.php b/src/AxisAlignedBB.php index 8daed3d..cf4a3e2 100644 --- a/src/AxisAlignedBB.php +++ b/src/AxisAlignedBB.php @@ -115,7 +115,7 @@ public function expandedCopy(float $x, float $y, float $z) : AxisAlignedBB{ * * @return $this */ - public function offset(float $x, float $y, float $z){ + public function offset(float $x, float $y, float $z) : AxisAlignedBB{ $this->minX += $x; $this->minY += $y; $this->minZ += $z; @@ -158,7 +158,7 @@ public function offsetTowardsCopy(int $face, float $distance) : AxisAlignedBB{ * * @return $this */ - public function contract(float $x, float $y, float $z){ + public function contract(float $x, float $y, float $z) : AxisAlignedBB{ $this->minX += $x; $this->minY += $y; $this->minZ += $z; @@ -489,7 +489,7 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu return new RayTraceResult($this, $face, $vector); } - public function __toString(){ + public function __toString() : string{ return "AxisAlignedBB({$this->minX}, {$this->minY}, {$this->minZ}, {$this->maxX}, {$this->maxY}, {$this->maxZ})"; } diff --git a/src/Facing.php b/src/Facing.php index 3a42e49..134a29b 100644 --- a/src/Facing.php +++ b/src/Facing.php @@ -162,17 +162,14 @@ public static function validate(int $facing) : void{ * Returns a human-readable string representation of the given Facing direction. */ public static function toString(int $facing) : string{ - $result = [ + return match($facing){ self::DOWN => "down", self::UP => "up", self::NORTH => "north", self::SOUTH => "south", self::WEST => "west", - self::EAST => "east" - ][$facing] ?? null; - if($result === null){ - throw new \InvalidArgumentException("Invalid facing $facing"); - } - return $result; + self::EAST => "east", + default => throw new \InvalidArgumentException("Invalid facing $facing") + }; } } diff --git a/src/Matrix.php b/src/Matrix.php index 062bd10..dd6850d 100644 --- a/src/Matrix.php +++ b/src/Matrix.php @@ -193,16 +193,20 @@ public function determinant() : float{ if($this->isSquare() !== true){ throw new \LogicException("Cannot calculate determinant of a non-square matrix"); } - switch($this->rows){ - case 1: - return $this->matrix[0][0]; - case 2: - return $this->matrix[0][0] * $this->matrix[1][1] - $this->matrix[0][1] * $this->matrix[1][0]; - case 3: - return $this->matrix[0][0] * $this->matrix[1][1] * $this->matrix[2][2] + $this->matrix[0][1] * $this->matrix[1][2] * $this->matrix[2][0] + $this->matrix[0][2] * $this->matrix[1][0] * $this->matrix[2][1] - $this->matrix[2][0] * $this->matrix[1][1] * $this->matrix[0][2] - $this->matrix[2][1] * $this->matrix[1][2] * $this->matrix[0][0] - $this->matrix[2][2] * $this->matrix[1][0] * $this->matrix[0][1]; - } - - throw new \LogicException("Not implemented"); + return match($this->rows){ + 1 => $this->matrix[0][0], + 2 => + $this->matrix[0][0] * $this->matrix[1][1] - + $this->matrix[0][1] * $this->matrix[1][0], + 3 => + $this->matrix[0][0] * $this->matrix[1][1] * $this->matrix[2][2] + + $this->matrix[0][1] * $this->matrix[1][2] * $this->matrix[2][0] + + $this->matrix[0][2] * $this->matrix[1][0] * $this->matrix[2][1] - + $this->matrix[2][0] * $this->matrix[1][1] * $this->matrix[0][2] - + $this->matrix[2][1] * $this->matrix[1][2] * $this->matrix[0][0] - + $this->matrix[2][2] * $this->matrix[1][0] * $this->matrix[0][1], + default => throw new \LogicException("Not implemented") + }; } public function __toString() : string{ diff --git a/src/RayTraceResult.php b/src/RayTraceResult.php index 0bab6f5..4d0928a 100644 --- a/src/RayTraceResult.php +++ b/src/RayTraceResult.php @@ -28,18 +28,14 @@ */ class RayTraceResult{ - public AxisAlignedBB $bb; - public int $hitFace; - public Vector3 $hitVector; - /** * @param int $hitFace one of the Facing::* constants */ - public function __construct(AxisAlignedBB $bb, int $hitFace, Vector3 $hitVector){ - $this->bb = $bb; - $this->hitFace = $hitFace; - $this->hitVector = $hitVector; - } + public function __construct( + public AxisAlignedBB $bb, + public int $hitFace, + public Vector3 $hitVector + ){} public function getBoundingBox() : AxisAlignedBB{ return $this->bb; diff --git a/src/Vector2.php b/src/Vector2.php index 2b61a2f..e6a8077 100644 --- a/src/Vector2.php +++ b/src/Vector2.php @@ -30,13 +30,10 @@ use function sqrt; class Vector2{ - public float $x; - public float $y; - - public function __construct(float $x, float $y){ - $this->x = $x; - $this->y = $y; - } + public function __construct( + public float $x, + public float $y + ){} public function getX() : float{ return $this->x; diff --git a/src/Vector3.php b/src/Vector3.php index 18f4024..ca8701d 100644 --- a/src/Vector3.php +++ b/src/Vector3.php @@ -34,15 +34,11 @@ use const PHP_ROUND_HALF_UP; class Vector3{ - public float|int $x; - public float|int $y; - public float|int $z; - - public function __construct(float|int $x, float|int $y, float|int $z){ - $this->x = $x; - $this->y = $y; - $this->z = $z; - } + public function __construct( + public float|int $x, + public float|int $y, + public float|int $z + ){} public static function zero() : Vector3{ //TODO: make this reuse a single object, once Vector3 becomes immutable