-
Notifications
You must be signed in to change notification settings - Fork 92
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
Narrow property types in validated objects #369
Comments
I've been doing sone work on this and based on the ticket description was pretty easy to get something working in two hours 🎉 @ondrejmirtes could you elaborate on what you mean with this:
Furthermore I'm wondering what should happen when assigning properties on a validated object. I guess that using the same narrowed type for Furthermore I have a technical question. Is there an easy method to parse the attributes arguments? For example |
I'd say that a validated object should stay validated, so it should only accept the narrowed type. I'd say it's a rare scenario anyway.
When you ask for attributes with You can see it has You can use this class to turn expressions into Types: https://apiref.phpstan.org/1.11.x/PHPStan.Reflection.InitializerExprTypeResolver.html There's a feature request for a nicer API: phpstan/phpstan#10443 |
@RobertMe Will you continue with this or can you share what you have created in those 2 hours? That would allow others to continue with it 😃 |
@ruudk sadly I didn't manage to really finish it. And in the end it ended up more like being a proof of concept, which wasn't really future proof. This as I just started parsing the attributes. But this has a lot of obvious limitations. For one it thus only supports the attributes, while Symfony also supports the constraints being defined in classic (PHPDoc) annotations, yaml / XML / ... config files, etc.. Furthermore I opted to not instantiate the attributes (to not depend on Symfony / the code) which is kinda bonkers to do on its own. As it requires a lot more of complex parsing (identifying options either by the named argument of the attribute or the position). At some later point I did find out that Symfony has some factory / registry / ... like class which can be used to get all the constraints for a class (/container?), which would be ideal to use for this as well. But it obviously would require some new "bootstrap" file (as is for example required for console application support, but also for phpstan-doctrine) to get the service from the container. But... the service is private (by default). But sadly I can't remember which service it was. And also didn't retry to build a solution based on this. |
Maybe an easier first step would be to handle array validation such as: $filters = [
'role' => request->query->get('role'),
];
$constraint = new Assert\Collection([
'role' => new Assert\Optional(new Assert\Type('string')),
]);
assert(count($this->validator->validate($filters, $constraint)) === 0);
// Here $filters has type array{role: ?string} |
/cc @DaveLiddament
When an object is validated with Symfony Validator, we could narrow down its property types based on constraints.
My idea is:
class ValidatedObjectType extends ObjectType
ObjectType::getProperty()
, read the validator attributes, and narrow the property type in reflection returned from parent. We'd probably createnew ValidatedObjectPropertyReflection
, inject the original object, and only override the type getters.ValidatedObjectType::isSuperTypeOf
andacceptsWithReason
answer.symfony-validated<UserDTO>
and return ValidatedObjectType thanks to https://phpstan.org/developing-extensions/custom-phpdoc-types.The only missing part is to narrow the object type to a validated one after calling the validator:
This interface isn't really static analysis friendly. It'd be a job for a type-specifying extension to do that, but something like
$validator->isValid($author)
would be much easier to work with.The text was updated successfully, but these errors were encountered: