Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions benchmark/BaseBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@
use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver;
use MongoDB\Client;
use MongoDB\Model\DatabaseInfo;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

use function array_map;
use function getenv;
use function in_array;
use function iterator_to_array;

/** @BeforeMethods({"initDocumentManager", "clearDatabase"}) */
abstract class BaseBench
{
public const DATABASE_NAME = 'doctrine_odm_performance';
private const DEFAULT_MONGODB_SERVER = 'mongodb://localhost:27017';
public const string DATABASE_NAME = 'doctrine_odm_performance';
private const string DEFAULT_MONGODB_SERVER = 'mongodb://localhost:27017';

/** @var DocumentManager */
protected static $documentManager;
protected static DocumentManager $documentManager;

protected function getDocumentManager(): DocumentManager
{
Expand Down
29 changes: 14 additions & 15 deletions benchmark/Document/HydrateDocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,28 @@
use Documents\User;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use PhpBench\Attributes\BeforeMethods;
use PhpBench\Attributes\Warmup;

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

/** @var array<string, mixed> */
private static $embedOneData;
private static array $embedOneData;

/** @var array<string, mixed[]> */
private static $embedManyData;
private static array $embedManyData;

/** @var array<string, mixed[]> */
private static $referenceOneData;
private static array $referenceOneData;

/** @var array<string, mixed[]> */
private static $referenceManyData;
private static array $referenceManyData;

/** @var HydratorInterface */
private static $hydrator;
private static HydratorInterface $hydrator;

public function init(): void
{
Expand Down Expand Up @@ -78,31 +77,31 @@ public function init(): void
->getHydratorFor(User::class);
}

/** @Warmup(2) */
#[Warmup(2)]
public function benchHydrateDocument(): void
{
self::$hydrator->hydrate(new User(), self::$data);
}

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

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

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

/** @Warmup(2) */
#[Warmup(2)]
public function benchHydrateDocumentWithReferenceMany(): void
{
self::$hydrator->hydrate(new User(), self::$data + self::$referenceManyData);
Expand Down
22 changes: 10 additions & 12 deletions benchmark/Document/LoadDocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
use Documents\Phonenumber;
use Documents\User;
use MongoDB\BSON\ObjectId;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use PhpBench\Attributes\BeforeMethods;
use PhpBench\Attributes\Warmup;

use function assert;

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

public function init(): void
{
Expand Down Expand Up @@ -53,42 +52,41 @@ public function init(): void
$this->getDocumentManager()->clear();
}

/** @Warmup(2) */
#[Warmup(2)]
public function benchLoadDocument(): void
{
$this->loadDocument();
}

/** @Warmup(2) */
#[Warmup(2)]
public function benchLoadEmbedOne(): void
{
$this->loadDocument()->getAddress()->getCity();
}

/** @Warmup(2) */
#[Warmup(2)]
public function benchLoadEmbedMany(): void
{
$this->loadDocument()->getPhonenumbers()->forAll(static function (int $key, Phonenumber $element) {
return $element->getPhoneNumber() !== null;
});
}

/** @Warmup(2) */
#[Warmup(2)]
public function benchLoadReferenceOne(): void
{
$this->loadDocument()->getAccount()->getName();
}

/** @Warmup(2) */
#[Warmup(2)]
public function benchLoadReferenceMany(): void
{
$this->loadDocument()->getGroups()->forAll(static function (int $key, Group $group) {
return $group->getName() !== null;
});
}

/** @return User */
private function loadDocument()
private function loadDocument(): User
{
$document = $this->getDocumentManager()->find(User::class, self::$userId);
assert($document instanceof User);
Expand Down
14 changes: 8 additions & 6 deletions benchmark/Document/StoreDocumentBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use Documents\Group;
use Documents\Phonenumber;
use Documents\User;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use PhpBench\Attributes\BeforeMethods;
use PhpBench\Attributes\Warmup;

#[BeforeMethods(['initDocumentManager', 'clearDatabase'])]
final class StoreDocumentBench extends BaseBench
{
/** @Warmup(2) */
#[Warmup(2)]
public function benchStoreDocument(): void
{
$user = new User();
Expand All @@ -27,7 +29,7 @@ public function benchStoreDocument(): void
$this->getDocumentManager()->clear();
}

/** @Warmup(2) */
#[Warmup(2)]
public function benchStoreDocumentWithEmbedOne(): void
{
$address = new Address();
Expand All @@ -44,7 +46,7 @@ public function benchStoreDocumentWithEmbedOne(): void
$this->getDocumentManager()->clear();
}

/** @Warmup(2) */
#[Warmup(2)]
public function benchStoreDocumentWithEmbedMany(): void
{
$user = new User();
Expand All @@ -58,7 +60,7 @@ public function benchStoreDocumentWithEmbedMany(): void
$this->getDocumentManager()->clear();
}

/** @Warmup(2) */
#[Warmup(2)]
public function benchStoreDocumentWithReferenceOne(): void
{
$account = new Account();
Expand All @@ -74,7 +76,7 @@ public function benchStoreDocumentWithReferenceOne(): void
$this->getDocumentManager()->clear();
}

/** @Warmup(2) */
#[Warmup(2)]
public function benchStoreDocumentWithReferenceMany(): void
{
$group1 = new Group('One');
Expand Down
21 changes: 7 additions & 14 deletions src/Aggregation/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,18 @@
*/
class Builder
{
/**
* The DocumentManager instance for this query
*/
private DocumentManager $dm;

/**
* The ClassMetadata instance.
*/
private ClassMetadata $class;
private readonly ClassMetadata $class;

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

/**
* The Collection instance.
*/
private Collection $collection;
private readonly Collection $collection;

/** @var Stage[] */
private array $stages = [];
Expand All @@ -61,9 +56,8 @@ class Builder
*
* @param class-string $documentName
*/
public function __construct(DocumentManager $dm, string $documentName)
public function __construct(private readonly DocumentManager $dm, string $documentName)
{
$this->dm = $dm;
$this->class = $this->dm->getClassMetadata($documentName);
$this->collection = $this->dm->getDocumentCollection($documentName);
}
Expand Down Expand Up @@ -212,9 +206,8 @@ public function fill(): Stage\Fill
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/
*
* @param float|array<string, mixed>|Point $x
* @param float $y
*/
public function geoNear($x, $y = null): Stage\GeoNear
public function geoNear(float|array|Point $x, ?float $y = null): Stage\GeoNear
{
$stage = new Stage\GeoNear($this, $x, $y);

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

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

Expand Down Expand Up @@ -608,7 +601,7 @@ public function skip(int $skip): Stage\Skip
* @param int|string|null $order Field order (if one field is specified)
* @phpstan-param SortShape|string $fieldName Field name or array of field/order pairs
*/
public function sort($fieldName, $order = null): Stage\Sort
public function sort(array|string $fieldName, int|string|null $order = null): Stage\Sort
{
$fields = is_array($fieldName) ? $fieldName : [$fieldName => $order];
// fixme: move to sort stage
Expand Down
5 changes: 2 additions & 3 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,7 @@ public function getReflectionClass(): ReflectionClass
return $this->reflClass;
}

/** @param string $fieldName */
public function isIdentifier($fieldName): bool
public function isIdentifier(string $fieldName): bool
{
return $this->identifier === $fieldName;
}
Expand Down Expand Up @@ -1034,7 +1033,7 @@ public function setLifecycleCallbacks(array $callbacks): void
*
* @param array<string, mixed>|string $fields Database field name(s)
*/
public function registerAlsoLoadMethod(string $method, $fields): void
public function registerAlsoLoadMethod(string $method, array|string $fields): void
{
$this->alsoLoadMethods[$method] = is_array($fields) ? $fields : [$fields];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ protected function validateIdentifier(ClassMetadata $class): void
}
}

protected function newClassMetadataInstance($className): ClassMetadata
protected function newClassMetadataInstance(string $className): ClassMetadata
{
return new ClassMetadata($className);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function loadClass(string $collectionClass, int $autoGenerate): string
}

/** @param string|false $fileName Filename to write collection class code or false to eval it. */
private function generateCollectionClass(string $for, string $targetFqcn, $fileName): void
private function generateCollectionClass(string $for, string $targetFqcn, string|false $fileName): void
{
$exploded = explode('\\', $targetFqcn);
$class = array_pop($exploded);
Expand Down
4 changes: 2 additions & 2 deletions src/Types/BinDataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BinDataType extends Type
*/
protected int $binDataType = Binary::TYPE_GENERIC;

public function convertToDatabaseValue($value)
public function convertToDatabaseValue(mixed $value): ?Binary
{
if ($value === null) {
return null;
Expand All @@ -37,7 +37,7 @@ public function convertToDatabaseValue($value)
return $value;
}

public function convertToPHPValue($value)
public function convertToPHPValue(mixed $value): mixed
{
return $value !== null ? ($value instanceof Binary ? $value->getData() : $value) : null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Types/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
*/
class BooleanType extends Type
{
public function convertToDatabaseValue($value)
public function convertToDatabaseValue(mixed $value): ?bool
{
return $value !== null ? (bool) $value : null;
}

public function convertToPHPValue($value)
public function convertToPHPValue(mixed $value): ?bool
{
return $value !== null ? (bool) $value : null;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Types/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
*/
class CollectionType extends Type
{
public function convertToDatabaseValue($value)
/** @return list<mixed> */
public function convertToDatabaseValue(mixed $value): ?array
{
if ($value !== null && ! is_array($value)) {
throw MongoDBException::invalidValueForType('Collection', ['array', 'null'], $value);
Expand All @@ -23,7 +24,8 @@ public function convertToDatabaseValue($value)
return $value !== null ? array_values($value) : null;
}

public function convertToPHPValue($value)
/** @return list<mixed> */
public function convertToPHPValue(mixed $value): ?array
{
return $value !== null ? array_values($value) : null;
}
Expand Down
Loading