Shalvah 1 год назад
Родитель
Сommit
6291e99536

+ 6 - 1
src/Attributes/GenericParam.php

@@ -39,8 +39,13 @@ class GenericParam
             return $this->enum;
         }
 
-        if (function_exists('enum_exists') && enum_exists($this->enum)) {
+        if (function_exists('enum_exists') && enum_exists($this->enum)
+            && method_exists($this->enum, 'tryFrom')
+        ) {
             return array_map(
+            // $case->value only exists on BackedEnums, not UnitEnums
+            // method_exists($enum, 'tryFrom') implies $enum instanceof BackedEnum
+            // @phpstan-ignore-next-line
                 fn ($case) => $case->value,
                 $this->enum::cases()
             );

+ 4 - 1
src/Extracting/ParsesValidationRules.php

@@ -145,7 +145,7 @@ trait ParsesValidationRules
         // Now this will return the complete ruleset.
         // Nested array parameters will be present, with '*' replaced by '0'
         $newRules = Validator::make($testData, $rules)->getRules();
-       
+
         return collect($newRules)->mapWithKeys(function ($val, $paramName) use ($rules) {
             // Transform the key names back from '__asterisk__' to '*'
             if (Str::contains($paramName, '__asterisk__')) {
@@ -206,6 +206,9 @@ trait ParsesValidationRules
             $type = $property->getValue($rule);
 
             if (enum_exists($type) && method_exists($type, 'tryFrom')) {
+                // $case->value only exists on BackedEnums, not UnitEnums
+                // method_exists($enum, 'tryFrom') implies $enum instanceof BackedEnum
+                // @phpstan-ignore-next-line
                 $cases = array_map(fn ($case) => $case->value, $type::cases());
                 $parameterData['type'] = gettype($cases[0]);
                 $parameterData['enumValues'] = $cases;

+ 3 - 0
src/Extracting/Strategies/GetFromInlineValidatorBase.php

@@ -88,6 +88,9 @@ class GetFromInlineValidatorBase extends Strategy
                         ($enum = $this->extractEnumClassFromArrayItem($arrayItem)) &&
                         enum_exists($enum) && method_exists($enum, 'tryFrom')
                     ) {
+                        // $case->value only exists on BackedEnums, not UnitEnums
+                        // method_exists($enum, 'tryFrom') implies $enum instanceof BackedEnum
+                        // @phpstan-ignore-next-line
                         $rulesList[] = 'in:' . implode(',', array_map(fn ($case) => $case->value, $enum::cases()));
                     }
                 }