Skip to content

Commit 6d5da83

Browse files
authored
Add support for PHP 8.1 enums in embedded classes (#9419)
1 parent 5f01dd8 commit 6d5da83

7 files changed

Lines changed: 86 additions & 3 deletions

File tree

lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,19 @@ public function wakeupReflection($reflService)
10561056

10571057
foreach ($this->fieldMappings as $field => $mapping) {
10581058
if (isset($mapping['declaredField']) && isset($parentReflFields[$mapping['declaredField']])) {
1059+
$childProperty = $this->getAccessibleProperty($reflService, $mapping['originalClass'], $mapping['originalField']);
1060+
assert($childProperty !== null);
1061+
1062+
if (isset($mapping['enumType'])) {
1063+
$childProperty = new ReflectionEnumProperty(
1064+
$childProperty,
1065+
$mapping['enumType']
1066+
);
1067+
}
1068+
10591069
$this->reflFields[$field] = new ReflectionEmbeddedProperty(
10601070
$parentReflFields[$mapping['declaredField']],
1061-
$this->getAccessibleProperty($reflService, $mapping['originalClass'], $mapping['originalField']),
1071+
$childProperty,
10621072
$mapping['originalClass']
10631073
);
10641074
continue;

phpcs.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,11 @@
269269
<rule ref="Generic.WhiteSpace.ScopeIndent.Incorrect">
270270
<!-- see https://github.com/squizlabs/PHP_CodeSniffer/issues/3474 -->
271271
<exclude-pattern>tests/Doctrine/Tests/Models/Enums/Suit.php</exclude-pattern>
272+
<exclude-pattern>tests/Doctrine/Tests/Models/Enums/Unit.php</exclude-pattern>
272273
</rule>
273274
<rule ref="Generic.WhiteSpace.ScopeIndent.IncorrectExact">
274275
<!-- see https://github.com/squizlabs/PHP_CodeSniffer/issues/3474 -->
275276
<exclude-pattern>tests/Doctrine/Tests/Models/Enums/Suit.php</exclude-pattern>
277+
<exclude-pattern>tests/Doctrine/Tests/Models/Enums/Unit.php</exclude-pattern>
276278
</rule>
277279
</ruleset>

psalm-baseline.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@
763763
<code>$fieldName</code>
764764
<code>$fieldName</code>
765765
</ParamNameMismatch>
766-
<PossiblyNullArgument occurrences="9">
766+
<PossiblyNullArgument occurrences="8">
767767
<code>$class</code>
768768
<code>$className</code>
769769
<code>$entityResult['entityClass']</code>
@@ -772,7 +772,6 @@
772772
<code>$parentReflFields[$embeddedClass['declaredField']]</code>
773773
<code>$parentReflFields[$mapping['declaredField']]</code>
774774
<code>$queryMapping['resultClass']</code>
775-
<code>$this-&gt;getAccessibleProperty($reflService, $mapping['originalClass'], $mapping['originalField'])</code>
776775
</PossiblyNullArgument>
777776
<PossiblyNullPropertyFetch occurrences="2">
778777
<code>$embeddable-&gt;reflClass-&gt;name</code>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\Enums;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Embedded;
9+
use Doctrine\ORM\Mapping\Entity;
10+
use Doctrine\ORM\Mapping\GeneratedValue;
11+
use Doctrine\ORM\Mapping\Id;
12+
13+
#[Entity]
14+
class Product
15+
{
16+
#[Id, GeneratedValue, Column(type: 'integer')]
17+
public int $id;
18+
19+
#[Embedded(class: Quantity::class)]
20+
public Quantity $quantity;
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\Enums;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Embeddable;
9+
10+
#[Embeddable]
11+
class Quantity
12+
{
13+
#[Column(type: 'integer')]
14+
public int $value;
15+
16+
#[Column(type: 'string', enumType: Unit::class)]
17+
public Unit $unit;
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\Enums;
6+
7+
enum Unit: string
8+
{
9+
case Gram = 'g';
10+
case Meter = 'm';
11+
}

tests/Doctrine/Tests/ORM/Functional/EnumTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
use Doctrine\ORM\Mapping\MappingException;
1010
use Doctrine\ORM\Tools\SchemaTool;
1111
use Doctrine\Tests\Models\Enums\Card;
12+
use Doctrine\Tests\Models\Enums\Product;
13+
use Doctrine\Tests\Models\Enums\Quantity;
1214
use Doctrine\Tests\Models\Enums\Suit;
1315
use Doctrine\Tests\Models\Enums\TypedCard;
16+
use Doctrine\Tests\Models\Enums\Unit;
1417
use Doctrine\Tests\OrmFunctionalTestCase;
1518

1619
use function dirname;
@@ -113,4 +116,23 @@ public function testItAllowsReadingAttributes(): void
113116
$this->assertCount(1, $attributes);
114117
$this->assertEquals(Column::class, $attributes[0]->getName());
115118
}
119+
120+
public function testEnumMappingWithEmbeddable(): void
121+
{
122+
$this->setUpEntitySchema([Product::class]);
123+
124+
$product = new Product();
125+
$product->quantity = new Quantity();
126+
$product->quantity->value = 10;
127+
$product->quantity->unit = Unit::Gram;
128+
129+
$this->_em->persist($product);
130+
$this->_em->flush();
131+
$this->_em->clear();
132+
133+
$fetchedProduct = $this->_em->find(Product::class, $product->id);
134+
135+
$this->assertInstanceOf(Unit::class, $fetchedProduct->quantity->unit);
136+
$this->assertEquals(Unit::Gram, $fetchedProduct->quantity->unit);
137+
}
116138
}

0 commit comments

Comments
 (0)