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

A Week of Symfony #957 (April 28 – May 4, 2025)

This week, Symfony 6.4.21 and 7.2.6 maintenance versions were released. In addition, we published the first beta of Symfony 7.3 so you can test it in your own apps before its final release at the end

May 4, 2025, 8:40:05 AM | Symfony
Symfony 7.2.6 released

Symfony 7.2.6 has just been released. Here is the list of the most important changes since 7.2.5:

bug #60288 [VarExporter] dump default value for property hooks if present (@xabbuh)

bug #60267 [C

May 2, 2025, 12:40:05 PM | Symfony
Symfony 7.3.0-BETA1 released

Symfony 7.3.0-BETA1 has just been released. Here is the list of the most important changes since 7.2:

feature #60232 Add PHP config support for routing (@fabpot)

feature #60102 [HttpFoundation] A

May 2, 2025, 12:40:04 PM | Symfony
SymfonyOnline June 2025 : Efficient Web Scraping with Symfony & PHP

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

May 2, 2025, 12:40:03 PM | Symfony
New in Symfony 7.3: Configurable Compound Rate Limiter

Contributed by Kevin Bond in

May 2, 2025, 10:20:09 AM | Symfony
Symfony 6.4.21 released

Symfony 6.4.21 has just been released. Here is the list of the most important changes since 6.4.20:

bug #60288 [VarExporter] dump default value for property hooks if present (@xabbuh)

bug #60268

May 2, 2025, 10:20:08 AM | Symfony
New in Symfony 7.3: Extra Runtime Dot Env Files

Contributed by Nathan Page in

May 1, 2025, 8:40:12 AM | Symfony