8000 bug #35851 [4.4][DoctrineBridge] Use new Types::* constants and suppo… · symfony/symfony@88b96ab · GitHub
[go: up one dir, main page]

Skip to content

Commit 88b96ab

Browse files
committed
bug #35851 [4.4][DoctrineBridge] Use new Types::* constants and support new json types (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [4.4][DoctrineBridge] Use new Types::* constants and support new json types | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #35817 (comment) | License | MIT | Doc PR | - Commits ------- ddf3353 [4.4][DoctrineBridge] Use new Types::* constants and support new json type
2 parents 1024f5f + ddf3353 commit 88b96ab

File tree

5 files changed

+113
-71
lines changed

5 files changed

+113
-71
lines changed

src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\Form;
1313

1414
use Doctrine\DBAL\Types\Type;
15+
use Doctrine\DBAL\Types\Types;
1516
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1617
use Doctrine\ORM\Mapping\MappingException as LegacyMappingException;
1718
use Doctrine\Persistence\ManagerRegistry;
@@ -28,9 +29,15 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
2829

2930
private $cache = [];
3031

32+
private static $useDeprecatedConstants;
33+
3134
public function __construct(ManagerRegistry $registry)
3235
{
3336
$this->registry = $registry;
37+
38+
if (null === self::$useDeprecatedConstants) {
39+
self::$useDeprecatedConstants = !class_exists(Types::class);
40+
}
3441
}
3542

3643
/**
@@ -52,39 +59,39 @@ public function guessType($class, $property)
5259
}
5360

5461
switch ($metadata->getTypeOfField($property)) {
55-
case Type::TARRAY:
56-
case Type::SIMPLE_ARRAY:
62+
case self::$useDeprecatedConstants ? Type::TARRAY : Types::ARRAY:
63+
case self::$useDeprecatedConstants ? Type::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
5764
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CollectionType', [], Guess::MEDIUM_CONFIDENCE);
58-
case Type::BOOLEAN:
65+
case self::$useDeprecatedConstants ? Type::BOOLEAN : Types::BOOLEAN:
5966
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\CheckboxType', [], Guess::HIGH_CONFIDENCE);
60-
case Type::DATETIME:
61-
case Type::DATETIMETZ:
67+
case self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE:
68+
case self::$useDeprecatedConstants ? Type::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
6269
case 'vardatetime':
6370
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', [], Guess::HIGH_CONFIDENCE);
6471
case 'datetime_immutable':
6572
case 'datetimetz_immutable':
6673 F438
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateTimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE);
6774
case 'dateinterval':
6875
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateIntervalType', [], Guess::HIGH_CONFIDENCE);
69-
case Type::DATE:
76+
case self::$useDeprecatedConstants ? Type::DATE : Types::DATE_MUTABLE:
7077
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', [], Guess::HIGH_CONFIDENCE);
7178
case 'date_immutable':
7279
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\DateType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE);
73-
case Type::TIME:
80+
case self::$useDeprecatedConstants ? Type::TIME : Types::TIME_MUTABLE:
7481
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', [], Guess::HIGH_CONFIDENCE);
7582
case 'time_immutable':
7683
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TimeType', ['input' => 'datetime_immutable'], Guess::HIGH_CONFIDENCE);
77-
case Type::DECIMAL:
84+
case self::$useDeprecatedConstants ? Type::DECIMAL : Types::DECIMAL:
7885
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', ['input' => 'string'], Guess::MEDIUM_CONFIDENCE);
79-
case Type::FLOAT:
86+
case self::$useDeprecatedConstants ? Type::FLOAT : Types::FLOAT:
8087
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\NumberType', [], Guess::MEDIUM_CONFIDENCE);
81-
case Type::INTEGER:
82-
case Type::BIGINT:
83-
case Type::SMALLINT:
88+
case self::$useDeprecatedConstants ? Type::INTEGER : Types::INTEGER:
89+
case self::$useDeprecatedConstants ? Type::BIGINT : Types::BIGINT:
90+
case self::$useDeprecatedConstants ? Type::SMALLINT : Types::SMALLINT:
8491
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\IntegerType', [], Guess::MEDIUM_CONFIDENCE);
85-
case Type::STRING:
92+
case self::$useDeprecatedConstants ? Type::STRING : Types::STRING:
8693
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::MEDIUM_CONFIDENCE);
87-
case Type::TEXT:
94+
case self::$useDeprecatedConstants ? Type::TEXT : Types::TEXT:
8895
return new TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextareaType', [], Guess::MEDIUM_CONFIDENCE);
8996
default:
9097
return ne 10000 w TypeGuess('Symfony\Component\Form\Extension\Core\Type\TextType', [], Guess::LOW_CONFIDENCE);
@@ -107,7 +114,7 @@ public function guessRequired($class, $property)
107114

108115
// Check whether the field exists and is nullable or not
109116
if (isset($classMetadata->fieldMappings[$property])) {
110-
if (!$classMetadata->isNullable($property) && Type::BOOLEAN !== $classMetadata->getTypeOfField($property)) {
117+
if (!$classMetadata->isNullable($property) && (self::$useDeprecatedConstants ? Type::BOOLEAN : Types::BOOLEAN) !== $classMetadata->getTypeOfField($property)) {
111118
return new ValueGuess(true, Guess::HIGH_CONFIDENCE);
112119
}
113120

@@ -144,7 +151,7 @@ public function guessMaxLength($class, $property)
144151
return new ValueGuess($mapping['length'], Guess::HIGH_CONFIDENCE);
145152
}
146153

147-
if (\in_array($ret[0]->getTypeOfField($property), [Type::DECIMAL, Type::FLOAT])) {
154+
if (\in_array($ret[0]->getTypeOfField($property), self::$useDeprecatedConstants ? [Type::DECIMAL, Type::FLOAT] : [Types::DECIMAL, Types::FLOAT])) {
148155
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
149156
}
150157
}
@@ -159,7 +166,7 @@ public function guessPattern($class, $property)
159166
{
160167
$ret = $this->getMetadata($class);
161168
if ($ret && isset($ret[0]->fieldMappings[$property]) && !$ret[0]->hasAssociation($property)) {
162-
if (\in_array($ret[0]->getTypeOfField($property), [Type::DECIMAL, Type::FLOAT])) {
169+
if (\in_array($ret[0]->getTypeOfField($property), self::$useDeprecatedConstants ? [Type::DECIMAL, Type::FLOAT] : [Types::DECIMAL, Types::FLOAT])) {
163170
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
164171
}
165172
}

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

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Doctrine\PropertyInfo;
1313

1414
use Doctrine\DBAL\Types\Type as DBALType;
15+
use Doctrine\DBAL\Types\Types;
1516
use Doctrine\ORM\EntityManagerInterface;
1617
use Doctrine\ORM\Mapping\ClassMetadata;
1718
use Doctrine\ORM\Mapping\ClassMetadataInfo;
@@ -33,6 +34,8 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
3334
private $entityManager;
3435
private $classMetadataFactory;
3536

37+
private static $useDeprecatedConstants;
38+
3639
/**
3740
* @param EntityManagerInterface $entityManager
3841
*/
@@ -46,6 +49,10 @@ public function __construct($entityManager)
4649
} else {
4750
throw new \TypeError(sprintf('$entityManager must be an instance of "%s", "%s" given.', EntityManagerInterface::class, \is_object($entityManager) ? \get_class($entityManager) : \gettype($entityManager)));
4851
}
52+
53+
if (null === self::$useDeprecatedConstants) {
54+
self::$useDeprecatedConstants = !class_exists(Types::class);
55+
}
4956
}
5057

