Skip to content

Commit

Permalink
PHP8.0: Use throw as expression where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
Muqsit committed Feb 5, 2024
1 parent 7fd5d34 commit e658592
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ public function registerSubCommand(TebexSubCommand $sub_command) : void{
}

public function unregisterSubCommand(string $name) : void{
if(!isset($this->sub_commands[$name])){
throw new InvalidArgumentException("Tried unregistering an unregistered sub-command: {$name}");
}
isset($this->sub_commands[$name]) || throw new InvalidArgumentException("Tried unregistering an unregistered sub-command: {$name}");
foreach($this->sub_commands[$name]->aliases as $alias){
unset($this->aliases[$alias]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public function __construct(){

public function fromPlayer(Player $player) : string{
$xuid = $player->getXuid();
if($xuid === ""){
throw new InvalidArgumentException("Cannot retrieve XUID of player: {$player->getName()}");
}
$xuid !== "" || throw new InvalidArgumentException("Cannot retrieve XUID of player: {$player->getName()}");
return $xuid;
}

Expand Down
6 changes: 2 additions & 4 deletions src/muqsit/tebex/thread/TebexThreadPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use pocketmine\Server;
use pocketmine\snooze\SleeperHandlerEntry;
use UnderflowException;
use function count;

final class TebexThreadPool{

Expand Down Expand Up @@ -36,10 +37,7 @@ public function addWorker(TebexThread $thread) : void{
}

public function start() : void{
if(count($this->workers) === 0){
throw new UnderflowException("Cannot start an empty pool of workers");
}

count($this->workers) > 0 || throw new UnderflowException("Cannot start an empty pool of workers");
foreach($this->workers as $thread){
$thread->start();
}
Expand Down
15 changes: 6 additions & 9 deletions src/muqsit/tebex/utils/TypeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace muqsit\tebex\utils;

use InvalidArgumentException;
use function is_array;
use function is_int;
use function is_string;

final class TypeValidator{

Expand All @@ -13,19 +16,15 @@ private static function printValue(mixed $value) : string{
}

public static function validateInt(string $key, mixed $value, int $min = PHP_INT_MIN, int $max = PHP_INT_MAX) : int{
if(!is_int($value)){
throw new InvalidArgumentException("Invalid value for {$key}: " . self::printValue($value));
}
is_int($value) || throw new InvalidArgumentException("Invalid value for {$key}: " . self::printValue($value));
if($value < $min || $value > $max){
throw new InvalidArgumentException("{$key}'s value is out of range [{$min}, {$max}]");
}
return $value;
}

public static function validateString(string $key, mixed $value) : string{
if(!is_string($value)){
throw new InvalidArgumentException("Invalid value for {$key}: " . self::printValue($value));
}
is_string($value) || throw new InvalidArgumentException("Invalid value for {$key}: " . self::printValue($value));
return $value;
}

Expand All @@ -35,9 +34,7 @@ public static function validateString(string $key, mixed $value) : string{
* @return string[]
*/
public static function validateStringList(string $key, mixed $value) : array{
if(!is_array($value)){
throw new InvalidArgumentException("Invalid value for {$key}: " . self::printValue($value));
}
is_array($value) || throw new InvalidArgumentException("Invalid value for {$key}: " . self::printValue($value));
foreach($value as $index => $item){
self::validateString("{$key}.{$index}", $item);
}
Expand Down

0 comments on commit e658592

Please sign in to comment.