Skip to content
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

PHPLIB-1635 BuilderEncoder accepts an instance of encoder instead of a class string #1608

Merged
merged 2 commits into from
Feb 26, 2025

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Feb 25, 2025

Fix PHPLIB-1635

Provides better dependency injection capability.

@GromNaN GromNaN requested a review from a team as a code owner February 25, 2025 16:26
@GromNaN GromNaN requested a review from jmikola February 25, 2025 16:26
@@ -32,7 +34,7 @@

$encoded = [];
foreach ($value->getIterator() as $stage) {
$encoded[] = $this->encoder->encodeIfSupported($stage);
$encoded[] = $this->recursiveEncode($stage);

Check notice

Code scanning / Psalm

MixedAssignment Note

Unable to determine the type of this assignment
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the return type be derived from @psalm-return on RecursiveEncode::recursiveEncode()? If not, does this need to be ignored in the baseline?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's already ignored in the baseline, but the first time, GitHub stills shows it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@alcaeus I also removed the interface and replaced the abstract class with a trait. That simplifies inheritance chain. This was useful for the constructor, but since only internal classes are instantiated by class name, I don't think we need them.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

A couple of questions but I think the only change necessary may be a baseline addition for the psalm warning. No need to hold that up with a second review unless you want @alcaeus to take a look.

@@ -67,7 +67,7 @@ public function canEncode(mixed $value): bool
return (bool) $this->getEncoderFor($value)?->canEncode($value);
}

public function encode(mixed $value): stdClass|array|string|int
public function encode(mixed $value): mixed
Copy link
Member

Choose a reason for hiding this comment

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

Is mixed necessary here? I noticed it's absent on Encoder interface, which only specifies:

* @return mixed
* @psalm-return BSONType

I also noted that the original return type prior to this PR corresponds to the psalm-template implementation.

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 point, that's what bothers me. There's no constraint on the return type of the custom encoders. So technically the BuilderEncoder could accept anything an Encoder would accept.

Copy link
Member Author

Choose a reason for hiding this comment

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

I reverted to Type|stdClass|array|string|int, as this is expected.

@@ -32,7 +34,7 @@

$encoded = [];
foreach ($value->getIterator() as $stage) {
$encoded[] = $this->encoder->encodeIfSupported($stage);
$encoded[] = $this->recursiveEncode($stage);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the return type be derived from @psalm-return on RecursiveEncode::recursiveEncode()? If not, does this need to be ignored in the baseline?

@GromNaN GromNaN merged commit 36fca42 into mongodb:v1.x Feb 26, 2025
31 checks passed
@GromNaN GromNaN deleted the PHPLIB-1635 branch February 26, 2025 10:28
This was referenced Feb 26, 2025
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.

2 participants