Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8171050

Browse filesBrowse files
committed
Fix invalid guess with enumType
1 parent 9f5238d commit 8171050
Copy full SHA for 8171050

File tree

8 files changed

+150
-3
lines changed
Filter options

8 files changed

+150
-3
lines changed

‎src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,17 @@ 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+
155160
$typeOfField = $metadata->getTypeOfField($property);
156161

157162
if (!$builtinType = $this->getPhpType($typeOfField)) {
158163
return null;
159164
}
160165

161-
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
162-
163166
switch ($builtinType) {
164167
case Type::BUILTIN_TYPE_OBJECT:
165168
switch ($typeOfField) {
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\ORM\Mapping as ORM;
15+
16+
/**
17+
* @ORM\Entity
18+
*/
19+
class DoctrineLoaderEnum
20+
{
21+
/**
22+
* @ORM\Id
23+
* @ORM\Column
24+
*/
25+
public $id;
26+
27+
/**
28+
* @ORM\Column(type="string", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString", length=1)
29+
*/
30+
public $enumString;
31+
32+
/**
33+
* @ORM\Column(type="integer", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
34+
*/
35+
public $enumInt;
36+
}

‎src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
use PHPUnit\Framework\TestCase;
2020
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
2121
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy210;
22+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEnum;
2223
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineGeneratedValue;
2324
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
25+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt;
26+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString;
2427
use Symfony\Component\PropertyInfo\Type;
2528

2629
/**
@@ -171,6 +174,15 @@ private function doTestExtractWithEmbedded(bool $legacy)
171174
$this->assertEquals($expectedTypes, $actualTypes);
172175
}
173176

177+
/**
178+
* @requires PHP 8.1
179+
*/
180+
public function testExtractEnum()
181+
{
182+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumString::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumString', []));
183+
$this->assertEquals([new Type(Type::BUILTIN_TYPE_OBJECT, false, EnumInt::class)], $this->createExtractor()->getTypes(DoctrineEnum::class, 'enumInt', []));
184+
}
185+
174186
public function typesProvider()
175187
{
176188
$provider = [
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Entity;
16+
17+
/**
18+
* @Entity
19+
*/
20+
class DoctrineEnum
21+
{
22+
/**
23+
* @Id
24+
* @Column(type="smallint")
25+
*/
26+
public $id;
27+
28+
/**
29+
* @Column(type="string", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumString")
30+
*/
31+
protected $enumString;
32+
33+
/**
34+
* @Column(type="integer", enumType="Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\EnumInt")
35+
*/
36+
protected $enumInt;
37+
}
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
enum EnumInt: int
15+
{
16+
case Foo = 0;
17+
case Bar = 1;
18+
}
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
enum EnumString: string
15+
{
16+
case Foo = 'f';
17+
case Bar = 'b';
18+
}

‎src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bridge\Doctrine\Tests\Fixtures\BaseUser;
1717
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEmbed;
1818
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEntity;
19+
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEnum;
1920
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderNestedEmbed;
2021
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderNoAutoMappingEntity;
2122
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderParentEntity;
@@ -149,6 +150,27 @@ public function testLoadClassMetadata()
149150
$this->assertSame(AutoMappingStrategy::DISABLED, $noAutoMappingMetadata[0]->getAutoMappingStrategy());
150151
}
151152

153+
/**
154+
* @requires PHP 8.1
155+
*/
156+
public function testExtractEnum()
157+
{
158+
$validator = Validation::createValidatorBuilder()
159+
->enableAnnotationMapping(true)
160+
->addDefaultDoctrineAnnotationReader()
161+
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
162+
->getValidator()
163+
;
164+
165+
$classMetadata = $validator->getMetadataFor(new DoctrineLoaderEnum());
166+
167+
$enumStringMetadata = $classMetadata->getPropertyMetadata('enumString');
168+
$this->assertCount(0, $enumStringMetadata); // asserts the length constraint is not added to an enum
169+
170+
$enumStringMetadata = $classMetadata->getPropertyMetadata('enumInt');
171+
$this->assertCount(0, $enumStringMetadata); // asserts the length constraint is not added to an enum
172+
}
173+
152174
public function testFieldMappingsConfiguration()
153175
{
154176
$validator = Validation::createValidatorBuilder()

‎src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
1717
use Doctrine\Persistence\Mapping\MappingException;
1818
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
19+
use Symfony\Component\PropertyInfo\Type;
1920
use Symfony\Component\Validator\Constraints\Length;
2021
use Symfony\Component\Validator\Constraints\Valid;
2122
use Symfony\Component\Validator\Mapping\AutoMappingStrategy;
@@ -99,7 +100,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
99100
$loaded = true;
100101
}
101102

102-
if (null === ($mapping['length'] ?? null) || !\in_array($mapping['type'], ['string', 'text'], true)) {
103+
if (null === ($mapping['length'] ?? null) || null !== ($mapping['enumType'] ?? null) || !\in_array($mapping['type'], ['string', 'text'], true)) {
103104
continue;
104105
}
105106

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.