Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jvasseur
Copy link
Contributor

@jvasseur jvasseur commented Sep 1, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR

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.

@carsonbot carsonbot added this to the 6.2 milestone Sep 1, 2022
@jvasseur jvasseur changed the title [Fom] Add a TypeGuesser that use typed property reflection [Form] Add a TypeGuesser that use typed property reflection Sep 1, 2022
@jvasseur jvasseur force-pushed the reflection-type-guess branch from e9ab555 to 448b249 Compare September 1, 2022 14:16
@carsonbot
Copy link

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)],
Copy link
Member

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

Copy link
Contributor Author

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.

@jvasseur jvasseur force-pushed the reflection-type-guess branch from 448b249 to 884d0a3 Compare September 5, 2022 12:40
@jvasseur jvasseur force-pushed the reflection-type-guess branch 2 times, most recently from 5d2411a to ac8a91d Compare September 8, 2022 10:28
{
public function guessType(string $class, string $property): ?TypeGuess
{
$type = $this->getReflectionType($class, $property);
Copy link
Member

@dunglas dunglas Sep 9, 2022

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.

Copy link
Contributor Author

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).

Copy link
Contributor

@webda2l webda2l Oct 2, 2025

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

@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@jvasseur jvasseur force-pushed the reflection-type-guess branch 2 times, most recently from 3f14eac to fbf29b9 Compare November 9, 2022 09:28
@jvasseur jvasseur force-pushed the reflection-type-guess branch from fbf29b9 to a1b6252 Compare November 9, 2022 10:42
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
@webda2l
Copy link
Contributor

webda2l commented Oct 2, 2025

This feature is interesting for DTO object.

See if, in this same ReflectionTypeGuesser, we could add some low guesses about the $property namming too:

  • price/amount => MoneyType
  • tel/phoneNumber => TelType
  • percent => PercentType
  • etc

?

@stof
Copy link
Member

stof commented Oct 2, 2025

Please don do guessing based on the property name. This would be far too fragile and unmaintainable:

  • devs writing their property names in other languages will either have no guessing, have wrong guessing or ask for i18n support in the guessing logic (introducing the risk of wrong guessing if we don't ask for the language used for guessing but guess in all languages at once)
  • performing a renaming of the property would suddenly have side effects that are not known by IDEs or other refactoring tools, making the usage of such tool risky

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 #[Target] attribute, which does not suffer from that issue as it uses a feature dedicated to providing metadata.

@nicolas-grekas nicolas-grekas modified the milestones: 7.4, 8.1 Nov 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants