Commit dbfe027d authored by Jan Kuchař's avatar Jan Kuchař

Merge branch 'better-exceptions' into 'master'

\LogicException -> \Grifart\Enum\UsageException

See merge request !9
parents d42321d1 44afd3a5
Pipeline #14370 passed with stages
in 39 seconds
......@@ -2,6 +2,8 @@
namespace Grifart\Enum\Internal;
use Grifart\Enum\UsageException;
final class ConsistencyChecker
{
public static function checkAnnotations(Meta $enumMeta): void
......@@ -30,7 +32,7 @@ final class ConsistencyChecker
if (\count($missingAnnotations) !== 0) {
$properDoc = "/**\n * " . implode("\n * ", $missingAnnotations) . "\n */\n";
throw new \LogicException("You have forgotten to add @method annotations for enum '{$enumReflection->getName()}'. Documentation block should contain: \n$properDoc");
throw new UsageException("You have forgotten to add @method annotations for enum '{$enumReflection->getName()}'. Documentation block should contain: \n$properDoc");
}
// todo: @method annotations without constants
}
......@@ -42,7 +44,7 @@ final class ConsistencyChecker
foreach ($enumMeta->getScalarValues() as $scalarValue) {
if (!$enumMeta->hasValueForScalar($scalarValue)) {
$constantName = $enumMeta->getConstantNameForScalar($scalarValue);
throw new \LogicException("You have forgotten to provide instance for $constantName.");
throw new UsageException("You have forgotten to provide instance for $constantName.");
}
}
}
......
......@@ -51,10 +51,10 @@ final class Meta
foreach($values as $value) {
$scalar = $value->getScalar();
if (isset($scalarToValues[$scalar])) {
throw new \LogicException('You have provided duplicated scalar values.');
throw new UsageException('You have provided duplicated scalar values.');
}
if(!$this->hasConstantForScalar($scalar)) {
throw new \LogicException("Provided instance contains scalar value '$scalar'. But no corresponding constant was found.");
throw new UsageException("Provided instance contains scalar value '$scalar'. But no corresponding constant was found.");
}
$scalarToValues[$scalar] = $value;
......@@ -130,7 +130,7 @@ final class Meta
{
$result = \array_search($scalarValue, $this->constantToScalar, true);
if ($result === false) {
throw new \LogicException("Could not find constant name for $scalarValue.");
throw new UsageException("Could not find constant name for $scalarValue.");
}
return $result;
}
......@@ -142,7 +142,7 @@ final class Meta
{
$result = \array_search($enum, $this->scalarToValue, true);
if ($result === false) {
throw new \LogicException("Could not find scalar value given value.");
throw new UsageException("Could not find scalar for given instance.");
}
return $result;
}
......
......@@ -14,7 +14,7 @@ class MethodAnnotationsMissing extends \Grifart\Enum\Enum
function () {
MethodAnnotationsMissing::fromScalar('a');
},
\LogicException::class,
\Grifart\Enum\UsageException::class,
"You have forgotten to add @method annotations for enum 'MethodAnnotationsMissing'. Documentation block should contain:\n"
. "/**\n"
. " * @method static MethodAnnotationsMissing STATE_A()\n"
......
......@@ -2,6 +2,7 @@
namespace Tests\Grifart\Enum\Consistency;
use Grifart\Enum\UsageException;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
......@@ -28,16 +29,16 @@ class MissingInstanceEnum extends \Grifart\Enum\Enum
$message = 'You have forgotten to provide instance for STATE_B.';
Assert::exception(function () {
MissingInstanceEnum::STATE_A();
}, \LogicException::class, $message);
}, UsageException::class, $message);
Assert::exception(function () {
MissingInstanceEnum::STATE_B();
}, \LogicException::class, $message);
}, UsageException::class, $message);
Assert::exception(function () {
MissingInstanceEnum::fromScalar('a');
}, \LogicException::class, $message);
}, UsageException::class, $message);
Assert::exception(function () {
MissingInstanceEnum::fromScalar('b');
}, \LogicException::class, $message);
\ No newline at end of file
}, UsageException::class, $message);
\ No newline at end of file
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