Skip to content

Conversation

@GromNaN
Copy link
Member

@GromNaN GromNaN commented Dec 5, 2025

Q A
Type improvement
BC Break no
Fixed issues Fix #2870

Summary

The default implementation of Type::closureToPhp() is incorrect as it forwards the unmodified value.

public function closureToPHP(): string
{
return '$return = $value;';
}

In 3.0, I want to change the default implementation to use the one from the ClosureToPHP trait that calls the type class.

return sprintf('
$type = \%s::getType($typeIdentifier);
$return = $type->convertToPHPValue($value);', Type::class);

Also adding the missing implementation for collection and hash.

@GromNaN GromNaN added this to the 2.16.0 milestone Dec 5, 2025

public function closureToPHP(): string
{
return '$return = array_values($value);';
Copy link
Member Author

Choose a reason for hiding this comment

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

null is already handled by the generated hydrator code.

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 deprecates the current default implementation of Type::closureToPHP() in preparation for MongoDB ODM 3.0, where the default will change to call convertToPHPValue() instead of returning the value unchanged.

  • Adds deprecation warnings for closureToMongo() and the default closureToPHP() implementation
  • Implements closureToPHP() for CollectionType and HashType which were previously missing
  • Adds ClosureToPHP trait to TimestampType and KeyType for the new behavior

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Types/Type.php Adds deprecation warnings to closureToMongo() and closureToPHP() methods
src/Types/CollectionType.php Implements closureToPHP() to match convertToPHPValue() behavior
src/Types/HashType.php Implements closureToPHP() to match convertToPHPValue() behavior
src/Types/TimestampType.php Adds ClosureToPHP trait for proper type conversion
src/Types/KeyType.php Adds ClosureToPHP trait for proper type conversion
src/Types/ClosureToPHP.php Documents trait deprecation in 3.0
tests/Tests/Types/TypeTest.php Adds test to verify closureToPHP() behavior
tests/Tests/Functional/CustomTypeTest.php Adds test for deprecation warning with custom type
UPGRADE-2.16.md Documents upgrade path for custom types

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Evaluate the performance benefit of Type::closureToPHP and closureToMongo

1 participant