New in Symfony 7.1: TypeInfo Component

This is the first article of the series that shows the most important new features
introduced by Symfony 7.1, which will be released at the end of May 2024.


Contributed by
Mathias Arlaud
and Baptiste Leduc
in #51476.

The PropertyInfo component extracts information about the types of PHP class
properties using metadata from PHP reflection, Doctrine, PHPDoc, PHPStan, etc.

readonly class Person
{
public function __construct(
public string $firstName,
)
{
}
}

$types = $propertyInfo->getTypes(Person::class, ‚firstName‘);
/*
$types = array(1) {
[0] =>
class SymfonyComponentPropertyInfoType (6) {
private $builtinType => string(6) „string“
private $nullable => bool(false)
private $class => NULL
private $collection => bool(false)
private $collectionKeyType => NULL
private $collectionValueType => NULL
}
}
*/

This component works well but it has two main limitations:

It can only get type information from properties and not from method arguments,
return types, and raw strings;
It can’t properly describe type unions, intersections or generics (this is
partially solved by returning an array of Type objects).

That’s why in Symfony 7.1 we’re introducing a new TypeInfo component to solve
these problems. The first problem is solved by defining a hierarchy of classes to
describe the types (this will be expanded in the future with new classes when
PHP adds new features related to types):

SymfonyComponentTypeInfoType
├─ BultinType
├─ UnionType
├─ IntersectionType
├─ GenericType
├─ Template
└─ ObjectType
└─ EnumType
└─ BackedEnumType

The second problem is solved with a series of type information extractors that
work not only on class properties (ReflectionReturnTypeResolver to get return
types using PHP reflection; StringTypeResolver to get type information using
PHPDoc annotations; etc.)

This component has been introduced as an experimental feature (meaning that
its API could change a bit before considering it stable) but it’s already in use
inside the PropertyInfo component. In future Symfony versions we’ll deprecate
the Type class from PropertyInfo and
all its related features in favor of the new Type class from TypeInfo component.

Sponsor the Symfony project.

Symfony Blog

Read More

Latest News

PHP-Releases

PHP 8.3.7 released!

PHP 8.2.19 released!

Generated by Feedzy