EnumTrait: use a better method to initialize enums

this method is simpler, and is also safe at the native type level.
Coincidentally, it also eliminates 30 PHPStan false-positives.
This commit is contained in:
Dylan K. Taylor 2020-02-01 20:33:30 +00:00
parent cc33c8155f
commit 9c33ea8dd1
11 changed files with 33 additions and 50 deletions

View File

@ -57,8 +57,8 @@ final class DyeColor{
/** @var DyeColor[] */
private static $numericIdMap = [];
protected static function setup() : iterable{
return [
protected static function setup() : void{
self::registerAll(
new DyeColor("white", "White", 0, new Color(0xf0, 0xf0, 0xf0)),
new DyeColor("orange", "Orange", 1, new Color(0xf9, 0x80, 0x1d)),
new DyeColor("magenta", "Magenta", 2, new Color(0xc7, 0x4e, 0xbd)),
@ -75,7 +75,7 @@ final class DyeColor{
new DyeColor("green", "Green", 13, new Color(0x5e, 0x7c, 0x16)),
new DyeColor("red", "Red", 14, new Color(0xb0, 0x2e, 0x26)),
new DyeColor("black", "Black", 15, new Color(0x1d, 0x1d, 0x21)),
];
);
}
protected static function register(DyeColor $color) : void{

View File

@ -46,15 +46,15 @@ final class SkullType{
/** @var SkullType[] */
private static $numericIdMap = [];
protected static function setup() : iterable{
return [
protected static function setup() : void{
self::registerAll(
new SkullType("skeleton", "Skeleton Skull", 0),
new SkullType("wither_skeleton", "Wither Skeleton Skull", 1),
new SkullType("zombie", "Zombie Head", 2),
new SkullType("player", "Player Head", 3),
new SkullType("creeper", "Creeper Head", 4),
new SkullType("dragon", "Dragon Head", 5)
];
);
}
protected static function register(SkullType $type) : void{

View File

@ -37,11 +37,11 @@ use pocketmine\utils\EnumTrait;
final class SlabType{
use EnumTrait;
protected static function setup() : iterable{
return [
protected static function setup() : void{
self::registerAll(
new self("bottom"),
new self("top"),
new self("double")
];
);
}
}

View File

@ -39,13 +39,13 @@ use pocketmine\utils\EnumTrait;
final class StairShape{
use EnumTrait;
protected static function setup() : iterable{
return [
protected static function setup() : void{
self::registerAll(
new self("straight"),
new self("inner_left"),
new self("inner_right"),
new self("outer_left"),
new self("outer_right")
];
);
}
}

View File

@ -46,15 +46,15 @@ final class TreeType{
/** @var TreeType[] */
private static $numericIdMap = [];
protected static function setup() : iterable{
return [
protected static function setup() : void{
self::registerAll(
new TreeType("oak", "Oak", 0),
new TreeType("spruce", "Spruce", 1),
new TreeType("birch", "Birch", 2),
new TreeType("jungle", "Jungle", 3),
new TreeType("acacia", "Acacia", 4),
new TreeType("dark_oak", "Dark Oak", 5)
];
);
}
protected static function register(TreeType $type) : void{

View File

@ -37,11 +37,11 @@ use pocketmine\utils\EnumTrait;
final class ItemUseResult{
use EnumTrait;
protected static function setup() : iterable{
return [
protected static function setup() : void{
self::registerAll(
new self("none"),
new self("fail"),
new self("success")
];
);
}
}

View File

@ -41,14 +41,14 @@ final class ToolTier{
__construct as Enum___construct;
}
protected static function setup() : iterable{
return [
protected static function setup() : void{
self::registerAll(
new self("wood", 1, 60, 5, 2),
new self("gold", 2, 33, 5, 12),
new self("stone", 3, 132, 6, 4),
new self("iron", 4, 251, 7, 6),
new self("diamond", 5, 1562, 8, 8)
];
);
}
/** @var int */

View File

@ -47,13 +47,13 @@ final class GameMode{
/** @var self[] */
protected static $magicNumberMap = [];
protected static function setup() : iterable{
return [
protected static function setup() : void{
self::registerAll(
new self("survival", 0, "Survival", "gameMode.survival", ["s", "0"]),
new self("creative", 1, "Creative", "gameMode.creative", ["c", "1"]),
new self("adventure", 2, "Adventure", "gameMode.adventure", ["a", "2"]),
new self("spectator", 3, "Spectator", "gameMode.spectator", ["v", "view", "3"])
];
);
}
protected static function register(self $member) : void{

View File

@ -36,10 +36,10 @@ use pocketmine\utils\EnumTrait;
final class PluginLoadOrder{
use EnumTrait;
protected static function setup() : iterable{
return [
protected static function setup() : void{
self::registerAll(
new self("startup"),
new self("postworld")
];
);
}
}

View File

@ -38,26 +38,9 @@ trait EnumTrait{
self::_registryRegister($member->name(), $member);
}
/**
* Returns an array of enum members to be registered.
*
* (This ought to be private, but traits suck too much for that.)
*
* @return self[]|iterable
*/
abstract protected static function setup() : iterable;
/**
* @internal Lazy-inits the enum if necessary.
*
* @throws \InvalidArgumentException
*/
protected static function checkInit() : void{
if(self::$members === null){
self::$members = [];
foreach(self::setup() as $item){
self::register($item);
}
protected static function registerAll(self ...$members) : void{
foreach($members as $member){
self::register($member);
}
}

View File

@ -41,14 +41,14 @@ final class NoteInstrument{
__construct as Enum___construct;
}
protected static function setup() : iterable{
return [
protected static function setup() : void{
self::registerAll(
new self("piano", 0),
new self("bass_drum", 1),
new self("snare", 2),
new self("clicks_and_sticks", 3),
new self("double_bass", 4)
];
);
}
/** @var int */