I want to remove redundant method_exists() check #51283
-
I want to remove the unnecessary code of PR link #51282 enum() method from \Illuminate\Http\Concerns\InteractsWithInput As you see when So I have done those changes PR link #51282 and this is method from PR /**
* Retrieve input from the request as an enum.
*
* @template TEnum
*
* @param string $key
* @param class-string<TEnum> $enumClass
* @return TEnum|null
*/
public function enum($key, $enumClass)
{
if ($this->filled($key) && enum_exists($enumClass)) {
return $enumClass::tryFrom($this->input($key));
}
return null;
} I think it is correct refactoring. I have also added a test for the enum() method's handling of empty value requests. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The root enum class, Even the generic template on the method leaves the type narrowing out of its declaration, so passing any PHP class string there would be legal, so the usefulness of that is even minimal at best. |
Beta Was this translation helpful? Give feedback.
The root enum class,
UnitEnum
, does not have atryFrom()
method. This is only part of theBackedEnum
class. So technically themethod_exists()
check, even if it could be expressed better but probably won't due to Laravel's liberal use of duck-typing in the framework, is required. I guess technically this way one could create a non-backed enum and implement a customtryFrom()
method, but that seems like such an off edge case for PHP as a whole that there's little benefit to the framework allowing this.Even the generic template on the method leaves the type narrowing out of its declaration, so passing any PHP class string there would be legal, so the usefulness of that is even minimal at b…