-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
mttsch
wants to merge
1
commit into
symfony:7.4
Choose a base branch
from
mttsch:feature/61272-form-enum-type-guesser
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+327
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||
xabbuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
{ | ||||||||||||
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
208
src/Symfony/Component/Form/Tests/EnumFormTypeGuesserTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Symfony/Component/Form/Tests/Fixtures/EnumFormTypeGuesserCase.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.