Skip to content

Commit

Permalink
Modernize code
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Aug 3, 2023
1 parent d2beb9d commit dc132d9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 52 deletions.
11 changes: 4 additions & 7 deletions src/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
};
}
}
6 changes: 3 additions & 3 deletions src/AxisAlignedBB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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})";
}

Expand Down
11 changes: 4 additions & 7 deletions src/Facing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
};
}
}
24 changes: 14 additions & 10 deletions src/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
14 changes: 5 additions & 9 deletions src/RayTraceResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 4 additions & 7 deletions src/Vector2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 5 additions & 9 deletions src/Vector3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dc132d9

Please sign in to comment.