Use null coalescing operator ?? and ??=#2843
Conversation
There was a problem hiding this comment.
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.
alcaeus
left a comment
There was a problem hiding this comment.
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()), |
There was a problem hiding this comment.
You could also use a first class callable here and in the other methods where callables are used:
| array_map([$this, 'prepareArgument'], func_get_args()), | |
| array_map($this->prepareArgument(...), func_get_args()), |
There was a problem hiding this comment.
Good catch, I updated some other places to use named closures.
404 rule not found. |
FWIW, there's |
Summary
Factorize code using the operators
??(PHP 7.0) and??=(PHP 7.4).