-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RFC][Validator] Remove constraint construction from associative arrays #51393
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
Comments
I like the idea to reduce the complexity in the code 👍 |
IIRC, the Yaml and XML loaders still rely on passing all options as array in the constructor. So we would need to have an opt-in for the new behavior for each constraints. Note that this is similar to what doctrine/annotations did in the past with their |
There is already a way #45072 :) |
hmm, apparently, I missed the introduction of this |
There's also the possibility of programmatically creating new instances of constraint classes, e.g. when declaring a form type. Migrating a large codebase that has lots of those constructor calls might be very painful, unless we have a way of automating that. |
Migrating explicit instantiations is something that could be implemented in Rector to automate the migration (the dumb migration is to spread the array, but we can provide better output for inline arrays where the keys are known statically) |
However, the first thing (which could even be done in 6.4) would be to ensure that all core constraints apply the attribute so that XML and Yaml loaders use the new way instead of the old one. |
I have a WIP branch locally where I migrate the test suite to call constraint constructors with named arguments consitently. A very painful task. 🙉 |
Thank you for this suggestion. |
@derrabus what is the state of your WIP branch ? |
It's WIP. With not so much P at the moment. 😓 |
I gave it a try here: #54744 |
Description
From @nicolas-grekas' comment: #51381 (comment)
Currently, we have two ways of constructing our constraints: By passing all properties via named arguments or by passing one big associative array. The former is needed so we have a nice completion when using the constraint as attribute while the latter is still supported for historical reasons: Named arguments did not exist in PHP 5 and the array parameter was the ways Doctrine Annotations used to work back in the days.
The example has been taken from https://symfony.com/doc/current/validation.html#constraint-configuration.
The code we maintain in each constraint constructor to allow both ways is pretty complex and it would make the maintenance of those classes a lot easier if we switched to named arguments completely. However, the migration path for downstream projects might be a bit bumpy.
The text was updated successfully, but these errors were encountered: