RegistryTrait: fixed mishandling of self::$members

Since PHPStan doesn't warn about potential nulls on untyped properties, this flew under the radar.
This commit is contained in:
Dylan K. Taylor 2023-09-08 12:16:16 +01:00
parent fe94379a93
commit a5aeabd836
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -37,8 +37,8 @@ use function preg_match;
*/ */
trait RegistryTrait{ trait RegistryTrait{
/** /**
* @var object[] * @var object[]|null
* @phpstan-var array<string, object> * @phpstan-var array<string, object>|null
*/ */
private static $members = null; private static $members = null;
@ -54,6 +54,9 @@ trait RegistryTrait{
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
private static function _registryRegister(string $name, object $member) : void{ private static function _registryRegister(string $name, object $member) : void{
if(self::$members === null){
throw new AssumptionFailedError("Cannot register members outside of " . self::class . "::setup()");
}
self::verifyName($name); self::verifyName($name);
$upperName = mb_strtoupper($name); $upperName = mb_strtoupper($name);
if(isset(self::$members[$upperName])){ if(isset(self::$members[$upperName])){
@ -86,6 +89,9 @@ trait RegistryTrait{
*/ */
private static function _registryFromString(string $name) : object{ private static function _registryFromString(string $name) : object{
self::checkInit(); self::checkInit();
if(self::$members === null){
throw new AssumptionFailedError(self::class . "::checkInit() did not initialize self::\$members correctly");
}
$upperName = mb_strtoupper($name); $upperName = mb_strtoupper($name);
if(!isset(self::$members[$upperName])){ if(!isset(self::$members[$upperName])){
throw new \InvalidArgumentException("No such registry member: " . self::class . "::" . $upperName); throw new \InvalidArgumentException("No such registry member: " . self::class . "::" . $upperName);
@ -121,6 +127,6 @@ trait RegistryTrait{
*/ */
private static function _registryGetAll() : array{ private static function _registryGetAll() : array{
self::checkInit(); self::checkInit();
return array_map(self::preprocessMember(...), self::$members); return array_map(self::preprocessMember(...), self::$members ?? throw new AssumptionFailedError(self::class . "::checkInit() did not initialize self::\$members correctly"));
} }
} }