From f378460eb7a8db89ea0407e5ad9c4c60ced374c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 30 Sep 2025 14:45:07 +0200 Subject: [PATCH 1/2] Use null coalescing operator --- lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php | 18 +++++++--------- .../ODM/MongoDB/Mapping/ClassMetadata.php | 15 +++++-------- .../Resolver/CachingClassNameResolver.php | 6 +----- lib/Doctrine/ODM/MongoDB/Query/Builder.php | 21 ++++++------------- lib/Doctrine/ODM/MongoDB/Query/Expr.php | 18 +++------------- 5 files changed, 23 insertions(+), 55 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php index 03bf828d8a..8a4f3ca2aa 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php @@ -122,11 +122,10 @@ public function add($expression1, $expression2, ...$expressions): static */ public function addAnd($expression, ...$expressions): static { - if (! isset($this->expr['$and'])) { - $this->expr['$and'] = []; - } - - $this->expr['$and'] = array_merge($this->expr['$and'], array_map([$this, 'prepareArgument'], func_get_args())); + $this->expr['$and'] = array_merge( + $this->expr['$and'] ?? [], + array_map([$this, 'prepareArgument'], func_get_args()), + ); return $this; } @@ -141,11 +140,10 @@ public function addAnd($expression, ...$expressions): static */ public function addOr($expression, ...$expressions): static { - if (! isset($this->expr['$or'])) { - $this->expr['$or'] = []; - } - - $this->expr['$or'] = array_merge($this->expr['$or'], array_map([$this, 'prepareArgument'], func_get_args())); + $this->expr['$or'] = array_merge( + $this->expr['$or'] ?? [], + array_map([$this, 'prepareArgument'], func_get_args()), + ); return $this; } diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php index ac75de1029..ee3eb385ee 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php @@ -1839,8 +1839,7 @@ public function isCollectionValuedAssociation($fieldName): bool */ public function isSingleValuedReference(string $fieldName): bool { - return isset($this->fieldMappings[$fieldName]['association']) && - $this->fieldMappings[$fieldName]['association'] === self::REFERENCE_ONE; + return ($this->fieldMappings[$fieldName]['association'] ?? null) === self::REFERENCE_ONE; } /** @@ -1849,8 +1848,7 @@ public function isSingleValuedReference(string $fieldName): bool */ public function isCollectionValuedReference(string $fieldName): bool { - return isset($this->fieldMappings[$fieldName]['association']) && - $this->fieldMappings[$fieldName]['association'] === self::REFERENCE_MANY; + return ($this->fieldMappings[$fieldName]['association'] ?? null) === self::REFERENCE_MANY; } /** @@ -1859,8 +1857,7 @@ public function isCollectionValuedReference(string $fieldName): bool */ public function isSingleValuedEmbed(string $fieldName): bool { - return isset($this->fieldMappings[$fieldName]['association']) && - $this->fieldMappings[$fieldName]['association'] === self::EMBED_ONE; + return ($this->fieldMappings[$fieldName]['association'] ?? null) === self::EMBED_ONE; } /** @@ -1869,8 +1866,7 @@ public function isSingleValuedEmbed(string $fieldName): bool */ public function isCollectionValuedEmbed(string $fieldName): bool { - return isset($this->fieldMappings[$fieldName]['association']) && - $this->fieldMappings[$fieldName]['association'] === self::EMBED_MANY; + return ($this->fieldMappings[$fieldName]['association'] ?? null) === self::EMBED_MANY; } /** @@ -2279,8 +2275,7 @@ public function getAssociationNames(): array /** @param string $fieldName */ public function getTypeOfField($fieldName): ?string { - return isset($this->fieldMappings[$fieldName]) ? - $this->fieldMappings[$fieldName]['type'] : null; + return $this->fieldMappings[$fieldName]['type'] ?? null; } /** diff --git a/lib/Doctrine/ODM/MongoDB/Proxy/Resolver/CachingClassNameResolver.php b/lib/Doctrine/ODM/MongoDB/Proxy/Resolver/CachingClassNameResolver.php index 8c2879ed34..cd0343301c 100644 --- a/lib/Doctrine/ODM/MongoDB/Proxy/Resolver/CachingClassNameResolver.php +++ b/lib/Doctrine/ODM/MongoDB/Proxy/Resolver/CachingClassNameResolver.php @@ -26,10 +26,6 @@ public function getRealClass(string $class): string public function resolveClassName(string $className): string { - if (! isset($this->resolvedNames[$className])) { - $this->resolvedNames[$className] = $this->resolver->resolveClassName($className); - } - - return $this->resolvedNames[$className]; + return $this->resolvedNames[$className] ??= $this->resolver->resolveClassName($className); } } diff --git a/lib/Doctrine/ODM/MongoDB/Query/Builder.php b/lib/Doctrine/ODM/MongoDB/Query/Builder.php index 7a0fed310d..6578e1e8a9 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/Builder.php +++ b/lib/Doctrine/ODM/MongoDB/Query/Builder.php @@ -457,11 +457,8 @@ public function equals($value): self */ public function exclude($fieldName = null): self { - if (! isset($this->query['select'])) { - $this->query['select'] = []; - } - - $fieldNames = is_array($fieldName) ? $fieldName : func_get_args(); + $this->query['select'] ??= []; + $fieldNames = is_array($fieldName) ? $fieldName : func_get_args(); foreach ($fieldNames as $fieldName) { $this->query['select'][$fieldName] = 0; @@ -1261,11 +1258,8 @@ public function returnNew(bool $bool = true): self */ public function select($fieldName = null): self { - if (! isset($this->query['select'])) { - $this->query['select'] = []; - } - - $fieldNames = is_array($fieldName) ? $fieldName : func_get_args(); + $this->query['select'] ??= []; + $fieldNames = is_array($fieldName) ? $fieldName : func_get_args(); foreach ($fieldNames as $fieldName) { $this->query['select'][$fieldName] = 1; @@ -1464,11 +1458,8 @@ public function snapshot(bool $bool = true): self */ public function sort($fieldName, $order = 1): self { - if (! isset($this->query['sort'])) { - $this->query['sort'] = []; - } - - $fields = is_array($fieldName) ? $fieldName : [$fieldName => $order]; + $this->query['sort'] ??= []; + $fields = is_array($fieldName) ? $fieldName : [$fieldName => $order]; foreach ($fields as $fieldName => $order) { if (is_string($order)) { diff --git a/lib/Doctrine/ODM/MongoDB/Query/Expr.php b/lib/Doctrine/ODM/MongoDB/Query/Expr.php index f2e6bbb3e3..3ff256bef2 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/Expr.php +++ b/lib/Doctrine/ODM/MongoDB/Query/Expr.php @@ -84,12 +84,8 @@ public function __construct(DocumentManager $dm) */ public function addAnd($expression, ...$expressions): self { - if (! isset($this->query['$and'])) { - $this->query['$and'] = []; - } - $this->query['$and'] = array_merge( - $this->query['$and'], + $this->query['$and'] ?? [], func_get_args(), ); @@ -107,12 +103,8 @@ public function addAnd($expression, ...$expressions): self */ public function addNor($expression, ...$expressions): self { - if (! isset($this->query['$nor'])) { - $this->query['$nor'] = []; - } - $this->query['$nor'] = array_merge( - $this->query['$nor'], + $this->query['$nor'] ?? [], func_get_args(), ); @@ -130,12 +122,8 @@ public function addNor($expression, ...$expressions): self */ public function addOr($expression, ...$expressions): self { - if (! isset($this->query['$or'])) { - $this->query['$or'] = []; - } - $this->query['$or'] = array_merge( - $this->query['$or'], + $this->query['$or'] ?? [], func_get_args(), ); From 02bfdf6497b7c8c4fd4e6b4f11e2054f91887e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 30 Sep 2025 16:06:48 +0200 Subject: [PATCH 2/2] Use named closure syntax --- lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php | 6 +++--- .../ODM/MongoDB/Aggregation/Stage/AbstractBucket.php | 2 +- .../ODM/MongoDB/Aggregation/Stage/AbstractReplace.php | 2 +- lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup.php | 2 +- .../ODM/MongoDB/Aggregation/Stage/Search/Compound.php | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php index 8a4f3ca2aa..20f642d929 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php @@ -124,7 +124,7 @@ public function addAnd($expression, ...$expressions): static { $this->expr['$and'] = array_merge( $this->expr['$and'] ?? [], - array_map([$this, 'prepareArgument'], func_get_args()), + array_map($this->prepareArgument(...), func_get_args()), ); return $this; @@ -142,7 +142,7 @@ public function addOr($expression, ...$expressions): static { $this->expr['$or'] = array_merge( $this->expr['$or'] ?? [], - array_map([$this, 'prepareArgument'], func_get_args()), + array_map($this->prepareArgument(...), func_get_args()), ); return $this; @@ -1114,7 +1114,7 @@ private function prepareArgument($expression) } if (is_array($expression)) { - return array_map([$this, 'prepareArgument'], $expression); + return array_map($this->prepareArgument(...), $expression); } if ($expression instanceof self) { diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/AbstractBucket.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/AbstractBucket.php index bf3a74fd95..478914f65c 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/AbstractBucket.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/AbstractBucket.php @@ -79,7 +79,7 @@ abstract protected function getStageName(): string; private function convertExpression($expression) { if (is_array($expression)) { - return array_map([$this, 'convertExpression'], $expression); + return array_map($this->convertExpression(...), $expression); } if (is_string($expression) && substr($expression, 0, 1) === '$') { diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/AbstractReplace.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/AbstractReplace.php index 1f39d4f95f..589ff2ae4e 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/AbstractReplace.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/AbstractReplace.php @@ -43,7 +43,7 @@ protected function getReplaceExpression() private function convertExpression($expression) { if (is_array($expression)) { - return array_map([$this, 'convertExpression'], $expression); + return array_map($this->convertExpression(...), $expression); } if (is_string($expression) && substr($expression, 0, 1) === '$') { diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup.php index e58809f2a0..59d39e2415 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/GraphLookup.php @@ -257,7 +257,7 @@ private function fromReference(string $fieldName): static private function convertExpression($expression) { if (is_array($expression)) { - return array_map([$this, 'convertExpression'], $expression); + return array_map($this->convertExpression(...), $expression); } if (is_string($expression) && substr($expression, 0, 1) === '$') { diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Search/Compound.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Search/Compound.php index 8b8f604276..220b05e742 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Search/Compound.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Stage/Search/Compound.php @@ -115,7 +115,7 @@ static function (SearchOperator $operator): object { protected function getAddOperatorClosure(): Closure { - return Closure::fromCallable([$this, 'addOperator']); + return $this->addOperator(...); } protected function getCompoundStage(): Compound