EventPriority: hardcode name -> value mapping instead of using constant()

this would have caught fire if additional non-priority constants were added, or if the constants were renamed.
This commit is contained in:
Dylan K. Taylor 2020-10-23 20:14:37 +01:00
parent f5e033ad5d
commit e9038336e3

View File

@ -23,8 +23,6 @@ declare(strict_types=1);
namespace pocketmine\event; namespace pocketmine\event;
use function constant;
use function defined;
use function mb_strtoupper; use function mb_strtoupper;
/** /**
@ -84,10 +82,16 @@ final class EventPriority{
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public static function fromString(string $name) : int{ public static function fromString(string $name) : int{
$name = mb_strtoupper($name); $value = [
$const = self::class . "::" . $name; "LOWEST" => self::LOWEST,
if($name !== "ALL" and defined($const)){ "LOW" => self::LOW,
return constant($const); "NORMAL" => self::NORMAL,
"HIGH" => self::HIGH,
"HIGHEST" => self::HIGHEST,
"MONITOR" => self::MONITOR
][mb_strtoupper($name)] ?? null;
if($value !== null){
return $value;
} }
throw new \InvalidArgumentException("Unable to resolve priority \"$name\""); throw new \InvalidArgumentException("Unable to resolve priority \"$name\"");