mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-20 07:39:42 +00:00
EnumTrait: throw InvalidArgumentException from fromString()
this is more in line with expected behaviour, since this might be used to process arbitrary user input. Only calling an undefined magic static method should throw an Error.
This commit is contained in:
@@ -76,12 +76,13 @@ trait EnumTrait{
|
|||||||
* @param string $name
|
* @param string $name
|
||||||
*
|
*
|
||||||
* @return self
|
* @return self
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public static function fromString(string $name) : self{
|
public static function fromString(string $name) : self{
|
||||||
self::checkInit();
|
self::checkInit();
|
||||||
$name = strtoupper($name);
|
$name = strtoupper($name);
|
||||||
if(!isset(self::$members[$name])){
|
if(!isset(self::$members[$name])){
|
||||||
throw new \Error("Undefined enum member: " . self::class . "::" . $name);
|
throw new \InvalidArgumentException("Undefined enum member: " . self::class . "::" . $name);
|
||||||
}
|
}
|
||||||
return self::$members[$name];
|
return self::$members[$name];
|
||||||
}
|
}
|
||||||
@@ -96,7 +97,11 @@ trait EnumTrait{
|
|||||||
if(!empty($arguments)){
|
if(!empty($arguments)){
|
||||||
throw new \ArgumentCountError("Expected exactly 0 arguments, " . count($arguments) . " passed");
|
throw new \ArgumentCountError("Expected exactly 0 arguments, " . count($arguments) . " passed");
|
||||||
}
|
}
|
||||||
return self::fromString($name);
|
try{
|
||||||
|
return self::fromString($name);
|
||||||
|
}catch(\InvalidArgumentException $e){
|
||||||
|
throw new \Error($e->getMessage(), 0, $e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user