-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Validator] remove support for generic constraint option handling #61063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 8.0
Are you sure you want to change the base?
Conversation
b8ba9e7
to
0c03730
Compare
@@ -32,7 +32,7 @@ public function __construct(mixed $options = null, ?array $groups = null, mixed | |||
throw new ConstraintDefinitionException(\sprintf('You can\'t redefine the "%s" option. Use the "%s::getConstraints()" method instead.', $this->getCompositeOption(), __CLASS__)); | |||
} | |||
|
|||
$this->constraints = $this->getConstraints($this->normalizeOptions($options)); | |||
$this->constraints = $this->getConstraints($options ?? []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need to deprecate using options in this Compound abstract class first, to be able to remove support properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, that's not a really useful feature anymore. I'm gonna send a PR for the deprecation targeting the 7.4
branch.
public function getDefaultOption(): ?string | ||
{ | ||
return 'constraints'; | ||
parent::__construct(null, $groups, $payload); | ||
} | ||
|
||
protected function getCompositeOption(): string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this method should also be removed, as it does not make sense anymore once the parent does not handle an array of options anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getCompositeOption()
is used independently from the $options
array in the constructor of the Composite
base class:
symfony/src/Symfony/Component/Validator/Constraints/Composite.php
Lines 64 to 115 in b49a855
foreach ((array) $this->getCompositeOption() as $option) { | |
/** @var Constraint[] $nestedConstraints */ | |
$nestedConstraints = $this->$option; | |
if (!\is_array($nestedConstraints)) { | |
$nestedConstraints = [$nestedConstraints]; | |
} | |
foreach ($nestedConstraints as $constraint) { | |
if (!$constraint instanceof Constraint) { | |
if (\is_object($constraint)) { | |
$constraint = get_debug_type($constraint); | |
} | |
throw new ConstraintDefinitionException(\sprintf('The value "%s" is not an instance of Constraint in constraint "%s".', $constraint, get_debug_type($this))); | |
} | |
if ($constraint instanceof Valid) { | |
throw new ConstraintDefinitionException(\sprintf('The constraint Valid cannot be nested inside constraint "%s". You can only declare the Valid constraint directly on a field or method.', get_debug_type($this))); | |
} | |
} | |
if (!isset(((array) $this)['groups'])) { | |
$mergedGroups = []; | |
foreach ($nestedConstraints as $constraint) { | |
foreach ($constraint->groups as $group) { | |
$mergedGroups[$group] = true; | |
} | |
} | |
// prevent empty composite constraint to have empty groups | |
$this->groups = array_keys($mergedGroups) ?: [self::DEFAULT_GROUP]; | |
$this->$option = $nestedConstraints; | |
continue; | |
} | |
foreach ($nestedConstraints as $constraint) { | |
if (isset(((array) $constraint)['groups'])) { | |
$excessGroups = array_diff($constraint->groups, $this->groups); | |
if (\count($excessGroups) > 0) { | |
throw new ConstraintDefinitionException(\sprintf('The group(s) "%s" passed to the constraint "%s" should also be passed to its containing constraint "%s".', implode('", "', $excessGroups), get_debug_type($constraint), get_debug_type($this))); | |
} | |
} else { | |
$constraint->groups = $this->groups; | |
} | |
} | |
$this->$option = $nestedConstraints; | |
} |
The important part when not being able anymore to use the options array is that the property/properties referenced with getCompositeOption()
are initialised in child class before the base constructor is called (that's part of the upgrade/changelog files).
src/Symfony/Component/Validator/Constraints/PasswordStrength.php
Outdated
Show resolved
Hide resolved
5428b30
to
e934b07
Compare
71a8b66
to
8397d33
Compare
17fcad8
to
9df2c71
Compare
This is ready to be reviewed. Status: Needs Review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, such nice diff! Thanks for working on this!
UPGRADE-8.0.md
Outdated
public $option2; | ||
|
||
#[HasNamedArguments] | ||
public function __construct($option1 = null, $option2 = null, ?array $groups = null, mixed $payload = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we propose CPP here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea, done
UPGRADE-8.0.md
Outdated
parent::__construct(null, $groups, $payload); | ||
} | ||
} | ||
``` | ||
* Remove `Bic::INVALID_BANK_CODE_ERROR` constant. This error code was not used in the Bic constraint validator anymore. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no trailing dot anywhere ;)
UPGRADE-8.0.md
Outdated
public $option1; | ||
public $option2; | ||
|
||
#[HasNamedArguments] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we still need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed no, fixed
@@ -41,11 +41,7 @@ class Date extends Constraint | |||
#[HasNamedArguments] | |||
public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) | |||
{ | |||
if (\is_array($options)) { | |||
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in cases like this, we still have a legacy $options argument
would it make sense to try to remove it, using some BC layer?
or should we throw to be sure passing anything else than null is forbidden?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot easily detect if options are passed when changing the order of arguments in 7.4 (we can try again in 8.1). But I added checks for $options
to be null
and throw otherwise.
$this->maxRatio = $maxRatio ?? $this->maxRatio; | ||
$this->minRatio = $minRatio ?? $this->minRatio; | ||
$this->minPixels = $minPixels ?? $this->minPixels; | ||
$this->maxPixels = $maxPixels ?? $this->maxPixels; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't really have a deprecation layer for this change, do we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do, all these properties could only have been non null
before if any of the properties were passed through the $options
array.
@@ -26,38 +25,16 @@ class Traverse extends Constraint | |||
public bool $traverse = true; | |||
|
|||
/** | |||
* @param bool|array<string,mixed>|null $traverse Whether to traverse the given object or not (defaults to true). Pass an associative array to configure the constraint's options (e.g. payload). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could be worth doing this on 7.4: docblocks should be only about the non-deprecated API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #61240
9df2c71
to
28644a9
Compare
28644a9
to
f70d261
Compare
opening the PR already even if some polishing needs to be done (and not all tests pass yet) to avoid someone else wasting their time on this topic