Skip to content

PHPC-2004 and PHPC-2007: Specify __toString() return type and implement Stringable #1274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 29, 2021

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Nov 10, 2021

https://jira.mongodb.org/browse/PHPC-2004
https://jira.mongodb.org/browse/PHPC-2007

See https://externals.io/message/116308#116318 for context. There is a subtle BC break here, as userland classes implementing our BSON interfaces will now need to explicitly specify a string return type on PHP versions before 8.1 (where it's added automatically). AFAICT, the only way to avoid the BC break entirely would be to continue to omit return type info and let PHP 8.1+ add the return type info and Stringable implementation automatically; however, that's not a permanent solution as PHP 8.2 will start raising warnings for the automatic behavior (php/php-src@86379b6).

For that reason, I'm inclined to explicitly add return type info and Stringable implementations (for PHP 8+) and force users to confront the BC break now. This is also unlikely to affect many users, since the BSON interfaces are not widely used (PHPC-640 is one such example).

@jmikola jmikola requested a review from sgolemon November 10, 2021 21:34
@jmikola jmikola force-pushed the phpc-2004 branch 2 times, most recently from 2845806 to c751654 Compare November 11, 2021 16:23
Adding return type info to the interface methods is a small subtle BC break. Userland classes will also need to add return type info prior to PHP 8.1, where it is added automatically. However, to leave the interface methods untyped would invite an error because BSON classes cannot otherwise conform to Stringable, which has return type info.

Additionally, PHP 8.2 will start raising a warning when return type info is automatically applied, so a BC break in some form is inevitable.
This was missed in fe99deb
@jmikola
Copy link
Member Author

jmikola commented Nov 11, 2021

Note: Evergreen failures are due to unexpected SSL cert verification (PHPC-1981)

@@ -21,7 +21,7 @@ class MyTimestamp implements MongoDB\BSON\TimestampInterface
return 1234;
}

public function __toString()
public function __toString(): string
Copy link
Member Author

Choose a reason for hiding this comment

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

This is an example of the BC break where users will need to add a type hint to any implementations of our BSON interfaces.

@jmikola jmikola merged commit 6c82ec8 into mongodb:master Nov 29, 2021
@jmikola jmikola deleted the phpc-2004 branch November 29, 2021 18:14
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.

1 participant