fix: 🐛 delete legacy base enum and backed int for enums

This commit is contained in:
K Muller 2026-09-24 14:56:17 +02:00
parent 9946832915
commit e3c2662547
14 changed files with 37 additions and 524 deletions

View file

@ -4,25 +4,9 @@ declare(strict_types=1);
namespace Domain\Enums;
use Shared\Enums\BaseEnum;
enum BarcodeFormat: int
enum BarcodeFormat: string
{
/** @use BaseEnum<int> */
use BaseEnum;
case EAN13 = 1;
case CODE128 = 2;
case GS1_128 = 3;
/**
* Optional labels definition
*
* @phpstan-ignore-next-line
*/
private const LABELS = [
self::EAN13->name => 'EAN',
self::CODE128->name => 'CODE 128',
self::GS1_128->name => 'GS1_128',
];
case EAN13 = "ean";
case CODE128 = "code128";
case GS1_128 = "gs1";
}

View file

@ -4,25 +4,9 @@ declare(strict_types=1);
namespace Domain\Enums\Campaign;
use Shared\Enums\BaseEnum;
enum CampaignStatus: int
enum CampaignStatus: string
{
/** @use \Shared\Enums\BaseEnum<int> */
use BaseEnum;
case CREATED = 1;
case TRANSFORMED = 2;
case REJECTED = 3;
/**
* Optional labels definition
*
* @phpstan-ignore-next-line
*/
private const LABELS = [
self::CREATED->name => 'Crée',
self::TRANSFORMED->name => 'Transformé',
self::REJECTED->name => 'Rejeté'
];
case CREATED = "created";
case TRANSFORMED = "transformed";
case REJECTED = "rejected";
}

View file

@ -4,23 +4,8 @@ declare(strict_types=1);
namespace Domain\Enums\Campaign;
use Shared\Enums\BaseEnum;
enum CampaignType: int
enum CampaignType: string
{
/** @use \Shared\Enums\BaseEnum<int> */
use BaseEnum;
case CLUB = 1;
case GAME = 2;
/**
* Optional labels definition
*
* @phpstan-ignore-next-line
*/
private const LABELS = [
self::CLUB->name => 'CLUB',
self::GAME->name => 'GAME',
];
case CLUB = "club";
case GAME = "game";
}

View file

@ -4,25 +4,9 @@ declare(strict_types=1);
namespace Domain\Enums;
use Shared\Enums\BaseEnum;
enum Country: int
enum Country: string
{
/** @use \Shared\Enums\BaseEnum<int> */
use BaseEnum;
case FR = 1;
case BE = 2;
case LU = 3;
/**
* Optional labels definition
*
* @phpstan-ignore-next-line
*/
private const LABELS = [
self::FR->name => 'FR',
self::BE->name => 'BE',
self::LU->name => 'LU',
];
case FR = "fr";
case BE = "be";
case LU = "lu";
}

View file

@ -23,6 +23,6 @@ final readonly class CouponLegalInformation implements Equatable
#[\Override]
public function equals(mixed $other): bool
{
return $other instanceof self && $this->legalText === $other->legalText && $this->forLocale->equals($other->forLocale);
return $other instanceof self && $this->legalText === $other->legalText && $this->forLocale === $other->forLocale;
}
}

View file

@ -41,6 +41,6 @@ final readonly class CouponProductInfo implements Equatable
&& $this->name === $other->name
&& $this->range === $other->range
&& $this->validityText === $other->validityText
&& $this->forLocale->equals($other->forLocale);
&& $this->forLocale === $other->forLocale;
}
}

View file

@ -1,133 +0,0 @@
<?php
declare(strict_types=1);
namespace Shared\Enums;
/**
* @template V of string|int
*
* @mixin \BackedEnum
*/
trait BaseEnum
{
private const LABELS_CONSTANT_NAME = 'LABELS';
/**
* @return V
*/
public function value(): string | int
{
return $this->value;
}
public function label(): string
{
return self::toLabels()[$this->name] ?? ucfirst(strtolower($this->name));
}
public function name(): string
{
return $this->name;
}
/**
* @return array<string,string>
*/
public static function toLabels(): array
{
/** @var array<string, string>|null */
static $cache = null;
if ($cache !== null) {
return $cache;
}
$labels = defined(self::class . '::' . self::LABELS_CONSTANT_NAME)
? constant(self::class . '::' . self::LABELS_CONSTANT_NAME)
: null;
$result = [];
foreach (self::cases() as $enum) {
if ($labels !== null && ! isset($labels[$enum->name])) {
throw new \Error('Missing definition label ' . self::class . " for '{$enum->name}'");
}
$result[$enum->name] = $labels[$enum->name] ?? ucfirst(strtolower($enum->name));
}
return $cache = $result;
}
/**
* @return array<string, V>
*/
public static function toValues(): array
{
/** @var array<string, int>|null */
static $cache = null;
if ($cache !== null) {
return $cache;
}
return $cache = array_column(
self::cases(),
'value',
'name'
);
}
/**
* @param self|V $value
*/
public static function make(self | int | string $value, string $field = 'value'): ?self
{
if ($value instanceof self) {
return $value;
}
if ($field === 'value') {
$tryValue = null;
try {
$tryValue = self::tryFrom(intval($value));
} catch (\ValueError) {
$tryValue = self::tryFrom(strval($value));
} finally {
if ($tryValue !== null) {
return $tryValue;
}
}
}
if ($field === 'label') {
$normalizedSearch = preg_replace('/[\W_]+/u', '', mb_strtolower((string) $value));
foreach (self::cases() as $enum) {
$normalizedLabel = preg_replace('/[\W_]+/u', '', mb_strtolower($enum->label()));
if ($normalizedLabel === $normalizedSearch) {
return $enum;
}
}
return null;
}
foreach (self::cases() as $enum) {
if ($enum->{$field} === $value) {
return $enum;
}
}
return null;
}
public function equals(mixed $other): bool
{
return $this === $other;
}
public function jsonSerialize(): mixed
{
return $this->value;
}
}

View file

@ -4,19 +4,7 @@ declare(strict_types=1);
namespace Shared\Enums;
enum Currency: int
enum Currency: string
{
/** @use BaseEnum<int> */
use BaseEnum;
case EUR = 1;
/**
* Optional labels definition
*
* @phpstan-ignore-next-line
*/
private const LABELS = [
self::EUR->name => 'Euro',
];
case EUR = "euro";
}

View file

@ -31,12 +31,12 @@ final readonly class Money implements \Stringable, Equatable
public function equals(mixed $other): bool
{
return $other instanceof self
&& $this->currency->equals($other->currency)
&& $this->currency->value === $other->currency->value
&& $this->value === $other->value;
}
public function __toString(): string
{
return sprintf('%d %s', $this->value, $this->currency->name());
return sprintf('%d %s', $this->value, $this->currency->name);
}
}

View file

@ -11,71 +11,13 @@ covers(BarcodeFormat::class);
describe('barcode format', function (): void {
it('has the expected cases with their backing values', function (): void {
// THEN
expect(BarcodeFormat::EAN13->value())->toBe(1)
->and(BarcodeFormat::CODE128->value())->toBe(2)
->and(BarcodeFormat::GS1_128->value())->toBe(3);
expect(BarcodeFormat::EAN13->value)->toBe("ean")
->and(BarcodeFormat::CODE128->value)->toBe("code128")
->and(BarcodeFormat::GS1_128->value)->toBe("gs1");
});
it('exposes its case name', function (): void {
// THEN
expect(BarcodeFormat::EAN13->name())->toBe('EAN13');
});
it('returns the defined label', function (): void {
// THEN
expect(BarcodeFormat::EAN13->label())->toBe('EAN')
->and(BarcodeFormat::CODE128->label())->toBe('CODE 128')
->and(BarcodeFormat::GS1_128->label())->toBe('GS1_128');
});
it('lists all labels keyed by case name', function (): void {
// THEN
expect(BarcodeFormat::toLabels())->toBe([
'EAN13' => 'EAN',
'CODE128' => 'CODE 128',
'GS1_128' => 'GS1_128',
]);
});
it('lists all values keyed by case name', function (): void {
// THEN
expect(BarcodeFormat::toValues())->toBe([
'EAN13' => 1,
'CODE128' => 2,
'GS1_128' => 3,
]);
});
it('makes an instance from its backing value', function (): void {
// THEN
expect(BarcodeFormat::make(1))->toBe(BarcodeFormat::EAN13);
});
it('makes an instance from a label', function (): void {
// THEN
expect(BarcodeFormat::make('CODE 128', 'label'))->toBe(BarcodeFormat::CODE128);
});
it('returns the same instance when already an enum case', function (): void {
// THEN
expect(BarcodeFormat::make(BarcodeFormat::GS1_128))->toBe(BarcodeFormat::GS1_128);
});
it('returns null for an unknown value', function (): void {
// THEN
expect(BarcodeFormat::make(99))->toBeNull();
});
it('implements equals correctly', function (): void {
// THEN
expect(BarcodeFormat::EAN13->equals(BarcodeFormat::EAN13))->toBeTrue()
->and(BarcodeFormat::EAN13->equals(BarcodeFormat::CODE128))->toBeFalse()
->and(BarcodeFormat::EAN13->equals(null))->toBeFalse();
});
it('serializes to json as its backing value', function (): void {
// THEN
expect(BarcodeFormat::EAN13->jsonSerialize())->toBe(1)
->and(json_encode(BarcodeFormat::EAN13))->toBe('1');
expect(BarcodeFormat::EAN13->name)->toBe('EAN13');
});
});

View file

@ -11,71 +11,13 @@ covers(CampaignStatus::class);
describe('campaign status', function (): void {
it('has the expected cases with their backing values', function (): void {
// THEN
expect(CampaignStatus::CREATED->value())->toBe(1)
->and(CampaignStatus::TRANSFORMED->value())->toBe(2)
->and(CampaignStatus::REJECTED->value())->toBe(3);
expect(CampaignStatus::CREATED->value)->toBe("created")
->and(CampaignStatus::TRANSFORMED->value)->toBe("transformed")
->and(CampaignStatus::REJECTED->value)->toBe("rejected");
});
it('exposes its case name', function (): void {
// THEN
expect(CampaignStatus::CREATED->name())->toBe('CREATED');
});
it('returns the defined label', function (): void {
// THEN
expect(CampaignStatus::CREATED->label())->toBe('Crée')
->and(CampaignStatus::TRANSFORMED->label())->toBe('Transformé')
->and(CampaignStatus::REJECTED->label())->toBe('Rejeté');
});
it('lists all labels keyed by case name', function (): void {
// THEN
expect(CampaignStatus::toLabels())->toBe([
'CREATED' => 'Crée',
'TRANSFORMED' => 'Transformé',
'REJECTED' => 'Rejeté',
]);
});
it('lists all values keyed by case name', function (): void {
// THEN
expect(CampaignStatus::toValues())->toBe([
'CREATED' => 1,
'TRANSFORMED' => 2,
'REJECTED' => 3,
]);
});
it('makes an instance from its backing value', function (): void {
// THEN
expect(CampaignStatus::make(1))->toBe(CampaignStatus::CREATED);
});
it('makes an instance from a label', function (): void {
// THEN
expect(CampaignStatus::make('Transformé', 'label'))->toBe(CampaignStatus::TRANSFORMED);
});
it('returns the same instance when already an enum case', function (): void {
// THEN
expect(CampaignStatus::make(CampaignStatus::REJECTED))->toBe(CampaignStatus::REJECTED);
});
it('returns null for an unknown value', function (): void {
// THEN
expect(CampaignStatus::make(99))->toBeNull();
});
it('implements equals correctly', function (): void {
// THEN
expect(CampaignStatus::CREATED->equals(CampaignStatus::CREATED))->toBeTrue()
->and(CampaignStatus::CREATED->equals(CampaignStatus::REJECTED))->toBeFalse()
->and(CampaignStatus::CREATED->equals(null))->toBeFalse();
});
it('serializes to json as its backing value', function (): void {
// THEN
expect(CampaignStatus::CREATED->jsonSerialize())->toBe(1)
->and(json_encode(CampaignStatus::CREATED))->toBe('1');
expect(CampaignStatus::CREATED->name)->toBe('CREATED');
});
});

View file

@ -11,67 +11,12 @@ covers(CampaignType::class);
describe('campaign type', function (): void {
it('has the expected cases with their backing values', function (): void {
// THEN
expect(CampaignType::CLUB->value())->toBe(1)
->and(CampaignType::GAME->value())->toBe(2);
expect(CampaignType::CLUB->value)->toBe("club")
->and(CampaignType::GAME->value)->toBe("game");
});
it('exposes its case name', function (): void {
// THEN
expect(CampaignType::CLUB->name())->toBe('CLUB');
});
it('returns the defined label', function (): void {
// THEN
expect(CampaignType::CLUB->label())->toBe('CLUB')
->and(CampaignType::GAME->label())->toBe('GAME');
});
it('lists all labels keyed by case name', function (): void {
// THEN
expect(CampaignType::toLabels())->toBe([
'CLUB' => 'CLUB',
'GAME' => 'GAME',
]);
});
it('lists all values keyed by case name', function (): void {
// THEN
expect(CampaignType::toValues())->toBe([
'CLUB' => 1,
'GAME' => 2,
]);
});
it('makes an instance from its backing value', function (): void {
// THEN
expect(CampaignType::make(1))->toBe(CampaignType::CLUB);
});
it('makes an instance from a label', function (): void {
// THEN
expect(CampaignType::make('GAME', 'label'))->toBe(CampaignType::GAME);
});
it('returns the same instance when already an enum case', function (): void {
// THEN
expect(CampaignType::make(CampaignType::CLUB))->toBe(CampaignType::CLUB);
});
it('returns null for an unknown value', function (): void {
// THEN
expect(CampaignType::make(99))->toBeNull();
});
it('implements equals correctly', function (): void {
// THEN
expect(CampaignType::CLUB->equals(CampaignType::CLUB))->toBeTrue()
->and(CampaignType::CLUB->equals(CampaignType::GAME))->toBeFalse()
->and(CampaignType::CLUB->equals(null))->toBeFalse();
});
it('serializes to json as its backing value', function (): void {
// THEN
expect(CampaignType::CLUB->jsonSerialize())->toBe(1)
->and(json_encode(CampaignType::CLUB))->toBe('1');
expect(CampaignType::CLUB->name)->toBe('CLUB');
});
});

