Skip to content

Conversation

@IonBazan
Copy link
Member

@IonBazan IonBazan commented Nov 25, 2025

Q A
Type improvement
BC Break yes
Fixed issues -

Summary

Adds typed properties and missing typehints (especially mixed, callable and union types which were not available earlier).

There are still many places to fix but let's consider this as first round.
Attributes were untouched to avoid conflicts with other changes.

@IonBazan IonBazan added this to the 3.0.0 milestone Nov 25, 2025
@IonBazan IonBazan self-assigned this Nov 25, 2025
Copilot finished reviewing on behalf of IonBazan November 25, 2025 17:22
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 adds typed properties and type hints to the codebase, introducing mixed, callable, and union types that were not available in earlier PHP versions. This is a breaking change that modernizes the type system across the ODM.

Key Changes:

  • Added explicit type declarations to class constants (e.g., public const int STATE_MANAGED = 1)
  • Converted PHPDoc type annotations to native typed properties
  • Added mixed type hints to method parameters and return types where values can be of any type
  • Introduced union types (e.g., string|int|null, array|Expr) for more precise type declarations
  • Used constructor property promotion to reduce boilerplate code
  • Made ReferencePrimer a readonly class with promoted constructor properties
  • Removed deprecated getFqcnFromAlias() method from ClassMetadataFactory

Reviewed changes

Copilot reviewed 37 out of 37 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/UnitOfWork.php Added type declarations to constants and method parameters (mixed types for identifier parameters)
src/Types/BinData*.php Converted annotated properties to typed properties for $binDataType
src/SchemaManager.php Added type declarations to all constants (int and array types)
src/Repository/UploadOptions.php Converted to typed properties with explicit default values
src/Repository/GridFSRepository.php Added mixed type hints for identifier parameters
src/Repository/DocumentRepository.php Used constructor promotion for dependencies and added proper type hints
src/Repository/DefaultGridFSRepository.php Added mixed type hints for identifier parameters
src/Query/ReferencePrimer.php Converted to readonly class with constructor property promotion
src/Query/QueryExpressionVisitor.php Added mixed return type
src/Query/Filter/BsonFilter.php Used constructor promotion and added typed properties
src/Query/Expr.php Added union types for parameters, made DocumentManager readonly
src/Query/CriteriaMerger.php Added array type to variadic parameter
src/Query/Builder.php Changed $currentField visibility to private and added type hints throughout
src/Mapping/Driver/XmlDriver.php Added type declarations to constants and constructor parameters
src/Mapping/Driver/SimplifiedXmlDriver.php Added type declarations to constant and constructor parameters
src/Mapping/ClassMetadataFactory.php Removed deprecated method and added type hints to protected methods
src/Mapping/ClassMetadata.php Extensive typed property additions throughout the class
src/LockMode.php Added type declarations to all constants
src/LockException.php Made constructor parameter readonly
src/Id/IncrementGenerator.php Converted to typed properties and added return type
src/Events.php Added string type declarations to all event name constants
src/DocumentNotFoundException.php Added mixed type for identifier parameter
src/DocumentManager.php Added type declarations to constant and method signatures
src/Configuration.php Added int type declarations to all autogeneration constants
src/Aggregation/Stage/Operator.php Added typed property for $expr
src/Aggregation/Stage/MatchStage.php Added typed property and void return type for __clone
src/Aggregation/Stage/Group.php Removed redundant property and constructor (inherited from Operator)
src/Aggregation/Stage/Bucket/AbstractOutput.php Used constructor promotion for $bucket dependency
src/Aggregation/Stage/AbstractReplace.php Added union type hints for constructor and method parameters
src/Aggregation/Stage/AbstractBucket.php Added typed properties with union types
src/Aggregation/Stage.php Used constructor property promotion for $builder

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@IonBazan IonBazan force-pushed the typed-v3 branch 2 times, most recently from f208c3e to 49dbd5b Compare November 25, 2025 17:35
Comment on lines 136 to 141
#[DataProvider('dataMethodsAffectedByNoObjectArguments')]
public function testThrowsExceptionOnNonObjectValues(string $methodName): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectException(TypeError::class);
$this->dm->$methodName(null);
}
Copy link
Member

Choose a reason for hiding this comment

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

Usually we don't test PHP native behavior of rejecting invalid value. But that's good to have it now to ensure we reject the same values as before.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's leave it here for now, in case we have to relax the types here in the future.

Copy link
Member

Choose a reason for hiding this comment

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

Unrelated, I wonder if we should keep 2 XML driver classes?

*
* @see discriminatorField
*
* @var class-string|null
Copy link
Member

Choose a reason for hiding this comment

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

You removed the class-string type. I was incorrect, right? We should fix this type in the current 2.x.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think this is wrong. The discriminatorValue holds the value that is used as key in discriminatorMap to get Document FQCN. I thought it can only hold string but it appears that int was a valid use case in #2712.
Let me create a fix for v2 as it's considered a bug.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like it's a bigger problem, reported in #2936. For now, I will revert this particular change.

Copilot finished reviewing on behalf of IonBazan November 27, 2025 05:46
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

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants