Skip to content

Commit 29dda5d

Browse files
authored
[3.x] Make Types strongly typed (#2945)
1 parent 377061d commit 29dda5d

29 files changed

+132
-164
lines changed

UPGRADE-3.0.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ generator to generate string UUIDs. For more efficient storage of UUIDs, use the
2323
`Doctrine\ODM\MongoDB\Types\BinaryUuidType` type in combination with the
2424
`Doctrine\ODM\MongoDB\Id\SymfonyUuidGenerator` generator.
2525

26+
## Strong typing
27+
28+
Native type hints have been introduced throughout the codebase. As a result, several
29+
methods signatures have changed, including but not limited to `Types` and `Mapping` namespaces.
30+
2631
## Metadata
2732
The `Doctrine\ODM\MongoDB\Mapping\ClassMetadata` class has been marked final and
2833
will no longer be extendable.
@@ -31,6 +36,8 @@ The `boolean`, `integer`, and `int_id` mapping types have been removed. Use the
3136
`bool`, `int`, and `int` types, respectively. These types behave exactly the
3237
same.
3338

39+
The `Int64Type` no longer extends `IntType`.
40+
3441
The `NOTIFY` change tracking policy has been removed, we suggest switching to
3542
`DEFERRED_EXPLICIT` instead. Consequentially `ClassMetadata::isChangeTrackingNotify`
3643
and `ClassMetadata::CHANGETRACKING_NOTIFY` have been removed as well. `UnitOfWork`

benchmark/BaseBench.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
abstract class BaseBench
2020
{
21-
public const DATABASE_NAME = 'doctrine_odm_performance';
22-
private const DEFAULT_MONGODB_SERVER = 'mongodb://localhost:27017';
21+
public const string DATABASE_NAME = 'doctrine_odm_performance';
22+
private const string DEFAULT_MONGODB_SERVER = 'mongodb://localhost:27017';
2323

2424
protected static DocumentManager $documentManager;
2525

src/Aggregation/Builder.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,18 @@
3333
*/
3434
class Builder
3535
{
36-
/**
37-
* The DocumentManager instance for this query
38-
*/
39-
private DocumentManager $dm;
40-
4136
/**
4237
* The ClassMetadata instance.
4338
*/
44-
private ClassMetadata $class;
39+
private readonly ClassMetadata $class;
4540

4641
/** @var class-string */
4742
private ?string $hydrationClass = null;
4843

4944
/**
5045
* The Collection instance.
5146
*/
52-
private Collection $collection;
47+
private readonly Collection $collection;
5348

5449
/** @var Stage[] */
5550
private array $stages = [];
@@ -61,9 +56,8 @@ class Builder
6156
*
6257
* @param class-string $documentName
6358
*/
64-
public function __construct(DocumentManager $dm, string $documentName)
59+
public function __construct(private readonly DocumentManager $dm, string $documentName)
6560
{
66-
$this->dm = $dm;
6761
$this->class = $this->dm->getClassMetadata($documentName);
6862
$this->collection = $this->dm->getDocumentCollection($documentName);
6963
}
@@ -212,9 +206,8 @@ public function fill(): Stage\Fill
212206
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/
213207
*
214208
* @param float|array<string, mixed>|Point $x
215-
* @param float $y
216209
*/
217-
public function geoNear($x, $y = null): Stage\GeoNear
210+
public function geoNear(float|array|Point $x, ?float $y = null): Stage\GeoNear
218211
{
219212
$stage = new Stage\GeoNear($this, $x, $y);
220213

@@ -491,7 +484,7 @@ public function redact(): Stage\Redact
491484
* @param string|mixed[]|Expr|null $expression Optional. A replacement expression that
492485
* resolves to a document.
493486
*/
494-
public function replaceRoot($expression = null): Stage\ReplaceRoot
487+
public function replaceRoot(string|array|Expr|null $expression = null): Stage\ReplaceRoot
495488
{
496489
$stage = new Stage\ReplaceRoot($this, $this->dm, $this->class, $expression);
497490

@@ -511,7 +504,7 @@ public function replaceRoot($expression = null): Stage\ReplaceRoot
511504
* @param string|mixed[]|Expr|null $expression Optional. A replacement expression that
512505
* resolves to a document.
513506
*/
514-
public function replaceWith($expression = null): Stage\ReplaceWith
507+
public function replaceWith(string|array|Expr|null $expression = null): Stage\ReplaceWith
515508
{
516509
$stage = new Stage\ReplaceWith($this, $this->dm, $this->class, $expression);
517510

@@ -608,7 +601,7 @@ public function skip(int $skip): Stage\Skip
608601
* @param int|string|null $order Field order (if one field is specified)
609602
* @phpstan-param SortShape|string $fieldName Field name or array of field/order pairs
610603
*/
611-
public function sort($fieldName, $order = null): Stage\Sort
604+
public function sort(array|string $fieldName, int|string|null $order = null): Stage\Sort
612605
{
613606
$fields = is_array($fieldName) ? $fieldName : [$fieldName => $order];
614607
// fixme: move to sort stage

src/Mapping/ClassMetadata.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,7 @@ public function getReflectionClass(): ReflectionClass
869869
return $this->reflClass;
870870
}
871871

872-
/** @param string $fieldName */
873-
public function isIdentifier($fieldName): bool
872+
public function isIdentifier(string $fieldName): bool
874873
{
875874
return $this->identifier === $fieldName;
876875
}
@@ -1036,7 +1035,7 @@ public function setLifecycleCallbacks(array $callbacks): void
10361035
*
10371036
* @param array<string, mixed>|string $fields Database field name(s)
10381037
*/
1039-
public function registerAlsoLoadMethod(string $method, $fields): void
1038+
public function registerAlsoLoadMethod(string $method, array|string $fields): void
10401039
{
10411040
$this->alsoLoadMethods[$method] = is_array($fields) ? $fields : [$fields];
10421041
}

src/Mapping/ClassMetadataFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ protected function validateIdentifier(ClassMetadata $class): void
221221
}
222222
}
223223

224-
protected function newClassMetadataInstance($className): ClassMetadata
224+
protected function newClassMetadataInstance(string $className): ClassMetadata
225225
{
226226
return new ClassMetadata($className);
227227
}

src/PersistentCollection/DefaultPersistentCollectionGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function loadClass(string $collectionClass, int $autoGenerate): string
110110
}
111111

112112
/** @param string|false $fileName Filename to write collection class code or false to eval it. */
113-
private function generateCollectionClass(string $for, string $targetFqcn, $fileName): void
113+
private function generateCollectionClass(string $for, string $targetFqcn, string|false $fileName): void
114114
{
115115
$exploded = explode('\\', $targetFqcn);
116116
$class = array_pop($exploded);

src/Types/BinDataType.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class BinDataType extends Type
2020
*/
2121
protected int $binDataType = Binary::TYPE_GENERIC;
2222

23-
/** @return Binary|null */
24-
public function convertToDatabaseValue($value)
23+
public function convertToDatabaseValue(mixed $value): ?Binary
2524
{
2625
if ($value === null) {
2726
return null;
@@ -38,8 +37,7 @@ public function convertToDatabaseValue($value)
3837
return $value;
3938
}
4039

41-
/** @return mixed|string|null */
42-
public function convertToPHPValue($value)
40+
public function convertToPHPValue(mixed $value): mixed
4341
{
4442
return $value !== null ? ($value instanceof Binary ? $value->getData() : $value) : null;
4543
}

src/Types/BooleanType.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
*/
1010
class BooleanType extends Type
1111
{
12-
/** @return bool|null */
13-
public function convertToDatabaseValue($value)
12+
public function convertToDatabaseValue(mixed $value): ?bool
1413
{
1514
return $value !== null ? (bool) $value : null;
1615
}
1716

18-
/** @return bool|null */
19-
public function convertToPHPValue($value)
17+
public function convertToPHPValue(mixed $value): ?bool
2018
{
2119
return $value !== null ? (bool) $value : null;
2220
}

src/Types/CollectionType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class CollectionType extends Type
1616
{
1717
/** @return list<mixed>|null */
18-
public function convertToDatabaseValue($value)
18+
public function convertToDatabaseValue(mixed $value): ?array
1919
{
2020
if ($value !== null && ! is_array($value)) {
2121
throw MongoDBException::invalidValueForType('Collection', ['array', 'null'], $value);
@@ -25,7 +25,7 @@ public function convertToDatabaseValue($value)
2525
}
2626

2727
/** @return list<mixed>|null */
28-
public function convertToPHPValue($value)
28+
public function convertToPHPValue(mixed $value): ?array
2929
{
3030
return $value !== null ? array_values($value) : null;
3131
}

src/Types/CustomIdType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
*/
1010
class CustomIdType extends Type
1111
{
12-
public function convertToDatabaseValue($value)
12+
public function convertToDatabaseValue(mixed $value): mixed
1313
{
1414
return $value;
1515
}
1616

17-
public function convertToPHPValue($value)
17+
public function convertToPHPValue(mixed $value): mixed
1818
{
1919
return $value;
2020
}

0 commit comments

Comments
 (0)