Skip to content

HTTP, Form, Serializer, and Security

SymfonyPlugin registers return-type providers for Symfony 6.4 and 7.x methods whose arguments guarantee a narrower return type than Symfony declares.

HttpFoundation

Method Condition Narrowed return type
HeaderBag::get($key, $default) $default is a non-null string. string
ParameterBag::getEnum($key, $class, $default) $class names a BackedEnum and $default is a non-null value of that enum. T, the target BackedEnum
InputBag::getEnum($key, $class, $default) $class names a BackedEnum and $default is a non-null value of that enum. T, the target BackedEnum
Request::getRequestFormat($default) $default is omitted or a non-null string. string
Request::getPreferredFormat($default) $default is omitted or a non-null string. string
Request::getPreferredLanguage($locales) $locales is a non-empty list<string> or a keyed string array with a required 0 entry. string
ResponseHeaderBag::getCookies($format) $format is omitted or 'flat' (COOKIES_FLAT). list<Cookie>
ResponseHeaderBag::getCookies($format) $format is 'array' (COOKIES_ARRAY). array<array-key, array<array-key, array<array-key, Cookie>>>

HttpKernel

Method Condition Narrowed return type
ControllerEvent::getAttributes($className) $className is a literal class string or class-string<T> naming an existing class or interface. list<T>
ControllerArgumentsEvent::getAttributes($className) $className is a literal class string or class-string<T> naming an existing class or interface. list<T>

Form

FormGetErrorsReturnTypeProvider narrows Symfony\Component\Form\FormInterface::getErrors($deep = false, $flatten = true):

Arguments Narrowed return type
$deep is false or $flatten is true, including omitted defaults FormErrorIterator<FormError>
$deep can be true and $flatten can be false FormErrorIterator<FormError\|FormErrorIterator>

Serializer

SerializerReturnTypeProvider targets:

  • Symfony\Component\Serializer\SerializerInterface::deserialize()
  • Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize()
  • Symfony\Component\Serializer\Serializer::deserialize()
  • Symfony\Component\Serializer\Serializer::denormalize()

It resolves the second argument $type when the referenced class-like exists in Mago's codebase:

$type argument Narrowed return type
Foo::class or 'App\Dto\Foo' Foo
'App\Dto\Foo[]' array<array-key, Foo>
'App\Dto\Foo[][]' at any depth of [] array<array-key, array<array-key, Foo>>
class-string<Foo> Foo

Messenger

WrappedExceptionsReturnTypeProvider targets getWrappedExceptions($class) on:

  • Symfony\Component\Messenger\Exception\WrappedExceptionsInterface
  • Symfony\Component\Messenger\Exception\WrappedExceptionsTrait
  • Symfony\Component\Messenger\Exception\HandlerFailedException
  • Symfony\Component\Messenger\Exception\DelayedMessageHandlingException

When $class is a literal class string or class-string<T> naming Throwable or a subtype, the provider returns array<array-key, T>. Non-Throwable marker interfaces keep Symfony's Throwable[] return type.

Security

Set MAGO_SYMFONY_USER_CLASS or pass userClass to SymfonyExtension::plugins() to name your project's Symfony\Component\Security\Core\User\UserInterface class or interface. When that symbol exists in Mago's codebase, SecurityUserReturnTypeProvider narrows getUser() to that configured type or null, such as App\Entity\User|null, on:

  • Symfony\Bundle\FrameworkBundle\Controller\AbstractController::getUser()
  • Symfony\Bundle\SecurityBundle\Security::getUser()
  • Symfony\Component\Security\Core\Security::getUser()
  • Symfony\Component\Security\Core\Authentication\Token\TokenInterface::getUser()

See Configuration for how to set MAGO_SYMFONY_USER_CLASS.