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

added final xor abstract check for enum root class

parent 1cc25ed5
......@@ -13,6 +13,7 @@ final class ConsistencyChecker
{
self::checkCallStaticAnnotations($enumMeta);
self::checkAllInstancesProvided($enumMeta);
self::checkAbstractAndFinal($enumMeta);
}
private static function checkCallStaticAnnotations(Meta $enumMeta): void
......@@ -51,4 +52,17 @@ final class ConsistencyChecker
}
}
}
private static function checkAbstractAndFinal(Meta $enumMeta): void
{
$enumReflection = $enumMeta->getClassReflection();
if (!$enumReflection->isFinal() && !$enumReflection->isAbstract()) {
throw new UsageException(
"Enum root class must be either abstract or final.\n"
. "Final is used when one type is enough for all enum instance values.\n"
. 'Abstract is used when enum values are always instances of child classes of enum root class.'
);
}
}
}
......@@ -7,7 +7,7 @@ require __DIR__ . '/../bootstrap.php';
* @method static EnumString VALUE1()
* @method static EnumString VALUE2()
*/
class EnumString extends \Grifart\Enum\Enum
final class EnumString extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
......@@ -26,7 +26,7 @@ class EnumString extends \Grifart\Enum\Enum
* @method static EnumInt VALUE1()
* @method static EnumInt VALUE2()
*/
class EnumInt extends \Grifart\Enum\Enum
final class EnumInt extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
......
......@@ -6,7 +6,7 @@ require __DIR__ . '/../bootstrap.php';
* @method static OrderState ACTIVE()
* @method static OrderState DELIVERED()
*/
class OrderState extends \Grifart\Enum\Enum
final class OrderState extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
......
......@@ -5,7 +5,7 @@ require __DIR__ . '/../bootstrap.php';
* @method static EqualsState NEW()
* @method static EqualsState ACTIVE()
*/
class EqualsState extends \Grifart\Enum\Enum
final class EqualsState extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
......
......@@ -7,7 +7,7 @@ require __DIR__ . '/../bootstrap.php';
* @method static Enum1 VALUE1()
* @method static Enum1 VALUE2()
*/
class Enum1 extends \Grifart\Enum\Enum
final class Enum1 extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
protected const VALUE1 = 'value1';
......
......@@ -7,7 +7,7 @@ require __DIR__ . '/../bootstrap.php';
* @method static Enum1 VALUE1()
* @method static Enum1 VALUE2()
*/
class Enum1 extends \Grifart\Enum\Enum
final class Enum1 extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
......
<?php declare(strict_types=1);
namespace Tests\Grifart\Enum\Consistency;
use Grifart\Enum\UsageException;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
class UndecidedEnum extends \Grifart\Enum\Enum
{
protected static function provideInstances(): array
{
return [];
}
}
Assert::exception(
function () {
UndecidedEnum::getAvailableValues();
},
UsageException::class,
"Enum root class must be either abstract or final.\n"
. "Final is used when one type is enough for all enum instance values.\n"
. 'Abstract is used when enum values are always instances of child classes of enum root class.'
);
<?php declare(strict_types=1);
require __DIR__ . '/../bootstrap.php';
class MethodAnnotationsMissing extends \Grifart\Enum\Enum
final class MethodAnnotationsMissing extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
......
......@@ -11,7 +11,7 @@ require __DIR__ . '/../bootstrap.php';
* @method static MissingInstanceEnum STATE_A()
* @method static MissingInstanceEnum STATE_B()
*/
class MissingInstanceEnum extends \Grifart\Enum\Enum
abstract class MissingInstanceEnum extends \Grifart\Enum\Enum
{
protected const STATE_A = 'a';
protected const STATE_B = 'b';
......
......@@ -5,7 +5,7 @@ require __DIR__ . '/../../bootstrap.php';
use Tester\Assert;
class OrderState {
final class OrderState {
public const NEW = 'new';
public const PROCESSING = 'processing';
}
......
......@@ -9,7 +9,7 @@ use Tester\Assert;
* @method static OrderState NEW()
* @method static OrderState PROCESSING()
*/
class OrderState extends \Grifart\Enum\Enum {
final class OrderState extends \Grifart\Enum\Enum {
use \Grifart\Enum\AutoInstances;
public const NEW = 'new';
public const PROCESSING = 'processing';
......
......@@ -9,7 +9,7 @@ use Tester\Assert;
* @method static OrderState NEW()
* @method static OrderState PROCESSING()
*/
class OrderState extends \Grifart\Enum\Enum {
final class OrderState extends \Grifart\Enum\Enum {
use \Grifart\Enum\AutoInstances;
private const NEW = 'new';
private const PROCESSING = 'processing';
......
......@@ -9,7 +9,7 @@ use Tester\Assert;
* @method static OrderState NEW()
* @method static OrderState PROCESSING()
*/
class OrderState extends \Grifart\Enum\Enum {
final class OrderState extends \Grifart\Enum\Enum {
use \Grifart\Enum\AutoInstances;
private const NEW = 'new';
private const PROCESSING = 'processing';
......
......@@ -5,7 +5,7 @@ require __DIR__ . '/../bootstrap.php';
* @method static ReflectionConstantNames NEW()
* @method static ReflectionConstantNames ACTIVE()
*/
class ReflectionConstantNames extends \Grifart\Enum\Enum
final class ReflectionConstantNames extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
......
......@@ -5,7 +5,7 @@ require __DIR__ . '/../bootstrap.php';
* @method static AvailableValuesEnum NEW()
* @method static AvailableValuesEnum ACTIVE()
*/
class AvailableValuesEnum extends \Grifart\Enum\Enum
final class AvailableValuesEnum extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
......
......@@ -5,7 +5,7 @@ require __DIR__ . '/../bootstrap.php';
/**
* @method static Enum1 VALUE()
*/
class Enum1 extends \Grifart\Enum\Enum
final class Enum1 extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
protected const VALUE = 'value';
......
......@@ -7,7 +7,7 @@ require __DIR__ . '/../bootstrap.php';
/**
* @method static Enum1 VALUE()
*/
class Enum1 extends \Grifart\Enum\Enum
final class Enum1 extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
protected const VALUE = 'value';
......
......@@ -6,7 +6,7 @@ require __DIR__ . '/../bootstrap.php';
* @method static EnumMixedKeys VALUE_STRING()
* @method static EnumMixedKeys VALUE_INT()
*/
class EnumMixedKeys extends \Grifart\Enum\Enum
final class EnumMixedKeys extends \Grifart\Enum\Enum
{
use Grifart\Enum\AutoInstances;
......
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