Skip to content

Commit 2b8f4ab

Browse files
committed
Remove Type::closureToPhp optimization
1 parent 29dda5d commit 2b8f4ab

24 files changed

+35
-384
lines changed

src/Hydrator/HydratorFactory.php

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Doctrine\ODM\MongoDB\Events;
1313
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
1414
use Doctrine\ODM\MongoDB\Proxy\InternalProxy;
15-
use Doctrine\ODM\MongoDB\Types\Type;
1615
use Doctrine\ODM\MongoDB\UnitOfWork;
1716
use ProxyManager\Proxy\GhostObjectInterface;
1817

@@ -165,14 +164,14 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
165164
if (isset($mapping['alsoLoadFields'])) {
166165
foreach ($mapping['alsoLoadFields'] as $name) {
167166
$code .= sprintf(
168-
<<<EOF
167+
<<<'PHP'
169168
170-
// AlsoLoad("$name")
171-
if (! array_key_exists('%1\$s', \$data) && array_key_exists('$name', \$data)) {
172-
\$data['%1\$s'] = \$data['$name'];
169+
// AlsoLoad("%1$s")
170+
if (! array_key_exists('%1$s', $data) && array_key_exists('$name', $data)) {
171+
$data['%1$s'] = $data['$name'];
173172
}
174173

175-
EOF
174+
PHP
176175
,
177176
$mapping['name'],
178177
);
@@ -181,48 +180,48 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
181180

182181
if ($mapping['type'] === 'date') {
183182
$code .= sprintf(
184-
<<<'EOF'
183+
<<<'PHP'
185184
186185
// Field(type: "date")
187186
if (array_key_exists('%1$s', $data) && ($data['%1$s'] !== null || ($this->class->fieldMappings['%2$s']['nullable'] ?? false))) {
188187
$value = $data['%1$s'];
189-
%3$s
188+
$return = Type::getType('%3$s')->convertToPHPValue($value);
190189
$this->class->propertyAccessors['%2$s']->setValue($document, $return === null ? null : clone $return);
191190
$hydratedData['%2$s'] = $return;
192191
}
193192

194-
EOF
193+
PHP
195194
,
196195
$mapping['name'],
197196
$mapping['fieldName'],
198-
Type::getType($mapping['type'])->closureToPHP(),
197+
$mapping['type'],
199198
);
200199
} elseif (! isset($mapping['association'])) {
201200
$code .= sprintf(
202-
<<<EOF
203-
204-
// Field(type: "{$mapping['type']}")
205-
if (isset(\$data['%1\$s']) || (! empty(\$this->class->fieldMappings['%2\$s']['nullable']) && array_key_exists('%1\$s', \$data))) {
206-
\$value = \$data['%1\$s'];
207-
if (\$value !== null) {
208-
\$typeIdentifier = \$this->class->fieldMappings['%2\$s']['type'];
209-
%3\$s
201+
<<<'PHP'
202+
203+
// Field(type: "%3$s")
204+
if (isset($data['%1$s']) || (! empty($this->class->fieldMappings['%2$s']['nullable']) && array_key_exists('%1$s', $data))) {
205+
$value = $data['%1$s'];
206+
if ($value !== null) {
207+
$typeIdentifier = $this->class->fieldMappings['%2$s']['type'];
208+
$return = Type::getType('%3$s')->convertToPHPValue($value);
210209
} else {
211-
\$return = null;
210+
$return = null;
212211
}
213-
\$this->class->propertyAccessors['%2\$s']->setValue(\$document, \$return);
214-
\$hydratedData['%2\$s'] = \$return;
212+
$this->class->propertyAccessors['%2$s']->setValue($document, $return);
213+
$hydratedData['%2$s'] = $return;
215214
}
216215

217-
EOF
216+
PHP
218217
,
219218
$mapping['name'],
220219
$mapping['fieldName'],
221-
Type::getType($mapping['type'])->closureToPHP(),
220+
$mapping['type'],
222221
);
223222
} elseif ($mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isOwningSide']) {
224223
$code .= sprintf(
225-
<<<'EOF'
224+
<<<'PHP'
226225
227226
// ReferenceOne
228227
if (isset($data['%1$s']) || (! empty($this->class->fieldMappings['%2$s']['nullable']) && array_key_exists('%1$s', $data))) {
@@ -243,7 +242,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
243242
$hydratedData['%2$s'] = $return;
244243
}
245244

246-
EOF
245+
PHP
247246
,
248247
$mapping['name'],
249248
$mapping['fieldName'],
@@ -252,22 +251,22 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
252251
} elseif ($mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isInverseSide']) {
253252
if (isset($mapping['repositoryMethod']) && $mapping['repositoryMethod']) {
254253
$code .= sprintf(
255-
<<<'EOF'
254+
<<<'PHP'
256255
257256
$className = $this->class->fieldMappings['%2$s']['targetDocument'];
258257
$return = $this->dm->getRepository($className)->%3$s($document);
259258
$this->class->propertyAccessors['%2$s']->setValue($document, $return);
260259
$hydratedData['%2$s'] = $return;
261260

262-
EOF
261+
PHP
263262
,
264263
$mapping['name'],
265264
$mapping['fieldName'],
266265
$mapping['repositoryMethod'],
267266
);
268267
} else {
269268
$code .= sprintf(
270-
<<<'EOF'
269+
<<<'PHP'
271270
272271
$mapping = $this->class->fieldMappings['%2$s'];
273272
$className = $mapping['targetDocument'];
@@ -283,15 +282,15 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
283282
$this->class->propertyAccessors['%2$s']->setValue($document, $return);
284283
$hydratedData['%2$s'] = $return;
285284

286-
EOF
285+
PHP
287286
,
288287
$mapping['name'],
289288
$mapping['fieldName'],
290289
);
291290
}
292291
} elseif ($mapping['association'] === ClassMetadata::REFERENCE_MANY || $mapping['association'] === ClassMetadata::EMBED_MANY) {
293292
$code .= sprintf(
294-
<<<'EOF'
293+
<<<'PHP'
295294
296295
// %4$s
297296
$mongoData = $data['%1$s'] ?? null;
@@ -310,7 +309,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
310309
$this->class->propertyAccessors['%2$s']->setValue($document, $return);
311310
$hydratedData['%2$s'] = $return;
312311

313-
EOF
312+
PHP
314313
,
315314
$mapping['name'],
316315
$mapping['fieldName'],
@@ -319,7 +318,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
319318
);
320319
} elseif ($mapping['association'] === ClassMetadata::EMBED_ONE) {
321320
$code .= sprintf(
322-
<<<'EOF'
321+
<<<'PHP'
323322
324323
// EmbedOne
325324
if (isset($data['%1$s']) || (! empty($this->class->fieldMappings['%2$s']['nullable']) && array_key_exists('%1$s', $data))) {
@@ -349,7 +348,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
349348
$hydratedData['%2$s'] = $return;
350349
}
351350

352-
EOF
351+
PHP
353352
,
354353
$mapping['name'],
355354
$mapping['fieldName'],
@@ -360,7 +359,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
360359

361360
$namespace = $this->hydratorNamespace;
362361
$code = sprintf(
363-
<<<EOF
362+
<<<PHP
364363
<?php
365364
366365
namespace $namespace;
@@ -370,6 +369,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla
370369
use Doctrine\ODM\MongoDB\Hydrator\HydratorInterface;
371370
use Doctrine\ODM\MongoDB\Query\Query;
372371
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
372+
use Doctrine\ODM\MongoDB\Types\Type;
373373
374374
use function array_key_exists;
375375
use function gettype;
@@ -389,7 +389,7 @@ public function hydrate(object \$document, array \$data, array \$hints = []): ar
389389
return \$hydratedData;
390390
}
391391
}
392-
EOF
392+
PHP
393393
,
394394
$code,
395395
);

src/Types/AbstractVectorType.php

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use function get_debug_type;
1313
use function is_array;
1414
use function sprintf;
15-
use function str_replace;
1615

1716
/** @internal */
1817
abstract class AbstractVectorType extends Type
@@ -72,43 +71,5 @@ public function convertToPHPValue(mixed $value): ?array
7271
return $value->toArray();
7372
}
7473

75-
public function closureToMongo(): string
76-
{
77-
return str_replace('%%vectorType%%', $this->getVectorType()->name, <<<'PHP'
78-
if ($value === null) {
79-
$return = null;
80-
} elseif (\is_array($value)) {
81-
$return = \MongoDB\BSON\Binary::fromVector($value, \MongoDB\BSON\VectorType::%%vectorType%%);
82-
} elseif (! $value instanceof \MongoDB\BSON\Binary) {
83-
throw new InvalidArgumentException(sprintf('Invalid data type %s received for vector field, expected null, array or MongoDB\BSON\Binary', get_debug_type($value)));
84-
} elseif ($value->getType() !== \MongoDB\BSON\Binary::TYPE_VECTOR) {
85-
throw new InvalidArgumentException(sprintf('Invalid binary data of type %d received for vector field, expected binary type %d', $value->getType(), \MongoDB\BSON\Binary::TYPE_VECTOR));
86-
} elseif ($value->getVectorType() !== \MongoDB\BSON\VectorType::%%vectorType%%) {
87-
throw new \InvalidArgumentException(sprintf('Invalid binary vector data of vector type %s received for vector field, expected vector type %%vectorType%%', $value->getVectorType()->name));
88-
} else {
89-
$return = $value;
90-
}
91-
PHP);
92-
}
93-
94-
public function closureToPHP(): string
95-
{
96-
return str_replace('%%vectorType%%', $this->getVectorType()->name, <<<'PHP'
97-
if ($value === null) {
98-
$return = null;
99-
} elseif (\is_array($value)) {
100-
$return = $value;
101-
} elseif (! $value instanceof \MongoDB\BSON\Binary) {
102-
throw new \InvalidArgumentException(sprintf('Invalid data of type "%s" received for vector field', get_debug_type($value)));
103-
} elseif ($value->getType() !== \MongoDB\BSON\Binary::TYPE_VECTOR) {
104-
throw new \InvalidArgumentException(sprintf('Invalid binary data of type %d received for vector field', $value->getType()));
105-
} elseif ($value->getVectorType() !== \MongoDB\BSON\VectorType::%%vectorType%%) {
106-
throw new \InvalidArgumentException(sprintf('Invalid binary vector data of vector type %s received for vector field, expected vector type %%vectorType%%', $value->getVectorType()->name));
107-
} else {
108-
$return = $value->toArray();
109-
}
110-
PHP);
111-
}
112-
11374
abstract protected function getVectorType(): VectorType;
11475
}

src/Types/BinDataType.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
use MongoDB\BSON\Binary;
88

9-
use function sprintf;
10-
119
/**
1210
* The BinData type for generic data.
1311
*/
@@ -41,14 +39,4 @@ public function convertToPHPValue(mixed $value): mixed
4139
{
4240
return $value !== null ? ($value instanceof Binary ? $value->getData() : $value) : null;
4341
}
44-
45-
public function closureToMongo(): string
46-
{
47-
return sprintf('$return = $value !== null ? new \MongoDB\BSON\Binary($value, %d) : null;', $this->binDataType);
48-
}
49-
50-
public function closureToPHP(): string
51-
{
52-
return '$return = $value !== null ? ($value instanceof \MongoDB\BSON\Binary ? $value->getData() : $value) : null;';
53-
}
5442
}

src/Types/BinaryUuidType.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,4 @@ public function convertToPHPValue(mixed $value): Uuid
4141

4242
return Uuid::fromBinary($value->getData());
4343
}
44-
45-
public function closureToMongo(): string
46-
{
47-
return <<<'PHP'
48-
$return = match (true) {
49-
$value === null => null,
50-
$value instanceof \MongoDB\BSON\Binary => $value,
51-
$value instanceof \Symfony\Component\Uid\Uuid => new \MongoDB\BSON\Binary($value->toBinary(), \MongoDB\BSON\Binary::TYPE_UUID),
52-
is_string($value) => new \MongoDB\BSON\Binary(\Symfony\Component\Uid\Uuid::fromString($value)->toBinary(), \MongoDB\BSON\Binary::TYPE_UUID),
53-
default => throw new \InvalidArgumentException(sprintf('Invalid data type %s received for UUID', get_debug_type($value))),
54-
};
55-
PHP;
56-
}
57-
58-
public function closureToPHP(): string
59-
{
60-
return <<<'PHP'
61-
if ($value instanceof \Symfony\Component\Uid\Uuid) {
62-
$return = $value;
63-
} elseif (! $value instanceof \MongoDB\BSON\Binary) {
64-
throw new \InvalidArgumentException(sprintf('Invalid data of type "%s" received for Uuid', get_debug_type($value)));
65-
} elseif ($value->getType() !== \MongoDB\BSON\Binary::TYPE_UUID) {
66-
throw new \InvalidArgumentException(sprintf('Invalid binary data of type %d received for Uuid', $value->getType()));
67-
} else {
68-
$return = \Symfony\Component\Uid\Uuid::fromBinary($value->getData());
69-
}
70-
PHP;
71-
}
7244
}

src/Types/BooleanType.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,4 @@ public function convertToPHPValue(mixed $value): ?bool
1818
{
1919
return $value !== null ? (bool) $value : null;
2020
}
21-
22-
public function closureToMongo(): string
23-
{
24-
return '$return = (bool) $value;';
25-
}
26-
27-
public function closureToPHP(): string
28-
{
29-
return '$return = (bool) $value;';
30-
}
3121
}

src/Types/ClosureToPHP.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Types/CustomIdType.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,4 @@ public function convertToPHPValue(mixed $value): mixed
1818
{
1919
return $value;
2020
}
21-
22-
public function closureToMongo(): string
23-
{
24-
return '$return = $value;';
25-
}
26-
27-
public function closureToPHP(): string
28-
{
29-
return '$return = $value;';
30-
}
3121
}

src/Types/DateType.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,6 @@ public function convertToPHPValue(mixed $value): ?DateTimeInterface
101101
return static::getDateTime($value);
102102
}
103103

104-
public function closureToMongo(): string
105-
{
106-
return 'if ($value === null || $value instanceof \MongoDB\BSON\UTCDateTime) { $return = $value; } else { $datetime = \\' . static::class . '::getDateTime($value); $return = new \MongoDB\BSON\UTCDateTime($datetime); }';
107-
}
108-
109-
public function closureToPHP(): string
110-
{
111-
return 'if ($value === null) { $return = null; } else { $return = \\' . static::class . '::getDateTime($value); }';
112-
}
113-
114104
public function getNextVersion(mixed $current): DateTimeInterface
115105
{
116106
return new DateTime();

src/Types/Decimal128Type.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
class Decimal128Type extends Type implements Incrementable, Versionable
1313
{
14-
use ClosureToPHP;
15-
1614
public function convertToDatabaseValue(mixed $value): ?Decimal128
1715
{
1816
if ($value === null) {

0 commit comments

Comments
 (0)