5158
/**
@@ -146,11 +153,11 @@ public function getTypes($class, $property, array $context = [])
146153
switch ($builtinType) {
147154
case Type::BUILTIN_TYPE_OBJECT:
148155
switch ($typeOfField) {
149-
case DBALType::DATE:
150-
case DBALType::DATETIME:
151-
case DBALType::DATETIMETZ:
156+
case self::$useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
157+
case self::$useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
158+
case self::$useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
152159
case 'vardatetime':
153-
case DBALType::TIME:
160+
case self::$useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
154161
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
155162

156163
case 'date_immutable':
@@ -166,11 +173,12 @@ public function getTypes($class, $property, array $context = [])
166173
break;
167174
case Type::BUILTIN_TYPE_ARRAY:
168175
switch ($typeOfField) {
169-
case DBALType::TARRAY:
170-
case DBALType::JSON_ARRAY:
176+
case self::$useDeprecatedConstants ? DBALType::TARRAY : Types::ARRAY:
177+
case 'json_array':
178+
case self::$useDeprecatedConstants ? false : Types::JSON:
171179
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
172180

173-
case DBALType::SIMPLE_ARRAY:
181+
case self::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
174182
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))];
175183
}
176184
}
@@ -245,43 +253,44 @@ private function isAssociationNullable(array $associationMapping): bool
245253
private function getPhpType(string $doctrineType): ?string
246254
{
247255
switch ($doctrineType) {
248-
case DBALType::SMALLINT:
249-
case DBALType::INTEGER:
256+
case self::$useDeprecatedConstants ? DBALType::SMALLINT : Types::SMALLINT:
257+
case self::$useDeprecatedConstants ? DBALType::INTEGER : Types::INTEGER:
250258
return Type::BUILTIN_TYPE_INT;
251259

252-
case DBALType::FLOAT:
260+
case self::$useDeprecatedConstants ? DBALType::FLOAT : Types::FLOAT:
253261
return Type::BUILTIN_TYPE_FLOAT;
254262

255-
case DBALType::BIGINT:
256-
case DBALType::STRING:
257-
case DBALType::TEXT:
258-
case DBALType::GUID:
259-
case DBALType::DECIMAL:
263+
case self::$useDeprecatedConstants ? DBALType::BIGINT : Types::BIGINT:
264+
case self::$useDeprecatedConstants ? DBALType::STRING : Types::STRING:
265+
case self::$useDeprecatedConstants ? DBALType::TEXT : Types::TEXT:
266+
case self::$useDeprecatedConstants ? DBALType::GUID : Types::GUID:
267+
case self::$useDeprecatedConstants ? DBALType::DECIMAL : Types::DECIMAL:
260268
return Type::BUILTIN_TYPE_STRING;
261269

262-
case DBALType::BOOLEAN:
270+
case self::$useDeprecatedConstants ? DBALType::BOOLEAN : Types::BOOLEAN:
263271
return Type::BUILTIN_TYPE_BOOL;
264272

265-
case DBALType::BLOB:
273+
case self::$useDeprecatedConstants ? DBALType::BLOB : Types::BLOB:
266274
case 'binary':
267275
return Type::BUILTIN_TYPE_RESOURCE;
268276

269-
case DBALType::OBJECT:
270-
case DBALType::DATE:
271-
case DBALType::DATETIME:
272-
case DBALType::DATETIMETZ:
277+
case self::$useDeprecatedConstants ? DBALType::OBJECT : Types::OBJECT:
278+
case self::$useDeprecatedConstants ? DBALType::DATE : Types::DATE_MUTABLE:
279+
case self::$useDeprecatedConstants ? DBALType::DATETIME : Types::DATETIME_MUTABLE:
280+
case self::$useDeprecatedConstants ? DBALType::DATETIMETZ : Types::DATETIMETZ_MUTABLE:
273281
case 'vardatetime':
274-
case DBALType::TIME:
282+
case self::$useDeprecatedConstants ? DBALType::TIME : Types::TIME_MUTABLE:
275283
case 'date_immutable':
276284
case 'datetime_immutable':
277285
case 'datetimetz_immutable':
278286
case 'time_immutable':
279287
case 'dateinterval':
280288
return Type::BUILTIN_TYPE_OBJECT;
281289

282-
case DBALType::TARRAY:
283-
case DBALType::SIMPLE_ARRAY:
284-
case DBALType::JSON_ARRAY:
290+
case self::$useDeprecatedConstants ? DBALType::TARRAY : Types::ARRAY:
291+
case self::$useDeprecatedConstants ? DBALType::SIMPLE_ARRAY : Types::SIMPLE_ARRAY:
292+
case 'json_array':
293+
case self::$useDeprecatedConstants ? false : Types::JSON:
285294
return Type::BUILTIN_TYPE_ARRAY;
286295
}
287296

src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespace Symfony\Bridge\Doctrine\Security\RememberMe;
1313

1414
use Doctrine\DBAL\Connection;
15-
use Doctrine\DBAL\Types\Type as DoctrineType;
15+
use Doctrine\DBAL\Types\Type;
16+
use Doctrine\DBAL\Types\Types;
1617
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
1718
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentTokenInterface;
1819
use Symfony\Component\Security\Core\Authentication\RememberMe\TokenProviderInterface;
@@ -40,9 +41,15 @@ class DoctrineTokenProvider implements TokenProviderInterface
4041
{
4142
private $conn;
4243

44+
private static $useDeprecatedConstants;
45+
4346
public function __construct(Connection $conn)
4447
{
4548
$this->conn = $conn;
49+
50+
if (null === self::$useDeprecatedConstants) {
51+
self::$useDeprecatedConstants = !class_exists(Types::class);
52+
}
4653
}
4754

4855
/**
@@ -90,7 +97,7 @@ public function updateToken($series, $tokenValue, \DateTime $lastUsed)
9097
];
9198
$paramTypes = [
9299
'value' => \PDO::PARAM_STR,
93-
'lastUsed' => DoctrineType::DATETIME,
100+
'lastUsed' => self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE,
94101
'series' => \PDO::PARAM_STR,
95102
];
96103
$updated = $this->conn->executeUpdate($sql, $paramValues, $paramTypes);
@@ -119,7 +126,7 @@ public function createNewToken(PersistentTokenInterface $token)
119126
'username' => \PDO::PARAM_STR,
120127
'series' => \PDO::PARAM_STR,
121128
'value' => \PDO::PARAM_STR,
122-
'lastUsed' => DoctrineType::DATETIME,
129+
'lastUsed' => self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE,
123130
];
124131
$this->conn->executeUpdate($sql, $paramValues, $paramTypes);
125132
}

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

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
use Doctrine\Common\Collections\Collection;
1515
use Doctrine\DBAL\Types\Type as DBALType;
16+
use Doctrine\DBAL\Types\Types;
1617
use Doctrine\ORM\EntityManager;
1718
use Doctrine\ORM\Tools\Setup;
1819
use PHPUnit\Framework\TestCase;
1920
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
21+
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy210;
2022
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineGeneratedValue;
2123
use Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation;
2224
use Symfony\Component\PropertyInfo\Type;
@@ -51,29 +53,40 @@ public function testLegacyGetProperties()
5153

5254
private function doTestGetProperties(bool $legacy)
5355
{
56+
// Fields
57+
$expected = [
58+
'id',
59+
'guid',
60+
'time',
61+
'timeImmutable',
62+
'dateInterval',
63+
'jsonArray',
64+
'simpleArray',
65+
'float',
66+
'decimal',
67+
'bool',
68+
'binary',
69+
'customFoo',
70+
'bigint',
71+
];
72+
73+
if (class_exists(Types::class)) {
74+
$expected[] = 'json';
75+
}
76+
77+
// Associations
78+
$expected = array_merge($expected, [
79+
'foo',
80+
'bar',
81+
'indexedBar',
82+
'indexedFoo',
83+
'indexedByDt',
84+
'indexedByCustomType',
85+
]);
86+
5487
$this->assertEquals(
55-
[
56-
'id',
57-
'guid',
58-
'time',
59-
'timeImmutable',
60-
'dateInterval',
61-
'json',
62-
'simpleArray',
63-
'float',
64-
'decimal',
65-
'bool',
66-
'binary',
67-
'customFoo',
68-
'bigint',
69-
'foo',
70-
'bar',
71-
'indexedBar',
72-
'indexedFoo',
73-
'indexedByDt',
74-
'indexedByCustomType',
75-
],
76-
$this->createExtractor($legacy)->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
88+
$expected,
89+
$this->createExtractor($legacy)->getProperties(!class_exists(Types::class) ? 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy' : DoctrineDummy210::class)
7790
);
7891
}
7992

@@ -120,7 +133,7 @@ public function testLegacyExtract($property, array $type = null)
120133

121134
private function doTestExtract(bool $legacy, $property, array $type = null)
122135
{
123-
$this->assertEquals($type, $this->createExtractor($legacy)->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy', $property, []));
136+
$this->assertEquals($type, $this->createExtractor($legacy)->getTypes(!class_exists(Types::class) ? 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy' : DoctrineDummy210::class, $property, []));
124137
}
125138

126139
public function testExtractWithEmbedded()
@@ -156,7 +169,7 @@ private function doTestExtractWithEmbedded(bool $legacy)
156169

157170
public function typesProvider()
158171
{
159-
return [
172+
$provider = [
160173
['id', [new Type(Type::BUILTIN_TYPE_INT)]],
161174
['guid', [new Type(Type::BUILTIN_TYPE_STRING)]],
162175
['bigint', [new Type(Type::BUILTIN_TYPE_STRING)]],
@@ -167,7 +180,7 @@ public function typesProvider()
167180
['decimal', [new Type(Type::BUILTIN_TYPE_STRING)]],
168181
['bool', [new Type(Type::BUILTIN_TYPE_BOOL)]],
169182
['binary', [new Type(Type::BUILTIN_TYPE_RESOURCE)]],
170-
['json', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)]],
183+
['jsonArray', [new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true)]],
171184
['foo', [new Type(Type::BUILTIN_TYPE_OBJECT, true, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')]],
172185
['bar', [new Type(
173186
Type::BUILTIN_TYPE_OBJECT,
@@ -206,6 +219,12 @@ public function typesProvider()
206219
)]],
207220
['indexedByCustomType', null],
208221
];
222+
223+
if (class_exists(Types::class)) {
224+
$provider[] = ['json', [new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true)]];
225+
}
226+
227+
return $provider;
209228
}
210229

211230
public function testGetPropertiesCatchException()

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class DoctrineDummy
7474
/** 6725
7575
* @Column(type="json_array")
7676
*/
77-
private $json;
77+
private $jsonArray;
7878

7979
/**
8080
* @Column(type="simple_array")

0 commit comments

Comments
 (0)
0