-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Form] Add a TypeGuesser that use typed property reflection #47450
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.1
Are you sure you want to change the base?
Conversation
e9ab555 to
448b249
Compare
|
Hey! I think @alamirault has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
| ['string', new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE)], | ||
| ['nullable', new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE)], | ||
| ['suit', new TypeGuess(EnumType::class, ['class' => Suit::class], Guess::MEDIUM_CONFIDENCE)], | ||
| ['date', new TypeGuess(DateTimeType::class, ['input' => 'datetime_immutable'], Guess::LOW_CONFIDENCE)], |
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 should also add a test case for a type where the guess would be 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.
I've added one case with a property typed with an unknown class, and one with an untyped property.
448b249 to
884d0a3
Compare
src/Symfony/Component/Form/Tests/Extension/Core/ReflectionTypeGuesserTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Form/Tests/Extension/Core/ReflectionTypeGuesserTest.php
Outdated
Show resolved
Hide resolved
5d2411a to
ac8a91d
Compare
| { | ||
| public function guessType(string $class, string $property): ?TypeGuess | ||
| { | ||
| $type = $this->getReflectionType($class, $property); |
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.
Shouldn't we use PropertyInfo instead of using reflection directly? This will allow supporting Reflection but also PHPDoc/PHPStan types as well as other metadata sources.
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 thought about using PropertyInfo but decided not to because I wanted the guesser to be the simplest possible and not depend on another component (this could be important when not using the full stack framework).
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 think this should be https://symfony.com/doc/current/components/type_info.html rather than the PropertyInfo today but it still adds new deps (symfony/type-info + phpstan/phpdoc-parser).
Versus this current PR, I see it essentially interesting to handle the case of EnumType & TimezoneType "multiple" option. https://github.com/a2lix/AutoFormBundle/blob/draft/src/Form/TypeGuesser/TypeInfoTypeGuesser.php#L45-L46
3f14eac to
fbf29b9
Compare
fbf29b9 to
a1b6252
Compare
|
This feature is interesting for DTO object. See if, in this same ReflectionTypeGuesser, we could add some low guesses about the $property namming too:
? |
|
Please don do guessing based on the property name. This would be far too fragile and unmaintainable:
The only features we have in Symfony that relies on property/parameter names are the named autowiring and the named bindings. For named autowiring, we already have a better alternative thanks to the |
When using form on objects that are not doctrine entities, you have to explicitly define all types that are used on forms.
This PR adds a type guesser that uses reflection information from typed properties to guess form types.