- Issue created by @godotislate
- Merge request !6873Issue #3425150: Add Constraint method return types. β (Closed) created by godotislate
- Status changed to Needs work
9 months ago 5:31pm 3 March 2024 Adding the return type declarations to the
validatedBy()
,getDefaultOption()
, andgetRequiredOptions()
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.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.- π¬π§United Kingdom longwave UK
I opened a duplicate before I found this; this is required for the Symfony 7 work so adding to π Symfony 7 compatibility Active - 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.
- π¬π§United Kingdom longwave UK
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
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 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 β .
- π¬π§United Kingdom longwave UK
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
- Status changed to Needs review
9 months ago 10:14pm 13 March 2024 - π¬π§United Kingdom longwave UK
Opened π Overwrite Symfony constraint errors in constructors instead of properties Active and π Remove $context properties from ConstraintValidators Active
I think this issue can land as-is in 11.x to fix the Symfony 7 return type issues.
- Status changed to RTBC
9 months ago 10:28pm 13 March 2024 - π¬π§United Kingdom longwave UK
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.
- Status changed to Fixed
9 months ago 9:55am 14 March 2024 Automatically closed - issue fixed for 2 weeks with no activity.