Improved PropertyAccess Error Messages

Pierre du Plessis
Contributed by Pierre du Plessis in #31194

Great error messages are essential to avoid frustration when using any piece of software. In Symfony 4.4 we improved the error messages generated by PropertyAccess when trying to find a writable property.

For example, when you already defined the adder/remover methods but their signature was wrong (e.g. addFoo()/removeFoo()), this was the error displayed:

1
2
Neither the property "foo" nor one of the methods "addFoo()/removeFoo()",
"setFoo()", "foo()", "__set()" or "__call()" exist and have public access.

The error message is now much more clear:

1
The method "removeFoo()" requires at least "1" parameter, "0" found.

This is just one example, but we improved lots of other error messages in this component.

Support *:only-of-type CSS selectors

Jakub Zalas
Contributed by Jakub Zalas in #33861

The CssSelector component, which is used indirectly in functional tests to select items based on CSS selectors, supported the :only-of-type pseudo-class when specifying an HTML element (e.g. p:only-of-type selects a child only if it's the only paragraph inside an element).

In Symfony 4.4 we also added support for *:only-of-type (or just :only-of-type) which selects single child elements. If an element has 2 <p> and 1 <a> as children, only the <a> is selected, but if it has 1 <p> and 1 <a>, both are selected because they are the only ones of their types.

Use properties as Range values

Jérôme Parmentier
Contributed by Jérôme Parmentier in #31511

The Range constraint validates that a given number or DateTime object is between some minimum and maximum. In Symfony 4.4, thanks to the new maxPropertyPath and minPropertyPath options, you can use the values stored in some properties as the values of those minimum/maximum:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// src/Entity/Event.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Event
{
    /**
     * @Assert\Range(
     *      minPropertyPath = "startsAt",
     *      maxPropertyPath = "endsAt"
     * )
     */
    private $registrationDate;

    private $startsAt;
    private $endsAt;

    // ...
}

Allow to sort translation messages

Dāvis Zālītis
Contributed by Dāvis Zālītis in #33117

The translation:update command helps you extract the translatable contents from Twig templates and PHP controllers. In Symfony 4.4, we added a new --sort option to sort the list of messages alphabetically (asc or desc):

1
$ php bin/console translation:update --force --sort=asc fr

Consider empty strings not valid

Maxime Steinhausser
Contributed by Maxime Steinhausser in #31528

Should an empty string be considered valid or invalid in the Length constraint? The answer is not as obvious as it may seem at first sight. In Symfony 4.4, we tried to improve and simplify this behavior with a new allowEmptyString option.

In Symfony 4.4, the new option is true by default, to keep compatibility with the existing behavior. However, it's deprecated to not define a value for this option explicitly, so you'll see several deprecation logs related to this. We had to deprecate not setting it so we could change its default value to false in Symfony 5.0.

Added .gitattributes to remove tests

Tobias Nyholm
Contributed by Tobias Nyholm in #33579

The special .gitattributes file allows to define attributes to different paths (directories or files) in the Git project (e.g. to change end-of-line characters or define how diff should be calculated).

One of those attributes is export-ignore, which makes the file or directory to be excluded when generating the Git archives. These archives are used when installing dependencies with composer update --prefer-dist (installing them with composer update --prefer-source doesn't exclude anything).

Given that Symfony components include a ton of test files, and given that most people don't need tests in production, some people from the community suggested to remove all test files with export-ignore. These discussions started in 2014 and have been a recurring demand since then. In Symfony 4.4, we finally decided to add these .gitattributes files.

The result is that when installing dependencies with --prefer-dist, you'll no longer get Symfony tests, which can save up to 50% of installation size (several megabytes per installation). This will help you make much smaller packages when deploying with Docker or on serverless.

Published in #Living on the edge