Skip to content

[Form] Add form type guesser for EnumType #61297

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\Glob;
use Symfony\Component\Form\EnumFormTypeGuesser;
use Symfony\Component\Form\Extension\HtmlSanitizer\Type\TextTypeHtmlSanitizerExtension;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormTypeExtensionInterface;
Expand Down Expand Up @@ -880,6 +881,10 @@ private function registerFormConfiguration(array $config, ContainerBuilder $cont
{
$loader->load('form.php');

if (!class_exists(EnumFormTypeGuesser::class)) {
$container->removeDefinition('form.type_guesser.enum_type');
}

if (null === $config['form']['csrf_protection']['enabled']) {
$this->writeConfigEnabled('form.csrf_protection', $config['csrf_protection']['enabled'], $config['form']['csrf_protection']);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator;
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
use Symfony\Component\Form\EnumFormTypeGuesser;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\ColorType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
Expand Down Expand Up @@ -76,6 +77,9 @@
->args([service('validator.mapping.class_metadata_factory')])
->tag('form.type_guesser')

->set('form.type_guesser.enum_type', EnumFormTypeGuesser::class)
->tag('form.type_guesser')

->alias('form.property_accessor', 'property_accessor')

->set('form.choice_list_factory.default', DefaultChoiceListFactory::class)
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `input=date_point` to `DateTimeType`, `DateType` and `TimeType`
* Add support for guessing form type of enum properties

7.3
---
Expand Down
73 changes: 73 additions & 0 deletions src/Symfony/Component/Form/EnumFormTypeGuesser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form;

use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;

final class EnumFormTypeGuesser implements FormTypeGuesserInterface
{
/**
* @var array<string, array<string, \ReflectionNamedType|false>>
*/
private array $cache = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private array $cache = [];
/**
* @var (\ReflectionNamedType|false)[]
*/
private array $cache = [];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggested type is not correct, I used the correct one (nested array)


public function guessType(string $class, string $property): ?TypeGuess
{
if (!($propertyType = $this->getPropertyType($class, $property))) {
return null;
}

return new TypeGuess(EnumType::class, ['class' => $propertyType->getName()], Guess::HIGH_CONFIDENCE);
}

public function guessRequired(string $class, string $property): ?ValueGuess
{
if (!($propertyType = $this->getPropertyType($class, $property))) {
return null;
}

return new ValueGuess(!$propertyType->allowsNull(), Guess::HIGH_CONFIDENCE);
}

public function guessMaxLength(string $class, string $property): ?ValueGuess
{
return null;
}

public function guessPattern(string $class, string $property): ?ValueGuess
{
return null;
}

private function getPropertyType(string $class, string $property): \ReflectionNamedType|false
{
if (isset($this->cache[$class][$property])) {
return $this->cache[$class][$property];
}

try {
$propertyReflection = new \ReflectionProperty($class, $property);
} catch (\ReflectionException) {
return $this->cache[$class][$property] = false;
}

$type = $propertyReflection->getType();
if (!$type instanceof \ReflectionNamedType || !enum_exists($type->getName())) {
$type = false;
}

return $this->cache[$class][$property] = $type;
}
}
208 changes: 208 additions & 0 deletions src/Symfony/Component/Form/Tests/EnumFormTypeGuesserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\EnumFormTypeGuesser;
use Symfony\Component\Form\Extension\Core\Type\EnumType as FormEnumType;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;
use Symfony\Component\Form\Tests\Fixtures\BackedEnumFormTypeGuesserCaseEnum;
use Symfony\Component\Form\Tests\Fixtures\EnumFormTypeGuesserCase;
use Symfony\Component\Form\Tests\Fixtures\EnumFormTypeGuesserCaseEnum;

class EnumFormTypeGuesserTest extends TestCase
{
#[DataProvider('provideGuessTypeCases')]
public function testGuessType(?TypeGuess $expectedTypeGuess, string $class, string $property)
{
$typeGuesser = new EnumFormTypeGuesser();

$typeGuess = $typeGuesser->guessType($class, $property);

self::assertEquals($expectedTypeGuess, $typeGuess);
}

#[DataProvider('provideGuessRequiredCases')]
public function testGuessRequired(?ValueGuess $expectedValueGuess, string $class, string $property)
{
$typeGuesser = new EnumFormTypeGuesser();

$valueGuess = $typeGuesser->guessRequired($class, $property);

self::assertEquals($expectedValueGuess, $valueGuess);
}

public static function provideGuessTypeCases(): iterable
{
yield 'Undefined class' => [
null,
'UndefinedClass',
'undefinedProperty',
];

yield 'Undefined property' => [
null,
EnumFormTypeGuesserCase::class,
'undefinedProperty',
];

yield 'Undefined enum' => [
null,
EnumFormTypeGuesserCase::class,
'undefinedEnum',
];

yield 'Non-enum property' => [
null,
EnumFormTypeGuesserCase::class,
'string',
];

yield 'Enum property' => [
new TypeGuess(
FormEnumType::class,
[
'class' => EnumFormTypeGuesserCaseEnum::class,
],
Guess::HIGH_CONFIDENCE,
),
EnumFormTypeGuesserCase::class,
'enum',
];

yield 'Nullable enum property' => [
new TypeGuess(
FormEnumType::class,
[
'class' => EnumFormTypeGuesserCaseEnum::class,
],
Guess::HIGH_CONFIDENCE,
),
EnumFormTypeGuesserCase::class,
'nullableEnum',
];

yield 'Backed enum property' => [
new TypeGuess(
FormEnumType::class,
[
'class' => BackedEnumFormTypeGuesserCaseEnum::class,
],
Guess::HIGH_CONFIDENCE,
),
EnumFormTypeGuesserCase::class,
'backedEnum',
];

yield 'Nullable backed enum property' => [
new TypeGuess(
FormEnumType::class,
[
'class' => BackedEnumFormTypeGuesserCaseEnum::class,
],
Guess::HIGH_CONFIDENCE,
),
EnumFormTypeGuesserCase::class,
'nullableBackedEnum',
];

yield 'Enum union property' => [
null,
EnumFormTypeGuesserCase::class,
'enumUnion',
];

yield 'Enum intersection property' => [
null,
EnumFormTypeGuesserCase::class,
'enumIntersection',
];
}

public static function provideGuessRequiredCases(): iterable
{
yield 'Unknown class' => [
null,
'UndefinedClass',
'undefinedProperty',
];

yield 'Unknown property' => [
null,
EnumFormTypeGuesserCase::class,
'undefinedProperty',
];

yield 'Undefined enum' => [
null,
EnumFormTypeGuesserCase::class,
'undefinedEnum',
];

yield 'Non-enum property' => [
null,
EnumFormTypeGuesserCase::class,
'string',
];

yield 'Enum property' => [
new ValueGuess(
true,
Guess::HIGH_CONFIDENCE,
),
EnumFormTypeGuesserCase::class,
'enum',
];

yield 'Nullable enum property' => [
new ValueGuess(
false,
Guess::HIGH_CONFIDENCE,
),
EnumFormTypeGuesserCase::class,
'nullableEnum',
];

yield 'Backed enum property' => [
new ValueGuess(
true,
Guess::HIGH_CONFIDENCE,
),
EnumFormTypeGuesserCase::class,
'backedEnum',
];

yield 'Nullable backed enum property' => [
new ValueGuess(
false,
Guess::HIGH_CONFIDENCE,
),
EnumFormTypeGuesserCase::class,
'nullableBackedEnum',
];

yield 'Enum union property' => [
null,
EnumFormTypeGuesserCase::class,
'enumUnion',
];

yield 'Enum intersection property' => [
null,
EnumFormTypeGuesserCase::class,
'enumIntersection',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

class EnumFormTypeGuesserCase
{
public string $string;
public UndefinedEnum $undefinedEnum;
public EnumFormTypeGuesserCaseEnum $enum;
public ?EnumFormTypeGuesserCaseEnum $nullableEnum;
public BackedEnumFormTypeGuesserCaseEnum $backedEnum;
public ?BackedEnumFormTypeGuesserCaseEnum $nullableBackedEnum;
public EnumFormTypeGuesserCaseEnum|BackedEnumFormTypeGuesserCaseEnum $enumUnion;
public EnumFormTypeGuesserCaseEnum&BackedEnumFormTypeGuesserCaseEnum $enumIntersection;
}

enum EnumFormTypeGuesserCaseEnum
{
case Foo;
case Bar;
}

enum BackedEnumFormTypeGuesserCaseEnum: string
{
case Foo = 'foo';
case Bar = 'bar';
}
Loading