Skip to content

Use null coalescing operator ?? and ??=#2843

Merged
GromNaN merged 2 commits intodoctrine:2.13.xfrom
GromNaN:null-coal
Sep 30, 2025
Merged

Use null coalescing operator ?? and ??=#2843
GromNaN merged 2 commits intodoctrine:2.13.xfrom
GromNaN:null-coal

Conversation

@GromNaN
Copy link
Member

@GromNaN GromNaN commented Sep 30, 2025

Q A
Type improvement
BC Break no
Fixed issues -

Summary

Factorize code using the operators ?? (PHP 7.0) and ??= (PHP 7.4).

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors existing PHP code to use modern null coalescing operators (?? and ??=) introduced in PHP 7.0 and 7.4, replacing verbose isset() checks with more concise syntax.

  • Replaced isset() checks followed by array initialization with null coalescing assignment operator (??=)
  • Replaced isset() checks in ternary operations with null coalescing operator (??)
  • Simplified conditional logic for array access patterns

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
lib/Doctrine/ODM/MongoDB/Query/Expr.php Replaced isset() checks with ?? operator in addAnd, addNor, and addOr methods
lib/Doctrine/ODM/MongoDB/Query/Builder.php Used ??= operator for array initialization in exclude, select, and sort methods
lib/Doctrine/ODM/MongoDB/Proxy/Resolver/CachingClassNameResolver.php Simplified caching logic using ??= operator
lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php Replaced isset() checks with ?? operator in association type checking methods
lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php Applied ?? operator in addAnd and addOr methods for cleaner array merging

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a phpcs rule for this that we could enable?

$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()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also use a first class callable here and in the other methods where callables are used:

Suggested change
array_map([$this, 'prepareArgument'], func_get_args()),
array_map($this->prepareArgument(...), func_get_args()),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I updated some other places to use named closures.

@GromNaN
Copy link
Member Author

GromNaN commented Sep 30, 2025

Do we have a phpcs rule for this that we could enable?

404 rule not found.

@GromNaN GromNaN enabled auto-merge (squash) September 30, 2025 16:16
@GromNaN GromNaN merged commit 18a3c34 into doctrine:2.13.x Sep 30, 2025
23 checks passed
@alcaeus
Copy link
Member

alcaeus commented Sep 30, 2025

Do we have a phpcs rule for this that we could enable?

404 rule not found.

FWIW, there's SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator and SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator, but I'm not sure if we're using a new enough version of the coding standard to leverage those.

@GromNaN GromNaN deleted the null-coal branch September 30, 2025 16:35
@GromNaN GromNaN added Task and removed Enhancement labels Sep 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants