Repository: symfony/symfony
File: src/Symfony/Component/Form/EnumFormTypeGuesser.php (PHP)

Code before:
        return null;
    }

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

    /**
     * @return null|NullableType<EnumType>|EnumType
     */
    private function getPropertyType(string $class, string $property): null|NullableType|EnumType
    {
        if (isset($this->cache[$class]) && array_key_exists($property, $this->cache[$class])) {
            return $this->cache[$class][$property];
        }

        if (!isset($this->cache[$class])) {
            $this->cache[$class] = [];
        }

        try {
            $classReflection = new \ReflectionClass($class);
            $propertyReflection = $classReflection->getProperty($property);
        } catch (\ReflectionException) {
            return $this->cache[$class][$property] = null;
        }

        $propertyType = $this->typeResolver->resolve($propertyReflection);
        if (!$propertyType instanceof EnumType && (!$propertyType instanceof NullableType || !$propertyType->getWrappedType() instanceof EnumType)) {
            return $this->cache[$class][$property] = null;
        }

        return $this->cache[$class][$property] = $propertyType;
    }
}


Review comment (suggestion):
I am unsure whether there is a cleaner way of implementing this check with the type info component but I want to only support these two concrete cases:

* enum
* nullable enum

No other more complex case with additional types because then the guessing does not work.

Code after:
<?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, string|false>>
     */
    private array $cache = [];

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

        return new TypeGuess(EnumType::class, ['class' => ltrim($enum, '?')], Guess::HIGH_CONFIDENCE);
    }

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

        return new ValueGuess('?' !== $enum[0], 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): string|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())) {
            $enum = false;
        } else {
            $enum = $type->getName();
            if ($type->allowsNull()) {
                $enum = '?'.$enum;
            }
        }

        return $this->cache[$class][$property] = $enum;
    }
}