Skip to content

Commit

Permalink
Fixed enum mapping validation
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Dec 14, 2023
1 parent 2b91edc commit f5bd990
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/Doctrine/ORM/Tools/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,25 @@ function (array $fieldMapping) use ($class): ?string {
);
}

if (
isset($fieldMapping['enumType'])
&& $propertyType !== $fieldMapping['enumType']
&& interface_exists($propertyType)

Check failure on line 412 in lib/Doctrine/ORM/Tools/SchemaValidator.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Function interface_exists() should not be referenced via a fallback global name, but via a use statement.
&& is_a($fieldMapping['enumType'], $propertyType, true)
) {
if ($metadataFieldType === (string) (new ReflectionEnum($fieldMapping['enumType']))->getBackingType()) {
return null;
}

return sprintf(
"The field '%s#%s' has the metadata enumType '%s' that differs from the metadata field type '%s'.",
$class->name,
$fieldName,
$fieldMapping['enumType'],
$metadataFieldType
);
}

if (
$fieldMapping['type'] === 'json'
&& in_array($propertyType, ['string', 'int', 'float', 'bool', 'true', 'false', 'null'], true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11037;

interface EntityStatus
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testMetadataFieldTypeNotCoherentWithEntityPropertyType(): void
[
"The field 'Doctrine\Tests\ORM\Functional\Ticket\GH11037\InvalidEntityWithTypedEnum#status1' has the property type 'Doctrine\Tests\ORM\Functional\Ticket\GH11037\StringEntityStatus' that differs from the metadata field type 'int' returned by the 'integer' DBAL type.",
"The field 'Doctrine\Tests\ORM\Functional\Ticket\GH11037\InvalidEntityWithTypedEnum#status2' has the property type 'Doctrine\Tests\ORM\Functional\Ticket\GH11037\IntEntityStatus' that differs from the metadata enumType 'Doctrine\Tests\ORM\Functional\Ticket\GH11037\StringEntityStatus'.",
"The field 'Doctrine\Tests\ORM\Functional\Ticket\GH11037\InvalidEntityWithTypedEnum#status3' has the metadata enumType 'Doctrine\Tests\ORM\Functional\Ticket\GH11037\StringEntityStatus' that differs from the metadata field type 'int'.",
],
$ce
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ class InvalidEntityWithTypedEnum
* @Column(type="integer", enumType=StringEntityStatus::class)
*/
protected IntEntityStatus $status2;

/**
* @Column(type="integer", enumType=StringEntityStatus::class)
*/
protected EntityStatus $status3;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11037;

enum StringEntityStatus: string
enum StringEntityStatus: string implements EntityStatus
{
case ACTIVE = 'active';
case INACTIVE = 'inactive';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ class ValidEntityWithTypedEnum
* @Column(type="smallint", enumType=IntEntityStatus::class)
*/
protected IntEntityStatus $status2;

/**
* @Column(type="string", enumType=StringEntityStatus::class)
*/
protected EntityStatus $status3;
}

0 comments on commit f5bd990

Please sign in to comment.