Skip to content
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

Throw exception when trying to use non-backed enum types #11176

Merged
merged 1 commit into from
Jan 23, 2024
Merged
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
10 changes: 9 additions & 1 deletion lib/Doctrine/ORM/Mapping/DefaultTypedFieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ public function validateAndComplete(array $mapping, ReflectionProperty $field):
$mapping['enumType'] = $type->getName();

$reflection = new ReflectionEnum($type->getName());
$type = $reflection->getBackingType();
if (! $reflection->isBacked()) {
throw MappingException::backedEnumTypeRequired(
$field->class,
$mapping['fieldName'],
$mapping['enumType']
);
}

$type = $reflection->getBackingType();

assert($type instanceof ReflectionNamedType);
}
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/ORM/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,16 @@ public static function enumsRequirePhp81(string $className, string $fieldName):
return new self(sprintf('Enum types require PHP 8.1 in %s::$%s', $className, $fieldName));
}

public static function backedEnumTypeRequired(string $className, string $fieldName, string $enumType): self
{
return new self(sprintf(
'Attempting to map a non-backed enum type %s in entity %s::$%s. Please use backed enums only',
$enumType,
$className,
$fieldName
));
}

public static function nonEnumTypeMapped(string $className, string $fieldName, string $enumType): self
{
return new self(sprintf(
Expand Down
22 changes: 22 additions & 0 deletions tests/Doctrine/Tests/Models/Enums/FaultySwitch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\Enums;

use Doctrine\ORM\Mapping\Column;

class FaultySwitch
{
#[Column(type: 'string')]
public string $value;

/**
* The following line is ignored on psalm and phpstan so that we can test
* that the mapping is throwing an exception when a non-backed enum is used.
*
* @psalm-suppress InvalidArgument
*/
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
#[Column(enumType: SwitchStatus::class)]
public SwitchStatus $status;
}
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/Models/Enums/SwitchStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\Enums;

enum SwitchStatus
{
case ON;
case OFF;
}
34 changes: 34 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/TypedEnumFieldMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Mapping;

use Doctrine\ORM\Mapping\DefaultTypedFieldMapper;
use Doctrine\ORM\Mapping\MappingException;
use Doctrine\Tests\Models\Enums\FaultySwitch;
use Doctrine\Tests\OrmTestCase;
use ReflectionClass;

/**
* @requires PHP >= 8.1
*/
class TypedEnumFieldMapperTest extends OrmTestCase
{
private static function defaultTypedFieldMapper(): DefaultTypedFieldMapper
{
return new DefaultTypedFieldMapper();
}

public function testNotBackedEnumThrows(): void
{
$reflectionClass = new ReflectionClass(FaultySwitch::class);

$this->expectException(MappingException::class);
$this->expectExceptionMessage(
'Attempting to map a non-backed enum type Doctrine\Tests\Models\Enums\SwitchStatus in entity Doctrine\Tests\Models\Enums\FaultySwitch::$status. Please use backed enums only'
);

self::defaultTypedFieldMapper()->validateAndComplete(['fieldName' => 'status'], $reflectionClass->getProperty('status'));
}
}