View file

@ -11,71 +11,13 @@ covers(Country::class);
describe('country', function (): void {
it('has the expected cases with their backing values', function (): void {
// THEN
expect(Country::FR->value())->toBe(1)
->and(Country::BE->value())->toBe(2)
->and(Country::LU->value())->toBe(3);
expect(Country::FR->value)->toBe("fr")
->and(Country::BE->value)->toBe("be")
->and(Country::LU->value)->toBe("lu");
});
it('exposes its case name', function (): void {
// THEN
expect(Country::FR->name())->toBe('FR');
});
it('returns the defined label', function (): void {
// THEN
expect(Country::FR->label())->toBe('FR')
->and(Country::BE->label())->toBe('BE')
->and(Country::LU->label())->toBe('LU');
});
it('lists all labels keyed by case name', function (): void {
// THEN
expect(Country::toLabels())->toBe([
'FR' => 'FR',
'BE' => 'BE',
'LU' => 'LU',
]);
});
it('lists all values keyed by case name', function (): void {
// THEN
expect(Country::toValues())->toBe([
'FR' => 1,
'BE' => 2,
'LU' => 3,
]);
});
it('makes an instance from its backing value', function (): void {
// THEN
expect(Country::make(1))->toBe(Country::FR);
});
it('makes an instance from a label', function (): void {
// THEN
expect(Country::make('BE', 'label'))->toBe(Country::BE);
});
it('returns the same instance when already an enum case', function (): void {
// THEN
expect(Country::make(Country::LU))->toBe(Country::LU);
});
it('returns null for an unknown value', function (): void {
// THEN
expect(Country::make(99))->toBeNull();
});
it('implements equals correctly', function (): void {
// THEN
expect(Country::FR->equals(Country::FR))->toBeTrue()
->and(Country::FR->equals(Country::BE))->toBeFalse()
->and(Country::FR->equals(null))->toBeFalse();
});
it('serializes to json as its backing value', function (): void {
// THEN
expect(Country::FR->jsonSerialize())->toBe(1)
->and(json_encode(Country::FR))->toBe('1');
expect(Country::FR->name)->toBe('FR');
});
});

View file

@ -11,56 +11,6 @@ covers(Currency::class);
describe('currency', function (): void {
it('has the expected cases with their backing values', function (): void {
// THEN
expect(Currency::EUR->value())->toBe(1);
});
it('exposes its case name', function (): void {
// THEN
expect(Currency::EUR->name())->toBe('EUR');
});
it('returns the defined label', function (): void {
// THEN
expect(Currency::EUR->label())->toBe('Euro');
});
it('lists all labels keyed by case name', function (): void {
// THEN
expect(Currency::toLabels())->toBe([
'EUR' => 'Euro',
]);
});
it('lists all values keyed by case name', function (): void {
// THEN
expect(Currency::toValues())->toBe([
'EUR' => 1,
]);
});
it('makes an instance from its backing value', function (): void {
// THEN
expect(Currency::make(1))->toBe(Currency::EUR);
});
it('makes an instance from a label', function (): void {
// THEN
expect(Currency::make('Euro', 'label'))->toBe(Currency::EUR);
});
it('returns the same instance when already an enum case', function (): void {
// THEN
expect(Currency::make(Currency::EUR))->toBe(Currency::EUR);
});
it('returns null for an unknown value', function (): void {
// THEN
expect(Currency::make(99))->toBeNull();
});
it('implements equals correctly', function (): void {
// THEN
expect(Currency::EUR->equals(Currency::EUR))->toBeTrue()
->and(Currency::EUR->equals(null))->toBeFalse();
expect(Currency::EUR->value)->toBe("euro");
});
});