Skip to content

Commit 260a585

Browse files
committed
Make Types strongly typed, update benchmarks
1 parent dc718ce commit 260a585

31 files changed

+138
-173
lines changed

benchmark/BaseBench.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@
99
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
1010
use MongoDB\Client;
1111
use MongoDB\Model\DatabaseInfo;
12-
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
1312
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1413

1514
use function array_map;
1615
use function getenv;
1716
use function in_array;
1817
use function iterator_to_array;
1918

20-
/** @BeforeMethods({"initDocumentManager", "clearDatabase"}) */
2119
abstract class BaseBench
2220
{
23-
public const DATABASE_NAME = 'doctrine_odm_performance';
24-
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';
2523

26-
/** @var DocumentManager */
27-
protected static $documentManager;
24+
protected static DocumentManager $documentManager;
2825

2926
protected function getDocumentManager(): DocumentManager
3027
{

benchmark/Document/HydrateDocumentBench.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,28 @@
99
use Documents\User;
1010
use MongoDB\BSON\ObjectId;
1111
use MongoDB\BSON\UTCDateTime;
12-
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
13-
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
12+
use PhpBench\Attributes\BeforeMethods;
13+
use PhpBench\Attributes\Warmup;
1414

15-
/** @BeforeMethods({"init"}, extend=true) */
15+
#[BeforeMethods(['initDocumentManager', 'clearDatabase', 'init'])]
1616
final class HydrateDocumentBench extends BaseBench
1717
{
1818
/** @var array<string, mixed> */
19-
private static $data;
19+
private static array $data;
2020

2121
/** @var array<string, mixed> */
22-
private static $embedOneData;
22+
private static array $embedOneData;
2323

2424
/** @var array<string, mixed[]> */
25-
private static $embedManyData;
25+
private static array $embedManyData;
2626

2727
/** @var array<string, mixed[]> */
28-
private static $referenceOneData;
28+
private static array $referenceOneData;
2929

3030
/** @var array<string, mixed[]> */
31-
private static $referenceManyData;
31+
private static array $referenceManyData;
3232

33-
/** @var HydratorInterface */
34-
private static $hydrator;
33+
private static HydratorInterface $hydrator;
3534

3635
public function init(): void
3736
{
@@ -78,31 +77,31 @@ public function init(): void
7877
->getHydratorFor(User::class);
7978
}
8079

81-
/** @Warmup(2) */
80+
#[Warmup(2)]
8281
public function benchHydrateDocument(): void
8382
{
8483
self::$hydrator->hydrate(new User(), self::$data);
8584
}
8685

87-
/** @Warmup(2) */
86+
#[Warmup(2)]
8887
public function benchHydrateDocumentWithEmbedOne(): void
8988
{
9089
self::$hydrator->hydrate(new User(), self::$data + self::$embedOneData);
9190
}
9291

93-
/** @Warmup(2) */
92+
#[Warmup(2)]
9493
public function benchHydrateDocumentWithEmbedMany(): void
9594
{
9695
self::$hydrator->hydrate(new User(), self::$data + self::$embedManyData);
9796
}
9897

99-
/** @Warmup(2) */
98+
#[Warmup(2)]
10099
public function benchHydrateDocumentWithReferenceOne(): void
101100
{
102101
self::$hydrator->hydrate(new User(), self::$data + self::$referenceOneData);
103102
}
104103

105-
/** @Warmup(2) */
104+
#[Warmup(2)]
106105
public function benchHydrateDocumentWithReferenceMany(): void
107106
{
108107
self::$hydrator->hydrate(new User(), self::$data + self::$referenceManyData);

benchmark/Document/LoadDocumentBench.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@
1212
use Documents\Phonenumber;
1313
use Documents\User;
1414
use MongoDB\BSON\ObjectId;
15-
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
16-
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
15+
use PhpBench\Attributes\BeforeMethods;
16+
use PhpBench\Attributes\Warmup;
1717

1818
use function assert;
1919

20-
/** @BeforeMethods({"init"}, extend=true) */
20+
#[BeforeMethods(['initDocumentManager', 'clearDatabase', 'init'])]
2121
final class LoadDocumentBench extends BaseBench
2222
{
23-
/** @var ObjectId */
24-
private static $userId;
23+
private static ObjectId $userId;
2524

2625
public function init(): void
2726
{
@@ -53,42 +52,41 @@ public function init(): void
5352
$this->getDocumentManager()->clear();
5453
}
5554

56-
/** @Warmup(2) */
55+
#[Warmup(2)]
5756
public function benchLoadDocument(): void
5857
{
5958
$this->loadDocument();
6059
}
6160

62-
/** @Warmup(2) */
61+
#[Warmup(2)]
6362
public function benchLoadEmbedOne(): void
6463
{
6564
$this->loadDocument()->getAddress()->getCity();
6665
}
6766

68-
/** @Warmup(2) */
67+
#[Warmup(2)]
6968
public function benchLoadEmbedMany(): void
7069
{
7170
$this->loadDocument()->getPhonenumbers()->forAll(static function (int $key, Phonenumber $element) {
7271
return $element->getPhoneNumber() !== null;
7372
});
7473
}
7574

76-
/** @Warmup(2) */
75+
#[Warmup(2)]
7776
public function benchLoadReferenceOne(): void
7877
{
7978
$this->loadDocument()->getAccount()->getName();
8079
}
8180

82-
/** @Warmup(2) */
81+
#[Warmup(2)]
8382
public function benchLoadReferenceMany(): void
8483
{
8584
$this->loadDocument()->getGroups()->forAll(static function (int $key, Group $group) {
8685
return $group->getName() !== null;
8786
});
8887
}
8988

90-
/** @return User */
91-
private function loadDocument()
89+
private function loadDocument(): User
9290
{
9391
$document = $this->getDocumentManager()->find(User::class, self::$userId);
9492
assert($document instanceof User);

benchmark/Document/StoreDocumentBench.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
use Documents\Group;
1212
use Documents\Phonenumber;
1313
use Documents\User;
14-
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
14+
use PhpBench\Attributes\BeforeMethods;
15+
use PhpBench\Attributes\Warmup;
1516

17+
#[BeforeMethods(['initDocumentManager', 'clearDatabase'])]
1618
final class StoreDocumentBench extends BaseBench
1719
{
18-
/** @Warmup(2) */
20+
#[Warmup(2)]
1921
public function benchStoreDocument(): void
2022
{
2123
$user = new User();
@@ -27,7 +29,7 @@ public function benchStoreDocument(): void
2729
$this->getDocumentManager()->clear();
2830
}
2931

30-
/** @Warmup(2) */
32+
#[Warmup(2)]
3133
public function benchStoreDocumentWithEmbedOne(): void
3234
{
3335
$address = new Address();
@@ -44,7 +46,7 @@ public function benchStoreDocumentWithEmbedOne(): void
4446
$this->getDocumentManager()->clear();
4547
}
4648

47-
/** @Warmup(2) */
49+
#[Warmup(2)]
4850
public function benchStoreDocumentWithEmbedMany(): void
4951
{
5052
$user = new User();
@@ -58,7 +60,7 @@ public function benchStoreDocumentWithEmbedMany(): void
5860
$this->getDocumentManager()->clear();
5961
}
6062

61-
/** @Warmup(2) */
63+
#[Warmup(2)]
6264
public function benchStoreDocumentWithReferenceOne(): void
6365
{
6466
$account = new Account();
@@ -74,7 +76,7 @@ public function benchStoreDocumentWithReferenceOne(): void
7476
$this->getDocumentManager()->clear();
7577
}
7678

77-
/** @Warmup(2) */
79+
#[Warmup(2)]
7880
public function benchStoreDocumentWithReferenceMany(): void
7981
{
8082
$group1 = new Group('One');

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
@@ -867,8 +867,7 @@ public function getReflectionClass(): ReflectionClass
867867
return $this->reflClass;
868868
}
869869

870-
/** @param string $fieldName */
871-
public function isIdentifier($fieldName): bool
870+
public function isIdentifier(string $fieldName): bool
872871
{
873872
return $this->identifier === $fieldName;
874873
}
@@ -1034,7 +1033,7 @@ public function setLifecycleCallbacks(array $callbacks): void
10341033
*
10351034
* @param array<string, mixed>|string $fields Database field name(s)
10361035
*/
1037-
public function registerAlsoLoadMethod(string $method, $fields): void
1036+
public function registerAlsoLoadMethod(string $method, array|string $fields): void
10381037
{
10391038
$this->alsoLoadMethods[$method] = is_array($fields) ? $fields : [$fields];
10401039
}

src/Mapping/ClassMetadataFactory.php

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

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

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class BinDataType extends Type
2020
*/
2121
protected int $binDataType = Binary::TYPE_GENERIC;
2222

23-
public function convertToDatabaseValue($value)
23+
public function convertToDatabaseValue(mixed $value): ?Binary
2424
{
2525
if ($value === null) {
2626
return null;
@@ -37,7 +37,7 @@ public function convertToDatabaseValue($value)
3737
return $value;
3838
}
3939

40-
public function convertToPHPValue($value)
40+
public function convertToPHPValue(mixed $value): mixed
4141
{
4242
return $value !== null ? ($value instanceof Binary ? $value->getData() : $value) : null;
4343
}

src/Types/BooleanType.php

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

17-
public function convertToPHPValue($value)
17+
public function convertToPHPValue(mixed $value): ?bool
1818
{
1919
return $value !== null ? (bool) $value : null;
2020
}

0 commit comments

Comments
 (0)