Problem/Motivation

In #3420990: Convert Constraint plugin discovery to attributes, the conversion from using annotations to attributes for Constraint plugins led to tests failing because Symfony deprecation warnings like these:

Remaining self deprecation notices (11)

1x: Method "Symfony\Component\Validator\Constraint::getDefaultOption()"
might add "?string" as a native return type declaration in the future. Do
the same in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\ValidKeysConstraint"
now to avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData

1x: Method "Symfony\Component\Validator\Constraint::getRequiredOptions()"
might add "array" as a native return type declaration in the future. Do the
same in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\ValidKeysConstraint"
now to avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData

1x: Method "Symfony\Component\Validator\Constraint::validatedBy()" might
add "string" as a native return type declaration in the future. Do the same
in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\UuidConstraint" now to
avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData

1x: Method "Symfony\Component\Validator\Constraint::validatedBy()" might
add "string" as a native return type declaration in the future. Do the same
in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\RegexConstraint" now
to avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData

1x: Method "Symfony\Component\Validator\Constraint::validatedBy()" might
add "string" as a native return type declaration in the future. Do the same
in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\LengthConstraint" now
to avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData

1x: Method "Symfony\Component\Validator\Constraint::getDefaultOption()"
might add "?string" as a native return type declaration in the future. Do
the same in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\EntityBundleExistsConstraint"
now to avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData

1x: Method "Symfony\Component\Validator\Constraint::getRequiredOptions()"
might add "array" as a native return type declaration in the future. Do the
same in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\EntityBundleExistsConstraint"
now to avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: Method "Symfony\Component\Validator\Constraint::validatedBy()" might
add "string" as a native return type declaration in the future. Do the same
in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\EmailConstraint" now
to avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: Method "Symfony\Component\Validator\Constraint::validatedBy()" might
add "string" as a native return type declaration in the future. Do the same
in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\CountConstraint" now
to avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: Method "Symfony\Component\Validator\Constraint::getDefaultOption()"
might add "?string" as a native return type declaration in the future. Do
the same in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\ComplexDataConstraint"
now to avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: Method "Symfony\Component\Validator\Constraint::getRequiredOptions()"
might add "array" as a native return type declaration in the future. Do the
same in child class
"Drupal\Core\Validation\Plugin\Validation\Constraint\ComplexDataConstraint"
now to avoid errors or add an explicit @return annotation to suppress this
message.
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
Remaining direct deprecation notices (10)
1x: The "Symfony\Component\Validator\Constraints\Regex::$message"
property is considered final. You should not override it in
"Drupal\Core\Validation\Plugin\Validation\Constraint\RegexConstraint".
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: The "Symfony\Component\Validator\Constraints\Length::$maxMessage"
property is considered final. You should not override it in
"Drupal\Core\Validation\Plugin\Validation\Constraint\LengthConstraint".
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: The "Symfony\Component\Validator\Constraints\Length::$minMessage"
property is considered final. You should not override it in
"Drupal\Core\Validation\Plugin\Validation\Constraint\LengthConstraint".
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: The "Symfony\Component\Validator\Constraints\Length::$exactMessage"
property is considered final. You should not override it in
"Drupal\Core\Validation\Plugin\Validation\Constraint\LengthConstraint".
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: The "Symfony\Component\Validator\Constraints\Count::$minMessage"
property is considered final. You should not override it in
"Drupal\Core\Validation\Plugin\Validation\Constraint\CountConstraint".
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: The "Symfony\Component\Validator\Constraints\Count::$maxMessage"
property is considered final. You should not override it in
"Drupal\Core\Validation\Plugin\Validation\Constraint\CountConstraint".
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: The "Symfony\Component\Validator\Constraints\Count::$exactMessage"
property is considered final. You should not override it in
"Drupal\Core\Validation\Plugin\Validation\Constraint\CountConstraint".
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: The "Symfony\Component\Validator\Constraints\Choice::$strict"
property is considered final. You should not override it in
"Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraint".
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: The "Symfony\Component\Validator\Constraints\Choice::$minMessage"
property is considered final. You should not override it in
"Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraint".
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData
1x: The "Symfony\Component\Validator\Constraints\Choice::$maxMessage"
property is considered final. You should not override it in
"Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraint".
1x in
RecursiveContextualValidatorTest::testBasicValidateWithoutConstraints from
Drupal\Tests\Core\TypedData

These warnings were either suppressed or addressed with @return annotations. This is a follow up to address the deprecations:

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3425150

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

godotislate created an issue. See original summary.

godotislate’s picture

Status: Active » Needs work

Adding the return type declarations to the validatedBy(), getDefaultOption(), and getRequiredOptions() methods is pretty straightforward.

Addressing overriding Symfony component class properties is a bit more complicated. In Symfony 6.1, all untyped Symfony\ namespace properties were made implicitly final: https://github.com/symfony/symfony/pull/45360. The reasoning behind this was to prepare for untyped properties to be typed in Symfony 7.0: https://github.com/symfony/symfony/issues/43600. (Note that Symfony's stated BC policy is to support overriding public and protected class properties: https://symfony.com/doc/current/contributing/code/bc.html#using-our-classes)

It looks like the Constraint properties are typed in 7.0 (example: https://github.com/symfony/symfony/blob/e770fba7c28750f8a11c8943fc85a279...), so once core 11.x is on Symfony 7, the overridden properties can be typed to match their parent Constraint classes. Don't know if it's worthy doing to change core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/RangeConstraint.php from #3276196: The "Symfony\Component\Validator\Constraints\Range::$minMessage" property is considered final back to using overridden properties with typehints.

longwave’s picture

I opened a duplicate before I found this; this is required for the Symfony 7 work so adding to #3394694: [Meta] Symfony 7 compatibility - thanks for the work so far!

As we are adding return types I think this can only be done in 11.x. The @return declarations will (in theory) notify any downstream users that they have to add types themselves before the next major version.

longwave’s picture

As per the PHPStan findings at https://git.drupalcode.org/issue/drupal-3394694/-/jobs/990299 let's fix the following here too:

InternalViolation
AllowedValuesConstraint
CountConstraint
LengthConstraint

CommentNameConstraintValidator
EntityTestCompositeConstraintValidator
EntityTestEntityLevelValidator
godotislate’s picture

Does this actually need to be split up into two issues? The first being to add the return types in 11.x before going to Symfony 7, and the second to add the typehints to the overridden Constraint class properties? Apparently, because of PHP type invariance, the overridden properties can't be typed before Symfony 7, and they need to be typed once Symfony 7 is in. Alternatively there could be an interim step (or just continue doing it that way going forward) to stop overriding the properties altogether and set them in the constructor a la #3276196: The "Symfony\Component\Validator\Constraints\Range::$minMessage" property is considered final.

longwave’s picture

I think actually three issues:

  • this one to deal with return types (11.x only in preparation for Symfony 7)
  • another one for the constraint properties which we should probably set in the constructor
  • a third one to remove the ConstraintValidator $context properties, which already exist in the parent class
longwave’s picture

Status: Needs work » Needs review
longwave’s picture

Status: Needs review » Reviewed & tested by the community

InternalViolation is a shim that can be removed in PHP 8.3, will open yet another issue for that.

This one is ready to land in 11.x only I think.

longwave’s picture

Issue tags: +Major version only

  • catch committed 6663e648 on 11.x
    Issue #3425150 by godotislate, longwave: Symfony deprecations in...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.