New in Symfony 2.8: YAML deprecations

Contributed by
Christian Flothmann
in #16201, #16285 and #16433.

The upcoming Symfony 3.1 version will introduce lots of new and useful features for the Yaml component. But before enjoying those features, in Symfony 2.8 we needed to deprecate some features to make Yaml files comply with the Yaml specification. Make sure to fix all these changes before upgrading your applications to Symfony 3.

Deprecated non-escaped \ in double-quoted strings

This deprecation is common when defining the classes of the services:

1
2
3
app.user_manager:
    class: "AppBundle\Manager\UserManager"
    # ...

The alternative is either remove the double quotes or escape the \ backslash doubling it:

1
2
3
4
5
6
app.user_manager:
    # remove double quotes...
    class: AppBundle\Manager\UserManager
    # ...or escape backslashes
    class: "AppBundle\\Manager\\UserManager"
    # ...

Deprecated usage of @ at the beginning of unquoted strings

This deprecation has a big impact because lots of services use the @service_id notation to define their dependencies:

1
2
3
app.user_manager:
    # ...
    arguments: [@router, @logger, @doctrine.orm.entity_manager]

According to Yaml specification, unquoted strings cannot start with @, so you must wrap these arguments with single or double quotes:

1
2
3
app.user_manager:
    # ...
    arguments: ['@router', '@logger', '@doctrine.orm.entity_manager']

Deprecated usage of reserved characters at the beginning of unquoted strings

These deprecations should have a low impact for Symfony developers because it's uncommon to use these characters as the beginning of unquoted strings:

1
2
3
app.user_manager:
    # ...
    arguments: [`string`, |string, >string]

The solution is to wrap these strings with single or double quotes:

1
2
3
app.user_manager:
    # ...
    arguments: ['`string`', '|string', '>string']

Deprecations are always a pain for developers and we try to avoid them as much as possible. But allowing to create Yaml files which don't respect the official specification is unquestionably wrong. The recent Symfony 3.0 release was the perfect moment to fix these problems.

Comments

Thanks for this article.

What about using single quotes to wrap full qualified class names ?
Does this also need to double backslashes ?
@Oliver no. Backslashes are not special chars in single quoted strings in Yaml. The only special char there is the single quote (which needs to be doubled to escape it)
Thank you.

What about parameters ? Since '@foo' must now be quoted, maybe it could be a good practice to quote '%parameters%' too ?
I like that.
So we better simply use single quotes almost everywhere we used to use double quotes?
Solution is simple: replace YAML with NEON https://ne-on.org. Strings can begin with @. And is much more expressive syntax for DI purposes, see this two examples: https://gist.github.com/dg/00ae93962614dfcb7d41

It is already used by some Symfony developers.
@Theirry: PHP Deprecated: Not quoting a scalar starting with the "%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0. in /home/ivanderberg/projects/symfony/src/Symfony/Component/Yaml/Inline.php on line 302

So I assume this will be as of 3.1
@Thierry @Iltar Yes, it's deprecated since #17809 (see https://github.com/symfony/symfony/pull/17809) which will be part of the 3.1 release.
Logic !
About the "Deprecated non-escaped \ in double-quoted strings", does it include class definitions under the argument key or this is just strictly for the class key?
Following YML standards is the way to go. Good move! Cheers!
Do I need to check all my YAML files ? Or the YAML deprecations send warnings/errors ?
@David Grudl NEON requires at least PHP 5.6 which makes it impossible to use as Symfony requires at least 5.5 and forcing a higher version is not possible.
@David Grudl YAML is also much more widely used and much better supported in many languages.

Comments are closed.

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