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
77Every 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,
3030classes, etc) code still works, but is marked as *deprecated *, indicating that
3131it 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
3434functionality are removed. So, as long as you've updated your code to stop
3535using 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
9696done!
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
156162find 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,126 @@ Next, use Composer to download new versions of the libraries:
186192In some rare situations, the next major version *may * contain backwards-compatibility
187193breaks. Make sure you read the ``UPGRADE-X.0.md `` (where X is the new major version)
188194included 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+ In PHP, if the parent has a return type declaration, any class implementing
207+ or overriding the method must have the return type as well. However, you
208+ can add a return type before the parent adds one. This means that it is
209+ important to add the native PHP return types to your classes before
210+ upgrading to Symfony 6.0. Otherwise, you will get incompatible declaration
211+ errors.
212+
213+ When debug mode is enabled (typically in the dev and test environment),
214+ Symfony will trigger deprecations for every incompatible method
215+ declarations based on PHPDocs. For instance, the ``UserInterface::getRoles() ``
216+ method will have an ``array `` return type in Symfony 6. In Symfony 5.4, you
217+ will get a deprecation notice about this and you must add the return type
218+ declaration to your ``getRoles() `` method.4.
219+
220+ To help with this, Symfony provides a script that can add these return
221+ types automatically for you. Make sure you installed the ``symfony/error-handler ``
222+ component. When installed, generate a complete class map using Composer and
223+ run the script to iterate over the class map and fix any incompatible
224+ method:
225+
226+ .. code-block :: terminal
227+
228+ # "-o" is important! This forces Composer to find all classes
229+ $ composer dump-autoload -o
230+
231+ # patch all incompatible method declarations
232+ $ ./vendor/bin/patch-type-declarations
233+
234+ .. tip ::
235+
236+ This feature is not limited to Symfony packages. It will also help you
237+ add types and prepare for other dependencies in your project.
238+
239+ The behavior of this script can be modified using the ``SYMFONY_PATCH_TYPE_DECLARATIONS ``
240+ env var. The value of this env var is url-encoded (e.g.
241+ ``param1=value2¶m2=value2 ``), the following parameters are available:
242+
243+ ``force ``
244+ Enables fixing return types, the value must be one of:
245+
246+ * ``2 `` to add all possible return types (default, recommended for applications);
247+ * ``1 `` to add return types only to tests, final, internal or private methods;
248+ * ``phpdoc `` to only add ``@return `` docblock annotations to the incompatible
249+ methods, or ``#[\ReturnTypeWillChange] `` if it's triggered by the PHP engine.
250+
251+ ``php ``
252+ The target version of PHP - e.g. ``7.1 `` doesn't generate "object"
253+ types (which were introduced in 7.2). This defaults to the PHP version
254+ used when running the script.
255+
256+ ``deprecations ``
257+ Set to ``0 `` to disable deprecations. Otherwise, a deprecation notice
258+ when a child class misses a return type while the parent declares an
259+ ``@return `` annotation (defaults to ``1 ``).
260+
261+ If there are specific files that should be ignored, you can set the
262+ ``SYMFONY_PATCH_TYPE_EXCLUDE `` env var to a regex. This regex will be
263+ matched to the full path to the class and each matching path will be
264+ ignored (e.g. ``SYMFONY_PATCH_TYPE_EXCLUDE="/tests\/Fixtures\//" ``).
265+ Classes in the ``vendor/ `` directory are always ignored.
266+
267+ .. tip ::
268+
269+ The script does not care about code style. Run your code style fixer,
270+ or `PHP CS Fixer `_ with the ``phpdoc_trim_consecutive_blank_line_separation ``,
271+ ``no_superfluous_phpdoc_tags `` and ``ordered_imports `` rules, after
272+ patching the types.
273+
274+ .. _patching-types-for-open-source-maintainers :
275+
276+ .. sidebar :: Patching Types for Open Source Maintainers
277+
278+ Open source bundles and packages need to be more cautious with adding
279+ return types, as adding a return type forces all users extending the
280+ class to add the return type as well. The recommended approach is to
281+ use a 2 step process:
282+
283+ 1. First, create a minor release (i.e. without backwards compatibility
284+ breaks) where you add types that can be safely introduced and add
285+ ``@return `` PHPDoc to all other methods:
286+
287+ .. code-block :: terminal
288+
289+ # Add type declarations to all internal, final, tests and private methods.
290+ # Update the "php" parameter to match your minimum required PHP version
291+ $ SYMFONY_DEPRECATIONS_HELPER="force=1&php=7.4" ./vendor/bin/patch-type-declarations
292+
293+ # Add PHPDoc to the leftover public and protected methods
294+ $ SYMFONY_DEPRECATIONS_HELPER="force=phpdoc&php=7.4" ./vendor/bin/patch-type-declarations
295+
296+ After running the scripts, check your classes and add more ``@return ``
297+ PHPDoc where they are missing. The deprecations and patch script
298+ work purely based on the PHPDoc information. Users of this release
299+ will get deprecation notices telling them to add the missing return
300+ types from your package to their code.
301+
302+ If you didn't need any PHPDoc and all your method declarations are
303+ already compatible with Symfony, you can safely allow ``^6.0 `` for
304+ the Symfony dependencies. Otherwise, you have to continue with (2).
305+
306+ 2. Create a new major release (i.e. *with * backwards compatibility
307+ breaks) where you add types to all methods:
308+
309+ .. code-block :: terminal
310+
311+ # Update the "php" parameter to match your minimum required PHP version
312+ $ SYMFONY_DEPRECATIONS_HELPER="force=2&php=7.4" ./vendor/bin/patch-type-declarations
313+
314+ As this release adds type declarations, you can safely allow
315+ ``^6.0 `` for the Symfony dependencies.
316+
317+ .. _`PHP CS Fixer` : https://github.com/friendsofphp/php-cs-fixer
0 commit comments