Commit 13d5924c authored by Jan Kuchař's avatar Jan Kuchař

added explicit error for case when Enum::NON_EXISTING_VALUE() called

parent 1bebee23
Pipeline #14336 passed with stages
in 43 seconds
......@@ -48,12 +48,17 @@ abstract class Enum
/**
* Provides access to values using ::CONSTANT_NAME() interface.
* @return static
* @throws MissingValueDeclarationException
*/
public static function __callStatic(string $constantName, array $arguments): Enum
{
\assert(empty($arguments));
return self::getMeta()->getValueForConstantName($constantName);
$value = self::getMeta()->getValueForConstantName($constantName);
if($value === NULL) {
throw new \Error('Call to undefined method ' . static::class . '::' . $constantName . '(). Please check that you have provided constant, annotation and value.');
}
return $value;
}
private static function getMeta(): Meta
......
......@@ -48,10 +48,16 @@ final class Meta
return \array_values($this->scalarToValue);
}
public function getValueForConstantName($constantName): Enum
/**
* @throws MissingValueDeclarationException
*/
public function getValueForConstantName($constantName): ?Enum
{
if(!isset($this->constantToScalar[$constantName])) {
return NULL;
}
$scalar = $this->constantToScalar[$constantName];
return $this->scalarToValue[$scalar];
return $this->getValueForScalar($scalar);
}
public function hasValueForScalar($scalarValue): bool
......
<?php declare(strict_types=1);
use Grifart\Enum\MissingValueDeclarationException;
require __DIR__ . '/../bootstrap.php';
/**
* @method static Enum1 VALUE()
*/
class Enum1 extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
protected const VALUE = 'value';
}
\Tester\Assert::exception(
function () {
/** Intentionally calling non-existing method. @noinspection PhpUndefinedMethodInspection */
Enum1::NON_EXISTING();
},
Error::class,
'Call to undefined method Enum1::NON_EXISTING(). Please check that you have provided constant, annotation and value.'
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment