-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Standardize constraint validators #28645
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
Conversation
that should be solved by #27917 right, here we just move code around. |
If it gets merged, yeah |
Don't get me wrong, but I believe that the method names that you have are not descriptive on what the methods are actually doing.
|
@@ -9,12 +9,13 @@ | |||
* file that was distributed with this source code. | |||
*/ | |||
|
|||
use Symfony\Component\Validator\Constraints\Bic; |
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.
The use statement should be below the namespace
@gmponos thanks :) i did it a bit quick to propose the idea... we can talk about naming any time, but i think your suggestion is better yes. i like |
* | ||
* @throws UnexpectedTypeException | ||
*/ | ||
final protected static function testConstraint(Constraint $constraint, string $expectedClass) |
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 really need this? I feel like this removes some info for static analysis + increases the stack traces of exception for little practical benefit, don't you think?
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.
I ran a little investigation:
- PHPStorm doesn't understand this
- Psalm does https://getpsalm.org/r/acd72ab0ab cc @muglug
- PHPStan doesn't https://phpstan.org/r/be6b68a728fc1d24fb27069640f46701 cc @ondrejmirtes
Anyway Validator is full of this piece of code, so I think it's worth it. SA tools which don't support it is problem of the tool itself, not the code. And we can help them by specifying @param
alongside @inheritDoc
(works for PHPStorm and Psalm, PHPStan is stubborn like usual).
Extraction of common pieces of code is important for keeping consistency. Also, if this was done in the first place, there would be no need to manually touch so many places in #27917
But, this should be renamed. testConstraint
is odd one. assertInstanceOf
?
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.
That's not really a common piece of logic - that's an "if"...
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.
I didn't mention "logic", I mentioned "piece of code". Anyway "if" contains some logic. This "if" is in almost all validators. Devs keep producing highly repetitive code instead of extracting it to function. This is like no. 1 rule of programming. Programs are good at repeating, not humans.
And here is another advantage of having such thing there in the first place - if it was already there, all custom validators using such function would be automatically compatible with #27917. Since it's not there (because such function is too simple right?), every custom constraint will now have to change this piece of code within it. But it's too late for #27917. Maybe another time there will be need to change the message or exception type.
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.
I get that, but I don't agree: designing the code to make such changes easy is not a target at all to me. Software architecture should be built around things that change, not those that don't (#27917 is not a change that should be centralized to me.)
Not sure how much my word counts on this, but personally I don't like this. It's the mentality behind of it that I don't like. I believe that it encourages developers to add more special cases inside these functions. What I mean is that I already imagine developers preparing their special cases with extra The Never the less if this gets approved I believe that the results returned by |
@gmponos i see you're point, im not too happy with the current approach myself as well :)
That's intended, i.e. the current behavior. But i agree we might not want to standardize it like this, as not every constraint has to rely on it. FYI in core i'd like to get rid of this rule eventually, see #28611 I'll give this some more thought for now, and see how #27917 goes first. |
Shall we close here? |
yep :) ill do a consistency round some other time per constraint |
there are subtle inconsistencies between constraint validators, esp. related to string values. I think we should tackle it, by taking away the boring stuff.
This would help custom constraints, as well as core to e.g. deprecate empty string handling in a unified way (#28611).