From a7d51a273d165d97cb0e7aa28e3b939704a8338b Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 17 Aug 2019 16:36:05 +0100 Subject: [PATCH] EnumTrait: add a numeric runtimeID field for arrays and switches --- src/utils/EnumTrait.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/utils/EnumTrait.php b/src/utils/EnumTrait.php index 351e8d5b1..c3c23bd82 100644 --- a/src/utils/EnumTrait.php +++ b/src/utils/EnumTrait.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\utils; +use function getmypid; use function preg_match; trait EnumTrait{ @@ -85,8 +86,13 @@ trait EnumTrait{ return self::_registryFromString($name); } + /** @var int|null */ + private static $nextId = null; + /** @var string */ private $enumName; + /** @var int */ + private $runtimeId; /** * @param string $enumName @@ -98,6 +104,10 @@ trait EnumTrait{ 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; + if(self::$nextId === null){ + self::$nextId = getmypid(); //this provides enough base entropy to prevent hardcoding + } + $this->runtimeId = self::$nextId++; } /** @@ -107,6 +117,17 @@ trait EnumTrait{ return $this->enumName; } + /** + * Returns a runtime-only identifier for this enum member. This will be different with each run, so don't try to + * hardcode it. + * This can be useful for switches or array indexing. + * + * @return int + */ + public function id() : int{ + return $this->runtimeId; + } + /** * Returns whether the two objects are equivalent. *