Skip to content

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

@SinErgy84

Description

@SinErgy84

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions