Skip to content

Commit 1220eae

Browse files
committed
Document DebugClassLoader type patch functionality
1 parent 17a7e85 commit 1220eae

File tree

1 file changed

+102
-10
lines changed

1 file changed

+102
-10
lines changed

setup/upgrade_major.rst

Lines changed: 102 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. index::
22
single: Upgrading; Major Version
33

4-
Upgrading a Major Version (e.g. 4.4.0 to 5.0.0)
4+
Upgrading a Major Version (e.g. 5.4.0 to 6.0.0)
55
===============================================
66

77
Every two years, Symfony releases a new major version release (the first number
@@ -30,7 +30,7 @@ backwards incompatible changes. To accomplish this, the "old" (e.g. functions,
3030
classes, etc) code still works, but is marked as *deprecated*, indicating that
3131
it will be removed/changed in the future and that you should stop using it.
3232

33-
When the major version is released (e.g. 5.0.0), all deprecated features and
33+
When the major version is released (e.g. 6.0.0), all deprecated features and
3434
functionality are removed. So, as long as you've updated your code to stop
3535
using these deprecated features in the last version before the major (e.g.
3636
``4.4.*``), you should be able to upgrade without a problem. That means that
@@ -95,6 +95,12 @@ Now, you can start fixing the notices:
9595
Once you fixed them all, the command ends with ``0`` (success) and you're
9696
done!
9797

98+
.. caution::
99+
100+
You will probably see many deprecations about incompatible native
101+
return types. See :ref:`Add Native Return Types <upgrading-native-return-types>`
102+
for guidance in fixing these deprecations.
103+
98104
.. sidebar:: Using the Weak Deprecations Mode
99105

100106
Sometimes, you can't fix all deprecations (e.g. something was deprecated
@@ -135,12 +141,12 @@ starting with ``symfony/`` to the new major version:
135141
"...": "...",
136142
137143
"require": {
138-
- "symfony/cache": "4.4.*",
139-
+ "symfony/cache": "5.0.*",
140-
- "symfony/config": "4.4.*",
141-
+ "symfony/config": "5.0.*",
142-
- "symfony/console": "4.4.*",
143-
+ "symfony/console": "5.0.*",
144+
- "symfony/cache": "5.4.*",
145+
+ "symfony/cache": "6.0.*",
146+
- "symfony/config": "5.4.*",
147+
+ "symfony/config": "6.0.*",
148+
- "symfony/console": "5.4.*",
149+
+ "symfony/console": "6.0.*",
144150
"...": "...",
145151
146152
"...": "A few libraries starting with
@@ -154,15 +160,15 @@ starting with ``symfony/`` to the new major version:
154160
155161
At the bottom of your ``composer.json`` file, in the ``extra`` block you can
156162
find a data setting for the Symfony version. Make sure to also upgrade
157-
this one. For instance, update it to ``5.0.*`` to upgrade to Symfony 5.0:
163+
this one. For instance, update it to ``6.0.*`` to upgrade to Symfony 6.0:
158164

159165
.. code-block:: diff
160166
161167
"extra": {
162168
"symfony": {
163169
"allow-contrib": false,
164170
- "require": "4.4.*"
165-
+ "require": "5.0.*"
171+
+ "require": "6.0.*"
166172
}
167173
}
168174
@@ -186,3 +192,89 @@ Next, use Composer to download new versions of the libraries:
186192
In some rare situations, the next major version *may* contain backwards-compatibility
187193
breaks. Make sure you read the ``UPGRADE-X.0.md`` (where X is the new major version)
188194
included in the Symfony repository for any BC break that you need to be aware of.
195+
196+
.. _upgrading-native-return-types:
197+
198+
Upgrading to Symfony 6: Add Native Return Types
199+
-----------------------------------------------
200+
201+
.. versionadded:: 5.4
202+
203+
The return type checking and fixing features were introduced in Symfony 5.4.
204+
205+
Symfony 6 will come with native PHP return types to (almost all) methods.
206+
It is important to add the native PHP return types to your classes first.
207+
Otherwise, you will get incompatible declaration errors when upgrading to
208+
Symfony 6.0.
209+
210+
When debug mode is enabled (typically in the dev and test environment),
211+
Symfony will trigger deprecations for every incompatible method
212+
declaration. For instance, the ``UserInterface::getRoles()`` method will
213+
have an ``array`` return type in Symfony 6. In order to be compatible, you
214+
must add the same return type to the ``getRoles()`` method while using
215+
Symfony 5.4.
216+
217+
To help with this, Symfony provides a ``DebugClassLoader`` that can add
218+
these native return types automatically for you. It can be configured using
219+
the ``SYMFONY_PATCH_TYPE_DECLARATIONS`` env var. The value of this env var
220+
is url-encoded (e.g. ``param1=value2&param2=value2``), the following
221+
parameters are available:
222+
223+
``force``
224+
Enables fixing return types, the value must be one of:
225+
226+
* ``2`` to add all possible return types (default, useful for applications)
227+
* ``1`` to add return types only to tests/final/internal/private methods
228+
* ``phpdoc`` to only add docblock annotations to the incompatible methods
229+
230+
If you're developing an application, you should set this to ``2``. The
231+
other values are useful for open source maintainers.
232+
233+
``php``
234+
The target version of PHP - e.g. ``7.1`` doesn't generate "object"
235+
types (which were introduced in 7.2). This defaults to the PHP version
236+
used when running the script.
237+
238+
``deprecations``
239+
Set to ``0`` to disable deprecations. Otherwise, a deprecation notice
240+
when a child class misses a return type while the parent declares an
241+
``@return`` annotation (defaults to ``1``).
242+
243+
If you're developing an application, use
244+
``SYMFONY_PATCH_TYPE_DECLARATIONS=force=2``. Symfony will only fix classes
245+
that are autoloaded. You can create a custom script that makes sure all
246+
classes in your project are loaded and fixed::
247+
248+
<?php
249+
// bin/fix-types.php
250+
251+
// required when using the PhpUnitBridge without "phpunit/phpunit"
252+
require __DIR__.'/../vendor/bin/.phpunit/phpunit/vendor/autoload.php';
253+
254+
$loader = require __DIR__.'/../vendor/autoload.php';
255+
256+
// enable Symfony's class loader
257+
Symfony\Component\ErrorHandler\DebugClassLoader::enable();
258+
259+
foreach ($loader->getClassMap() as $class => $file) {
260+
// don't fix classes in the "vendor/" directory
261+
if (str_contains(realpath($file), '/vendor/')) {
262+
continue;
263+
}
264+
265+
// loads the class
266+
class_exists($class);
267+
}
268+
269+
Symfony\Component\ErrorHandler\DebugClassLoader::checkClasses();
270+
271+
Now, you can use the Composer autoloader to find all classes and use this
272+
script to fix them:
273+
274+
.. code-block:: terminal
275+
276+
# "-o" is important! This forces Composer to find all classes
277+
$ composer dump-autoload -o
278+
279+
# run your custom script to add all types
280+
$ SYMFONY_PATCH_TYPE_DECLARATIONS=force=2 php bin/fix-types.php

0 commit comments

Comments
 (0)