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

@ -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);
}
}