Skip to content

Commit e212ce7

Browse files
committed
Don't repeat error or condition
1 parent f76eca3 commit e212ce7

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/Facing.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ public function offset() : array{
6767
* Returns whether the direction is facing the positive of its axis.
6868
*/
6969
public function isPositive() : bool{
70-
return $this === self::UP || $this === self::SOUTH || $this === self::EAST;
70+
return match($this){
71+
self::UP, self::SOUTH, self::EAST => true,
72+
self::DOWN, self::NORTH, self::WEST => false,
73+
};
7174
}
7275

7376
/**
@@ -96,24 +99,28 @@ public function rotate(Axis $axis, bool $clockwise) : Facing{
9699
self::EAST => self::SOUTH,
97100
self::SOUTH => self::WEST,
98101
self::WEST => self::NORTH,
99-
default => throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\"")
102+
default => null
100103
},
101104
Axis::Z => match($this){
102105
self::UP => self::EAST,
103106
self::EAST => self::DOWN,
104107
self::DOWN => self::WEST,
105108
self::WEST => self::UP,
106-
default => throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\"")
109+
default => null
107110
},
108111
Axis::X => match($this){
109112
self::UP => self::NORTH,
110113
self::NORTH => self::DOWN,
111114
self::DOWN => self::SOUTH,
112115
self::SOUTH => self::UP,
113-
default => throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\"")
116+
default => null
114117
}
115118
};
116119

120+
if($rotated === null) {
121+
throw new \InvalidArgumentException("Cannot rotate facing \"" . strtolower($this->name) . "\" around axis \"" . strtolower($axis->name) . "\"");
122+
}
123+
117124
return $clockwise ? $rotated : $rotated->opposite();
118125
}
119126

0 commit comments

Comments
 (0)