Skip to content

Commit

Permalink
[Bugfix] Remove direct usage of self::$properties as it can be not in…
Browse files Browse the repository at this point in the history
…itialized
  • Loading branch information
grachevko committed Nov 15, 2019
1 parent bdf3bf2 commit 90be1fb
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ final public static function create(int $id): self
*/
final public static function from(string $property, $value): self
{
return static::create(array_flip(self::$properties[static::class][$property]->getValue())[$value]);
return static::create(array_flip(self::getReflectionProperty($property)->getValue())[$value]);
}

final public function getId(): int
Expand All @@ -145,6 +145,7 @@ final public function getName(): string
}

/**
* @throws ReflectionException
* @throws InvalidArgumentException
*
* @return mixed
Expand All @@ -155,11 +156,11 @@ final public function get(string $property)
return $this->id;
}

if (!array_key_exists($property, self::$properties[static::class])) {
if (!self::getReflection()->hasProperty($property)) {
throw new InvalidArgumentException(sprintf('Property "%s" not exist at class "%s"', $property, static::class));
}

return self::$properties[static::class][$property]->getValue()[$this->getId()];
return self::getReflectionProperty($property)->getValue()[$this->getId()];
}

/**
Expand All @@ -174,7 +175,7 @@ final public static function all(array $values = [], bool $reverse = false, stri
if ('id' === $property) {
$all = array_values(self::getReflection()->getConstants());
} else {
$all = array_values(self::$properties[static::class][$property]->getValue());
$all = array_values(self::getReflectionProperty($property)->getValue());
}

if ([] === $values) {
Expand Down Expand Up @@ -231,6 +232,17 @@ private static function getReflection(): ReflectionClass
return self::$reflections[$class] ?? self::$reflections[$class] = self::validate(new ReflectionClass($class));
}

private static function getReflectionProperty(string $property): ReflectionProperty
{
$class = static::class;

if ((self::$properties[$class][$property] ?? null) instanceof ReflectionProperty) {
self::getReflection();
}

return self::$properties[$class][$property];
}

/**
* @throws LogicException
*/
Expand Down

0 comments on commit 90be1fb

Please sign in to comment.