refactor(BaseEnum): improve cache handling and type annotations

Enhanced type annotations for cache variables and improved readability of the toLabels, toValues, and getCustomDefinitions methods.
This commit is contained in:
Jérémy Munsch 2026-11-16 11:44:31 +01:00
parent cbb1ed812b
commit ea06b37a47
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91

View file

@ -36,12 +36,14 @@ trait BaseEnum
*/
public static function toLabels(): array
{
/** @var array<string, string>|null $cache */
static $cache = null;
if ($cache !== null) {
return $cache;
}
/** @var array<string, string>|null $labels */
$labels = defined(self::class . '::' . self::LABELS_CONSTANT_NAME)
? constant(self::class . '::' . self::LABELS_CONSTANT_NAME)
: null;
@ -62,17 +64,21 @@ trait BaseEnum
*/
public static function toValues(): array
{
/** @var array<string, V>|null $cache */
static $cache = null;
if ($cache !== null) {
return $cache;
}
return $cache = array_column(
/** @var array<string, V> $values */
$values = array_column(
self::cases(),
'value',
'name'
);
return $cache = $values;
}
/**
@ -136,13 +142,15 @@ trait BaseEnum
*/
private static function getCustomDefinitions(): array
{
/** @var array<string, array<string, mixed>>|null $cache */
static $cache = null;
if ($cache !== null) {
return $cache;
}
$names = array_column(self::cases(), 'name', 'name');
$names = array_column(self::cases(), 'name', 'name');
/** @var array<string, array<string, mixed>> $filteredConstants */
$filteredConstants = [];
foreach ((new \ReflectionClass(self::class))->getConstants() as $key => $constant) {
@ -151,6 +159,7 @@ trait BaseEnum
}
if (! empty(array_intersect_key($constant, $names))) {
/** @var array<string, mixed> $constant */
$filteredConstants[$key] = $constant;
}
}