Elao/PhpEnums

FlaggedEnum formtype not working with warm cache

Closed this issue · 3 comments

I think I found an issue with the FlaggedEnums, that might be related to cache.

Consider the following enum:

<?php

namespace App\Enum;

use Elao\Enum\FlaggedEnum;

final class TestEnumType extends FlaggedEnum
{
    const TEST_1 = 1;
    const TEST_2 = 2;
    
    public static function values(): array
    {
        return [
            // Only declare valid bit flags:
            static::TEST_1,
            static::TEST_2,
        ];
    }

    public static function readables(): array
    {
        return [
            static::TEST_1 => 'Test 1',
            static::TEST_2 => 'Test 2',
        ];
    }
}

Then, the this part return is_a($value, FlaggedEnum::class, true);of the following class returns true when the cache is cold and returns false if the cache is warm (available):

<?php

namespace Elao\Enum\Bridge\Symfony\Form\Type;

...

class FlaggedEnumType extends AbstractType
{
...
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver
            ->setDefault('multiple', true)
            ->setAllowedValues('enum_class', static function ($value) {
                return is_a($value, FlaggedEnum::class, true);
            })
        ;
    }
...
}

I am using this in combination with EasyAdmin 2.

Hi there.

That's weird indeed. I should inspect this, but I don't see how it could be related to the enums, and specifically flagged enums 🤔

The issue does not occur on regular enums, therefore I targeted the issue specific to flagged enums.

In another attempt from my side, I tried always returning true.
Then, I get the following error:

Attempted to load class "TestenumType" from namespace "App\Enum".
Did you forget a "use" statement for another namespace?

in vendor/elao/enum/src/Bridge/Symfony/Form/Type/EnumType.php (line 60) 

Oké, I finally spotted the issue. Nothing related to (flagged) enums indeed. For reference and others seeing strange behavior:

In EasyAdmin 2 the enums are configured using yaml:

            form:
                fields:
                    - { property: 'testProperty', type: Elao\Enum\Bridge\Symfony\Form\Type\FlaggedEnumType, type_options: { enum_class: App\Enum\TestEnumType }}

However, instead of App\Enum\TestEnumType I configured App\Enum\TestenumType, so with typo.
For some reason, this is probably fine without cache, and not with cache.