Skip to content

Commit dc132d9

Browse files
committed
Modernize code
1 parent d2beb9d commit dc132d9

File tree

7 files changed

+39
-52
lines changed

7 files changed

+39
-52
lines changed

src/Axis.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ private function __construct(){
3636
* Returns a human-readable string representation of the given axis.
3737
*/
3838
public static function toString(int $axis) : string{
39-
$result = [
39+
return match($axis){
4040
Axis::Y => "y",
4141
Axis::Z => "z",
42-
Axis::X => "x"
43-
][$axis] ?? null;
44-
if($result === null){
45-
throw new \InvalidArgumentException("Invalid axis $axis");
46-
}
47-
return $result;
42+
Axis::X => "x",
43+
default => throw new \InvalidArgumentException("Invalid axis $axis")
44+
};
4845
}
4946
}

src/AxisAlignedBB.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function expandedCopy(float $x, float $y, float $z) : AxisAlignedBB{
115115
*
116116
* @return $this
117117
*/
118-
public function offset(float $x, float $y, float $z){
118+
public function offset(float $x, float $y, float $z) : AxisAlignedBB{
119119
$this->minX += $x;
120120
$this->minY += $y;
121121
$this->minZ += $z;
@@ -158,7 +158,7 @@ public function offsetTowardsCopy(int $face, float $distance) : AxisAlignedBB{
158158
*
159159
* @return $this
160160
*/
161-
public function contract(float $x, float $y, float $z){
161+
public function contract(float $x, float $y, float $z) : AxisAlignedBB{
162162
$this->minX += $x;
163163
$this->minY += $y;
164164
$this->minZ += $z;
@@ -489,7 +489,7 @@ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2) : ?RayTraceResu
489489
return new RayTraceResult($this, $face, $vector);
490490
}
491491

492-
public function __toString(){
492+
public function __toString() : string{
493493
return "AxisAlignedBB({$this->minX}, {$this->minY}, {$this->minZ}, {$this->maxX}, {$this->maxY}, {$this->maxZ})";
494494
}
495495

src/Facing.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,14 @@ public static function validate(int $facing) : void{
162162
* Returns a human-readable string representation of the given Facing direction.
163163
*/
164164
public static function toString(int $facing) : string{
165-
$result = [
165+
return match($facing){
166166
self::DOWN => "down",
167167
self::UP => "up",
168168
self::NORTH => "north",
169169
self::SOUTH => "south",
170170
self::WEST => "west",
171-
self::EAST => "east"
172-
][$facing] ?? null;
173-
if($result === null){
174-
throw new \InvalidArgumentException("Invalid facing $facing");
175-
}
176-
return $result;
171+
self::EAST => "east",
172+
default => throw new \InvalidArgumentException("Invalid facing $facing")
173+
};
177174
}
178175
}

src/Matrix.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,20 @@ public function determinant() : float{
193193
if($this->isSquare() !== true){
194194
throw new \LogicException("Cannot calculate determinant of a non-square matrix");
195195
}
196-
switch($this->rows){
197-
case 1:
198-
return $this->matrix[0][0];
199-
case 2:
200-
return $this->matrix[0][0] * $this->matrix[1][1] - $this->matrix[0][1] * $this->matrix[1][0];
201-
case 3:
202-
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];
203-
}
204-
205-
throw new \LogicException("Not implemented");
196+
return match($this->rows){
197+
1 => $this->matrix[0][0],
198+
2 =>
199+
$this->matrix[0][0] * $this->matrix[1][1] -
200+
$this->matrix[0][1] * $this->matrix[1][0],
201+
3 =>
202+
$this->matrix[0][0] * $this->matrix[1][1] * $this->matrix[2][2] +
203+
$this->matrix[0][1] * $this->matrix[1][2] * $this->matrix[2][0] +
204+
$this->matrix[0][2] * $this->matrix[1][0] * $this->matrix[2][1] -
205+
$this->matrix[2][0] * $this->matrix[1][1] * $this->matrix[0][2] -
206+
$this->matrix[2][1] * $this->matrix[1][2] * $this->matrix[0][0] -
207+
$this->matrix[2][2] * $this->matrix[1][0] * $this->matrix[0][1],
208+
default => throw new \LogicException("Not implemented")
209+
};
206210
}
207211

208212
public function __toString() : string{

src/RayTraceResult.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,14 @@
2828
*/
2929
class RayTraceResult{
3030

31-
public AxisAlignedBB $bb;
32-
public int $hitFace;
33-
public Vector3 $hitVector;
34-
3531
/**
3632
* @param int $hitFace one of the Facing::* constants
3733
*/
38-
public function __construct(AxisAlignedBB $bb, int $hitFace, Vector3 $hitVector){
39-
$this->bb = $bb;
40-
$this->hitFace = $hitFace;
41-
$this->hitVector = $hitVector;
42-
}
34+
public function __construct(
35+
public AxisAlignedBB $bb,
36+
public int $hitFace,
37+
public Vector3 $hitVector
38+
){}
4339

4440
public function getBoundingBox() : AxisAlignedBB{
4541
return $this->bb;

src/Vector2.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@
3030
use function sqrt;
3131

3232
class Vector2{
33-
public float $x;
34-
public float $y;
35-
36-
public function __construct(float $x, float $y){
37-
$this->x = $x;
38-
$this->y = $y;
39-
}
33+
public function __construct(
34+
public float $x,
35+
public float $y
36+
){}
4037

4138
public function getX() : float{
4239
return $this->x;

src/Vector3.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@
3434
use const PHP_ROUND_HALF_UP;
3535

3636
class Vector3{
37-
public float|int $x;
38-
public float|int $y;
39-
public float|int $z;
40-
41-
public function __construct(float|int $x, float|int $y, float|int $z){
42-
$this->x = $x;
43-
$this->y = $y;
44-
$this->z = $z;
45-
}
37+
public function __construct(
38+
public float|int $x,
39+
public float|int $y,
40+
public float|int $z
41+
){}
4642

4743
public static function zero() : Vector3{
4844
//TODO: make this reuse a single object, once Vector3 becomes immutable

0 commit comments

Comments
 (0)