-
-
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
base: 7.4
Are you sure you want to change the base?
Conversation
30ce63c
to
0796aa4
Compare
This comment was marked as outdated.
This comment was marked as outdated.
80d176c
to
78a2346
Compare
1fbb523
to
a5eebb2
Compare
|
||
namespace Symfony\Component\Form\Tests\Fixtures; | ||
|
||
final class EnumFormTypeGuesserCase |
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.
why final?
use Symfony\Component\Form\Tests\Fixtures\EnumFormTypeGuesserCase; | ||
use Symfony\Component\Form\Tests\Fixtures\EnumFormTypeGuesserCaseEnum; | ||
|
||
final class EnumFormTypeGuesserTest extends TestCase |
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.
final is pure visual overhead ;)
final class EnumFormTypeGuesserTest extends TestCase | |
class EnumFormTypeGuesserTest extends TestCase |
|
||
final class EnumFormTypeGuesser implements FormTypeGuesserInterface | ||
{ | ||
private array $cache = []; |
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.
private array $cache = []; | |
/** | |
* @var (\ReflectionNamedType|false)[] | |
*/ | |
private array $cache = []; |
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.
The suggested type is not correct, I used the correct one (nested array)
|
||
public function guessType(string $class, string $property): ?TypeGuess | ||
{ | ||
if (null === ($propertyType = $this->getPropertyType($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.
if (null === ($propertyType = $this->getPropertyType($class, $property))) { | |
if (!$propertyType = $this->getPropertyType($class, $property)) { |
|
||
private function getPropertyType(string $class, string $property): ?\ReflectionNamedType | ||
{ | ||
if (isset($this->cache[$class]) && \array_key_exists($property, $this->cache[$class])) { |
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.
if (isset($this->cache[$class]) && \array_key_exists($property, $this->cache[$class])) { | |
if (isset($this->cache[$class][$property])) { |
if (!isset($this->cache[$class])) { | ||
$this->cache[$class] = []; | ||
} | ||
|
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.
if (!isset($this->cache[$class])) { | |
$this->cache[$class] = []; | |
} |
$classReflection = new \ReflectionClass($class); | ||
$propertyReflection = $classReflection->getProperty($property); | ||
} catch (\ReflectionException) { | ||
return $this->cache[$class][$property] = 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.
return $this->cache[$class][$property] = null; | |
return $this->cache[$class][$property] = false; |
$classReflection = new \ReflectionClass($class); | ||
$propertyReflection = $classReflection->getProperty($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.
$classReflection = new \ReflectionClass($class); | |
$propertyReflection = $classReflection->getProperty($property); | |
$propertyReflection = new \ReflectionProperty($class, $property); |
if (!$propertyReflection->getType() instanceof \ReflectionNamedType || !enum_exists($propertyReflection->getType()->getName())) { | ||
return $this->cache[$class][$property] = null; | ||
} | ||
|
||
return $this->cache[$class][$property] = $propertyReflection->getType(); |
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.
if (!$propertyReflection->getType() instanceof \ReflectionNamedType || !enum_exists($propertyReflection->getType()->getName())) { | |
return $this->cache[$class][$property] = null; | |
} | |
return $this->cache[$class][$property] = $propertyReflection->getType(); | |
$type = $propertyReflection->getType(); | |
if (!$type instanceof \ReflectionNamedType || !enum_exists($enum->getName())) { | |
$type = false; | |
} | |
return $this->cache[$class][$property] = $type; |
a5eebb2
to
1e540eb
Compare
@@ -564,6 +565,10 @@ public function load(array $configs, ContainerBuilder $container): void | |||
if (!$this->readConfigEnabled('html_sanitizer', $container, $config['html_sanitizer']) || !class_exists(TextTypeHtmlSanitizerExtension::class)) { | |||
$container->removeDefinition('form.type_extension.form.html_sanitizer'); | |||
} | |||
|
|||
if (!class_exists(EnumFormTypeGuesser::class)) { |
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 would do this inside registerFormConfiguration()
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.
Done
|
||
namespace Symfony\Component\Form; | ||
|
||
use Symfony\Component\Form\Extension\Core\Type\EnumType as FormEnumType; |
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.
use Symfony\Component\Form\Extension\Core\Type\EnumType as FormEnumType; | |
use Symfony\Component\Form\Extension\Core\Type\EnumType; |
I don't see why we need the alias here
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.
Done. (Was previously needed because EnumType
from symfony/type-info
was also used.)
1e540eb
to
dc492c9
Compare
This new form type guesser guesses the form type of an enum property and also sets the
class
option of theEnumType
to the relevant enum name.