mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-04 09:10:00 +00:00
RegistryTrait: enforce name validation rules on everything, not just enums
fixes #4916
This commit is contained in:
parent
2b8a54f8ff
commit
b7e6854189
@ -70,9 +70,7 @@ trait EnumTrait{
|
|||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
private function __construct(string $enumName){
|
private function __construct(string $enumName){
|
||||||
if(preg_match('/^(?!\d)[A-Za-z\d_]+$/u', $enumName, $matches) === 0){
|
self::verifyName($enumName);
|
||||||
throw new \InvalidArgumentException("Invalid enum member name \"$enumName\", should only contain letters, numbers and underscores, and must not start with a number");
|
|
||||||
}
|
|
||||||
$this->enumName = $enumName;
|
$this->enumName = $enumName;
|
||||||
if(self::$nextId === null){
|
if(self::$nextId === null){
|
||||||
self::$nextId = Process::pid(); //this provides enough base entropy to prevent hardcoding
|
self::$nextId = Process::pid(); //this provides enough base entropy to prevent hardcoding
|
||||||
|
@ -31,12 +31,19 @@ trait RegistryTrait{
|
|||||||
/** @var object[] */
|
/** @var object[] */
|
||||||
private static $members = null;
|
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.
|
* Adds the given object to the registry.
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
private static function _registryRegister(string $name, object $member) : void{
|
private static function _registryRegister(string $name, object $member) : void{
|
||||||
|
self::verifyName($name);
|
||||||
$upperName = mb_strtoupper($name);
|
$upperName = mb_strtoupper($name);
|
||||||
if(isset(self::$members[$upperName])){
|
if(isset(self::$members[$upperName])){
|
||||||
throw new \InvalidArgumentException("\"$upperName\" is already reserved");
|
throw new \InvalidArgumentException("\"$upperName\" is already reserved");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user