Skip to content

Commit 86df076

Browse files
author
Gigino Chianese
committed
[DoctrineBridge] Extend type guessing on enum fields
Doctrine supports enumType on array values. In those cases the guessed type should be of type array with collection information.
1 parent fbd5b86 commit 86df076

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

PropertyInfo/DoctrineExtractor.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,18 @@ public function getTypes($class, $property, array $context = [])
152152
}
153153

154154
if ($metadata->hasField($property)) {
155-
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
156-
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
157-
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass)];
158-
}
159-
160155
$typeOfField = $metadata->getTypeOfField($property);
161156

162157
if (!$builtinType = $this->getPhpType($typeOfField)) {
163158
return null;
164159
}
165160

161+
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
162+
$enumType = null;
163+
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
164+
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
165+
}
166+
166167
switch ($builtinType) {
167168
case Type::BUILTIN_TYPE_OBJECT:
168169
switch ($typeOfField) {
@@ -192,11 +193,22 @@ public function getTypes($class, $property, array $context = [])
192193
case self::$useDeprecatedConstants ? DBALType::TARRAY : Types::ARRAY:
193194
// no break
194195
case 'json_array':
196+
// return null if $enumType is set, because we can't determine if collectionKeyType is string or int
197+
if ($enumType) {
198+
return null;
199+
}
200+
195201
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
196202

197203
case self::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
198-
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
204+
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), $enumType ?? new Type(Type::BUILTIN_TYPE_STRING))];
205+
}
206+
case Type::BUILTIN_TYPE_INT:
207+
case Type::BUILTIN_TYPE_STRING:
208+
if ($enumType !== null) {
209+
return [$enumType];
199210
}
211+
// no break
200212
}
201213

202214
return [new Type($builtinType, $nullable)];

Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ public function testExtractEnum()
185185
}
186186
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumString::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumString', []));
187187
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumInt::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumInt', []));
188+
$this->assertEquals(null, $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumStringArray', []));
189+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumInt::class))], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumIntArray', []));
190+
$this->assertEquals(null, $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumCustom', []));
188191
}
189192

190193
public function typesProvider()

Tests/PropertyInfo/Fixtures/DoctrineEnum.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,19 @@ class DoctrineEnum
3535
* @Column(type="integer", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
3636
*/
3737
protected $enumInt;
38+
39+
/**
40+
* @Column(type="array", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString")
41+
*/
42+
protected $enumStringArray;
43+
44+
/**
45+
* @Column(type="simple_array", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
46+
*/
47+
protected $enumIntArray;
48+
49+
/**
50+
* @Column(type="custom_foo", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
51+
*/
52+
protected $enumCustom;
3853
}

0 commit comments

Comments
 (0)