-
Notifications
You must be signed in to change notification settings - Fork 15.7k
feat(php): improve return typehint when repeatedfield #11734
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
Conversation
// accommodate for edge case with multiple types. | ||
size_t start_pos = type.find('|'); | ||
if (start_pos != std::string::npos) { | ||
type.replace(start_pos, 1, ">|\\Google\\Protobuf\\Internal\\RepeatedField<"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use ArrayAccess
instead? RepeatedField
is an internal type and we do not want to expose it to public interfaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense to me as long as there are no methods of RepeatedField
that we want IDEs to typehint (e.g., we want the users to only see the objects as an array, and NOT as a RepeatedField
).
Also, if we do that, we should also do that for the setter typehints, for consistency. In this case, I think the best approach would be to use []
, such as @return string[]
.
As changes like this will effect most of the files in our repo, making them at the same time would be ideal. If that sounds good to you, I'll update this PR to remove RepeatedField
entirely for both getters and setters, in favor of the type[]
syntax!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds good to me. The only caveat is that I'm not sure if we support assignment of PHP arrays to repeated fields or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok sounds good, yes let's remove RepeatedField in getters and setters. If we use type[]
we should be satisfying that by implementing ArrayAccess
, yes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@haberman yes that's correct! ArrayAccess
will handle anything annotated with type[]
We do!
…On Mon, Feb 13, 2023, 7:55 PM Joshua Haberman ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/google/protobuf/compiler/php/php_generator.cc
<#11734 (comment)>
:
> case FieldDescriptor::TYPE_GROUP: return "null";
default: assert(false); return "";
}
+ if (field->is_repeated()) {
+ // accommodate for edge case with multiple types.
+ size_t start_pos = type.find('|');
+ if (start_pos != std::string::npos) {
+ type.replace(start_pos, 1, ">|\\Google\\Protobuf\\Internal\\RepeatedField<");
That sounds good to me. The only caveat is that I'm not sure if we support
assignment of PHP arrays to repeated fields or not.
—
Reply to this email directly, view it on GitHub
<#11734 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAZMBP6FYCQ4VKRGZVDXXLWXLJXPANCNFSM6AAAAAAUM4NYKM>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
We triage inactive PRs and issues in order to make it easier to find active work. If this PR should remain active, please add a comment. This PR is labeled |
We triage inactive PRs and issues in order to make it easier to find active work. If this PR should remain active or becomes active again, please reopen it. This PR was closed and archived because there has been no new activity in the 14 days since the |
The PR is kept open if there are any comments on the issue. It's to gauge whether there is still interest from the author. I'll reopen this one. |
Hi @bshaffer , can you confirm an answer to my most recent question above?
|
It looks like the PR is still using |
@haberman so a few thoughts on this... There are a few issues with typing things as we have now. This depends on the context of. the typehint: Typehinting a setter as
|
What about |
|
Another thought on this is we could typehint the returns as |
I would also like to add that UnaryCall, ServerStreamingCall, ClientStreamingCall and BidiStreamingCall would be nice to type this way too. |
aaa08d8 seems to have broken all the tests. |
@bshaffer - can you look into the test failures? |
@JasonLunn the tests are passing! This is ready for review |
<?php | ||
|
||
// Protocol Buffers - Google's data interchange format | ||
// Copyright 2008 Google Inc. All rights reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: do we even need this header comment? If so, should it be this year's date?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question - I didn't update it since the file was essentially moved, but it did change a little... so maybe we should update it? I'm happy to do so if you'd like!
Adds the type of the
RepeatedField
to the PHPDoc, e.g.Whereas before, the
<int>
part was not included.This is the "getter" counterpart for #4533