diff --git a/src/utils/EnumTrait.php b/src/utils/EnumTrait.php index fd927a203..cf92a9433 100644 --- a/src/utils/EnumTrait.php +++ b/src/utils/EnumTrait.php @@ -70,9 +70,7 @@ trait EnumTrait{ * @throws \InvalidArgumentException */ private function __construct(string $enumName){ - if(preg_match('/^(?!\d)[A-Za-z\d_]+$/u', $enumName, $matches) === 0){ - throw new \InvalidArgumentException("Invalid enum member name \"$enumName\", should only contain letters, numbers and underscores, and must not start with a number"); - } + self::verifyName($enumName); $this->enumName = $enumName; if(self::$nextId === null){ self::$nextId = Process::pid(); //this provides enough base entropy to prevent hardcoding diff --git a/src/utils/RegistryTrait.php b/src/utils/RegistryTrait.php index 185dfefb3..61427b333 100644 --- a/src/utils/RegistryTrait.php +++ b/src/utils/RegistryTrait.php @@ -31,12 +31,19 @@ trait RegistryTrait{ /** @var object[] */ private static $members = null; + private static function verifyName(string $name) : void{ + if(preg_match('/^(?!\d)[A-Za-z\d_]+$/u', $name) === 0){ + throw new \InvalidArgumentException("Invalid member name \"$name\", should only contain letters, numbers and underscores, and must not start with a number"); + } + } + /** * Adds the given object to the registry. * * @throws \InvalidArgumentException */ private static function _registryRegister(string $name, object $member) : void{ + self::verifyName($name); $upperName = mb_strtoupper($name); if(isset(self::$members[$upperName])){ throw new \InvalidArgumentException("\"$upperName\" is already reserved");