extractor icon indicating copy to clipboard operation
extractor copied to clipboard

Undefined array key "message" on ValidationAnnotation->extractFromConstraints() while use constructor promoted properties

Open SinErgy84 opened this issue 1 year ago • 1 comments

Hi!

I have discovered an error in the following class:

/var/www/vhosts/abc/vendor/php-translation/extractor/src/Visitor/Php/Symfony/ValidationAnnotation.php:112 Warning: Undefined array key "message"
#0 /var/www/vhosts/abc/vendor/php-translation/extractor/src/Visitor/Php/Symfony/ValidationAnnotation.php(91): Translation\Extractor\Visitor\Php\Symfony\ValidationAnnotation->extractFromConstraints()

while using constructor promoted properties instead a class var for $message.

During debugging I found following behaviour:

The reflection class of

    public function __construct($options = NULL, array $groups = NULL, $payload = NULL, public string $message = 'validatoren.adresse.ungueltig', public string $message_connection_error = 'validatoren.adresse.connection')
    {
        parent::__construct($options, $groups, $payload);
    }

does not provide message as default properties so they are missing while dumping

$ref = new \ReflectionClass($constraint);
$defaultValues = $ref->getDefaultProperties();

see: image

If you declare $message the old school way as class var:

public string $message = 'validatoren.adresse.ungueltig';

    public function __construct($options = NULL, array $groups = NULL, $payload = NULL, public string $message_connection_error = 'validatoren.adresse.connection')
    {
        parent::__construct($options, $groups, $payload);
    }

the default properties of the reflection class does contains message as array key and there is no warning during extraction:

image

A discussion about this behaviour as an potential php bug you can find here: https://bugs.php.net/bug.php?id=81386&edit=1

Not using constructor promoted properties as an PHP8 feature is not a solution for this issue.

SinErgy84 avatar Feb 19 '24 05:02 SinErgy84

Yeah, sounds like a problem related to the new PHP 8 syntax. It would be great to work around it IMO, do you want to give it a try? Even if that fixed in a newer version of PHP - it still would be a problem for legacy PHP versions, so a good workaround would be better.

bocharsky-bw avatar Feb 19 '24 13:02 bocharsky-bw