New in Symfony 6.1: Improvements Related to Types

UIDs Controller Resolver

        Contributed by Thomas Calvet
         in #44665.

Symfony provides argument value resolvers to transform certain arguments of controller methods into other objects. That's how you can get for example the current request object just by typing any argument with the Response class. In Symfony 6.1 we're add a new resolver to transform UID values into UID objects. For example, in previous Symfony versions you need to do this to get a UUID v4 from a controller argument:

    #[Route(path: '/token/{token}')]

public function someControllerMethod(string $token): Response { $token = UuidV4::fromRfc4122($token); // ... }

In Symfony 6.1 you can do the same type-hinting the controller argument with the type of object you want to receive:

    use Symfony\Component\Uid\UuidV4;

[Route(path: '/token/{token}')]

public function someControllerMethod(UuidV4 $token): Response { // ... }

Support for Mutation of Constructor Promoted Properties

        Contributed by Raphaël Voisin
         in #45062.

Starting from PHP 8.0, you can use constructor property promotion to declare properties directly in the constructor signature. However, you can use PHPdoc annotations to change the type of the properties. In Symfony 6.1 we've improved the PropertyInfo component to take into account those PHPdoc modifications. In the following example, the type of $someProperty will be reported as string instead of mixed:

    class SomeClass

{ /**

  • @param string $someProperty */ public function __construct(private mixed $someProperty) { } }

Backend Enums Controller Resolver

        Contributed by Maxime Steinhausser
         in #44831.

Another controller argument resolver added in Symfony 6.1 allows to transform arguments into backend enums cases. Take for example the well-known Suit enum example:

    namespace App\Model;

enum Suit: string { case Hearts = 'H'; case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; }

In Symfony 6.1 you can use Suit as the type of the controller argument. If the route parameter matches any of the enum values, Symfony will inject the enum case. Otherwise, it will return a 404 HTTP error:

    class CardController

{

[Route('/cards/{suit}')]

public function list(Suit $suit): Response
{
    // ...
}

}

PHPStan Pseudo-Types Support

        Contributed by Emil Masiakowski
         in #44451.

Thanks to the popularity of static analyzers such as PHPStan, more and more developers use pseudo-types that describe arguments and return values with more precision. For example, instead of string you can use non-empty-string; instead of int you can use positiveInt; instead of int|float you can use number, etc. In Symfony 6.1 we've improved the PropertyInfo to properly infer the right PHP type of a variable described with these pseudo-types:

    class SomeClass

{ /* @var negative-int / public $property1;

/** @var non-empty-array */
public $property2;

/** @var non-empty-list */
public $property3;

/** @var non-empty-lowercase-string */
public $property4;

}

In Symfony 6.1, the PHP type of these properties will be correctly inferred as integer, array, array and string respectively.

                Sponsor the Symfony project.

https://symfony.com/blog/new-in-symfony-6-1-improvements-related-to-types?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Created 3y | May 20, 2022, 11:20:10 AM


Login to add comment

Other posts in this group

Nicolas Grekas returns to Montreal on February 25

Get ready, Montreal! The Full Stack Gurus meetup is back in 2025, and we can't wait to reconnect with the Symfony and PHP community. Mark your calendar for Tuesday, February 25th at 5:00 PM— t

Feb 6, 2025, 4:50:21 PM | Symfony
SymfonyDay Chicago 2025 - The full schedule is live!

Mark your calendars for March 17, 2025 because SymfonyDay Chicago 2025 is shaping up to be an unmissable event! This full-day conference is dedicated to celebrating the amazing contributions o

Feb 5, 2025, 3:20:20 PM | Symfony
SymfonyLive Paris 2025 : Développer avec API Platform 4, ça change quoi ?

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

Feb 4, 2025, 9:20:06 AM | Symfony
SymfonyLive Berlin 2025: Where have the women of tech history gone?

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

As we are now unveiling th

Feb 3, 2025, 2:40:15 PM | Symfony
A Week of Symfony #944 (27 January - 2 February 2025)

This week, Symfony released maintenance versions 6.4.18, 7.1.11 and 7.2.3. In addition, Twig published a security advisory and we shared several updates about upcoming Symfony conferences. Lastly, Sym

Feb 2, 2025, 10:50:16 AM | Symfony
Back on the inspiring SymfonyCon Vienna 2024!

We were absolutely thrilled to gather with the incredible Symfony community for the first time in Vienna, Austria, from December 5th to 6th, surrounded by the warm and festive atmosphere of the

Jan 31, 2025, 12:30:18 PM | Symfony
SymfonyLive Paris 2025 : Reveal of workshop topics!

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

💻

Jan 30, 2025, 8:50:03 AM | Symfony