SymfonyWorld Online 2021 Winter Edition December 9 – 10, 2021 100% Online 25 talks and 10 workshops

Symfony Type Declarations, Return Types and PHPUnit Compatibility

If you follow the Living on the Edge category of this blog, you'll find all the latest and greatest new features of each Symfony version. Big and small features that help you create your projects while having the best possible developer experience.

However, this is only part of the development activity needed to create and maintain Symfony. In this blog post we'll mention some changes that required hundreds of hours of work but remained invisible because they are not part of a shiny new feature.

Added PHP Type Declarations

PHP type declarations (or "type hints") add information about the data types of the function and method arguments. Symfony code didn't have type declarations yet because, according to the Symfony Release Process, we cannot add the latest PHP features right away and we need to wait until certain Symfony versions are released to make those changes.

Symfony 5.0 was the perfect moment to add these type declarations to our entire code base. This was a painstaking process because, even if some work could be automated, each change had to be reviewed manually.

The reason is that it's not only about removing PHPdoc comments and adding the types to the method arguments. You also need to check the rest of the code to look for data type castings. For example:

- * @param bool|null $enabled
- */
-public function setStrictRequirements($enabled)
+public function setStrictRequirements(?bool $enabled)
-    $this->strictRequirements = null === $enabled ? null : (bool) $enabled;
+    $this->strictRequirements = $enabled;

Check out the Pull Request #32179 to get an idea of the size of this change. Tens of thousands of lines were changed in thousands of files and tens of hours were spent reviewing and merging everything.

Thanks to @jschaedl, @Simperfit, @Tobion, @Matts, @smoench, @vudaltsov, @julien57, @azjezz, @tigitz, @andreia, @thomasbisignani, @lyrixx, @xabbuh for contributing these changes. Special thanks to @derrabus for coordinating the work and to @nicolas-grekas for doing the actual merge and conflict solving.

Added PHP Return Types

PHP return types allow functions and methods to specify the data type of their returned values. We considered adding these return types in Symfony 5.0 but we finally didn't do it because of two reasons:

  • The burden this would put on the community is immense, because third-party libraries and bundles should also be updated to be compatible with this new Symfony code that includes return types. In other words, Symfony should be the last to add these return types, not the first one.
  • Given the complexity and flexibility of Symfony's code base, we'd need return type covariance, which is only available in PHP 7.4 (Symfony 5.x requires PHP 7.2).

In any case, we have a plan to add these return types in Symfony 6.0. Check out the Pull Request #33236 to see the clever trick used to add these return types automatically thanks to the DebugClassLoader. Special thanks to @fancyweb for fixing all current return types defined in PHPdoc comments (see Pull Request #30323).

Added Compatibility with All PHP and PHPUnit Versions

The last recent feature that required an insane amount of work but developers take it for granted is the PHPUnit Bridge compatibility with all PHP versions from 5.5 to 7.4 and all PHPUnit versions from 4.8 to 8.0.

As mentioned in the New in Symfony 4.4: PHPUnit Polyfills blog post, we added lots of polyfills to make most of PHPUnit features available regardless of the PHPUnit version used in your application. This is needed for Symfony's own testing but will also help those applications that must keep a broad compatibility with legacy PHP and PHPUnit versions.

Check out the Pull Request #32844 to see the tens of related pull requests that were needed to achieve this. Special thanks to @jderusse who did most of the work related to this feature.

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

Symfony Type Declarations, Return Types and PHPUnit Compatibility

Tweet this


Huge amount of tedious work! We all know that wasn‘t fun at all. So a special thank you to everyone involved!
This is simply amazing. Keep up the good work!
Awesome ! Thanks everyone for the work
Symfony 6.0 :D
Wow! Thanks to all contributors for the great work behind the scenes.
> [...], we'd need return type covariance, which is only available in PHP 7.4 [...]

I've hit this wall too. I'm surprised that covariance and contravariance arrived so late in PHP. The next big missing thing is generics, e.g., `$container->get< Mailer >()` would be amazing.
Awesome work! Thanks to everyone involved!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.