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:
parent
cbb1ed812b
commit
ea06b37a47
1 changed files with 11 additions and 2 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue