Get rid of EnumTrait::fromString()

it's better to just implement this for stuff where there's explicitly designated aliases, otherwise we could end up with unexpected BC breaks (e.g. hardcoding POSTWORLD in plugin.yml would suddenly break if the core enum was changed, even though it remained valid).
This commit is contained in:
Dylan K. Taylor 2021-06-27 20:52:56 +01:00
parent 902ea515f7
commit 3dd33cd35e
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 61 additions and 23 deletions

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\player;
use pocketmine\utils\EnumTrait;
use function mb_strtolower;
/**
* This doc-block is generated automatically, do not modify it manually.
@ -39,7 +40,6 @@ final class GameMode{
use EnumTrait {
__construct as Enum___construct;
register as Enum_register;
fromString as Enum_fromString;
}
/** @var self[] */
@ -47,23 +47,27 @@ final class GameMode{
protected static function setup() : void{
self::registerAll(
new self("survival", "Survival", "gameMode.survival", ["s", "0"]),
new self("creative", "Creative", "gameMode.creative", ["c", "1"]),
new self("adventure", "Adventure", "gameMode.adventure", ["a", "2"]),
new self("spectator", "Spectator", "gameMode.spectator", ["v", "view", "3"])
new self("survival", "Survival", "gameMode.survival", ["survival", "s", "0"]),
new self("creative", "Creative", "gameMode.creative", ["creative", "c", "1"]),
new self("adventure", "Adventure", "gameMode.adventure", ["adventure", "a", "2"]),
new self("spectator", "Spectator", "gameMode.spectator", ["spectator", "v", "view", "3"])
);
}
protected static function register(self $member) : void{
self::Enum_register($member);
foreach($member->getAliases() as $alias){
self::$aliasMap[$alias] = $member;
self::$aliasMap[mb_strtolower($alias)] = $member;
}
}
public static function fromString(string $str) : self{
self::checkInit();
return self::$aliasMap[$str] ?? self::Enum_fromString($str);
$result = self::$aliasMap[mb_strtolower($str)] ?? null;
if($result === null){
throw new \InvalidArgumentException("Invalid gamemode alias $str");
}
return $result;
}
/** @var int */

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\plugin;
use pocketmine\utils\EnumTrait;
use function mb_strtolower;
/**
* This doc-block is generated automatically, do not modify it manually.
@ -34,12 +35,58 @@ use pocketmine\utils\EnumTrait;
* @method static PluginEnableOrder STARTUP()
*/
final class PluginEnableOrder{
use EnumTrait;
use EnumTrait {
__construct as Enum___construct;
register as Enum_register;
}
protected static function setup() : void{
self::registerAll(
new self("startup"),
new self("postworld")
new self("startup", ["startup"]),
new self("postworld", ["postworld"])
);
}
/**
* @var self[]
* @phpstan-var array<string, self>
*/
private static array $aliasMap = [];
protected static function register(self $member) : void{
self::Enum_register($member);
foreach($member->getAliases() as $alias){
self::$aliasMap[mb_strtolower($alias)] = $member;
}
}
public static function fromString(string $name) : self{
self::checkInit();
$result = self::$aliasMap[mb_strtolower($name)] ?? null;
if($result === null){
throw new \InvalidArgumentException("No such alias $name");
}
return $result;
}
/**
* @var string[]
* @phpstan-var list<string>
*/
private array $aliases;
/**
* @param string[] $aliases
* @phpstan-param list<string> $aliases
*/
private function __construct(string $enumName, array $aliases){
$this->Enum___construct($enumName);
$this->aliases = $aliases;
}
/**
* @return string[]
* @phpstan-return list<string>
*/
public function getAliases() : array{ return $this->aliases; }
}

View File

@ -56,19 +56,6 @@ trait EnumTrait{
return $result;
}
/**
* Returns the enum member matching the given name.
* This is overridden to change the return typehint.
*
* @throws \InvalidArgumentException if no member matches.
*/
public static function fromString(string $name) : self{
//phpstan doesn't support generic traits yet :(
/** @var self $result */
$result = self::_registryFromString($name);
return $result;
}
/** @var int|null */
private static $nextId = null;