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
Protoc generator for php missing type hint on message getters (yet setters have it) #6145
Comments
I don't think |
The code already does this for setters, but not for getters. It's an addition to the docblock line, not changing the true return type (which stays as RepeatedField). The PHP client is extremely unfriendly to use due to this omission, with IDEs being unable to help with autocompleting off of getters. Again, the setters have this already. |
This is actually one of the first thing I found very weird. The getters for repeated fields would be a lot easier to use if they returned the right type, just like the setters do. /** @var InnerMessage[] $innerMessage */ |
@TeBoring Can you please reread the issue to understand? It's about modifying the |
The setter accept both php native array and our internal repeated field.
However, the getter only returns our internal repeated field. If we change
it to php native array, that would be a lie.
Does php have some interface like iterable? Probably we can change both
getter and setter to that.
@haberman, how do you think?
…On Mon, Jul 6, 2020 at 3:40 PM Nate Bessette ***@***.***> wrote:
@TeBoring <https://github.com/TeBoring> Can you please reread the issue
to understand? It's about modifying the @return tag in the docblock, not
changing the real return type. Again, the generator already does this for
the setters... for some reason the getters (which are imo even more useful)
were excluded.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6145 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABHUPZLVY2ZHBCNBL3YO7A3R2JHGVANCNFSM4HNPTQYA>
.
|
I agree with @TeBoring's analysis. The getters are documented to accept Ideally we could annotate the return type as something like If not, the question is whether is it acceptable and desirable to write @bshaffer any thoughts here? |
It’s this, exactly. Due to limitations of the php language, it’s very common to tell “white lies” in phpdoc comments about /** @return \Generator|string[] */
function generate(): \Generator
{
yield “hello”;
yield “world”;
} The phpdoc for these sorts of things is really about helping out in the IDE. It has no effect on actual usage whatsoever. As @pelletiermaxime mentioned, right now your users are littering their code based with literally hundreds/thousands of manual |
I couldn't agree more with @frickenate. Yes, it's kind of a lie, but that why we always return both with |
I agree it's very important to add the return type of the iterated items, and t's ok to add the union return type for this cause (it's pretty clear what this means). In my testing (PHPStorm), I found the following worked as expected: /**
* @return InnerMessage[]|RepeatedField
* @return RepeatedField|InnerMessage[]
*/ And the following did not work: /**
* @return RepeatedField[InnerMessage]
* @return RepeatedField<InnerMessage>
*/ The first ( |
Sounds like we have consensus. I'll re-open the issue. I'd be happy to accept a pull request for this. |
Hi all, I did a little digging here, and it seems as though generics were supported by PHP storm at one point in time while PSR-5 was more active. As PSR-5 developed, it looks as though generics support was dropped (at least for the time being, based on the verbiage). It looks to me this may be why PHP storm doesn't support generics today - however, there appears to be an on-going case to get the support back into the IDE (which is recently active). If it seems like PHP storm may support generics soon, would we be open to using that syntax instead of union? It seems like a more accurate representation of the state of the code and can be supported in the interim with a metadata file. If the IDE doesn't add support, we can move back to using union instead. WDYT? |
Adding generics would help static analysis tools and maybe PhpStorm in the future, but to me it's a bit weird to add tool/ide specific annotations in generated code. That may be my own bias as I didn't use PhpStorm for a long while. |
Indeed,
Regarding
Whereas |
Fair and reasonable points @pelletiermaxime and @frickenate. I shouldn't have so broadly assumed folks using an IDE nowadays would be on PHP Storm.
I was thinking it could be scripted, but I haven't proof on concepted that out yet to determine how feasible it is.
I understand we're between a rock and a hard place here, I'm just bothered by the return block not being truthful - for each case it may help folks there could also be cases where it misleads others. Would you be able to share some examples where this is considered an idiomatic workaround? I'd be open to learning a bit more on the subject. |
Good data points, thanks. Alright I think I'm convinced this can be a reasonable path forward until (hopefully) generic support lands. |
an idea could be to add the generic phpdoc using |
It looks like the consensus here is that this can be implemented, but it will not be a priority for the proto team. If you all would like this feature, we would be happy to review and merge a PR! |
What language does this apply to?
protoc generated code for php
Describe the problem you are trying to solve.
Setters on messages for repeated fields are docblock type hinted (thanks to
PhpSetterTypeName()
insrc/google/protobuf/compiler/php/php_generator.cc
) to include both\Google\Protobuf\Internal\RepeatedField
as well as the underlying repeated field type—whether it's a native type (eg.int[]
orstring[]
) or an embedded/nested message (eg.InnerMessage[]
). This is perfect for integration with IDEs.Unfortunately the getters for repeated fields, as implemented by
PhpGetterTypeName()
, only type hints\Google\Protobuf\Internal\RepeatedField
, without adding the additionalTYPE[]|
type hint for the underlying type being repeated.Example stub of what is currently generated, where the setter has the "useful type hint" as well as
RepeatedField
, but the getter only hasRepeatedField
and is missing the "useful type hint":Thus when writing PHP code in an intelligent IDE that uses the getters:
Describe the solution you'd like
The logic used by
PhpSetterTypeName()
to generate the@param
phpdoc for setters should be replicated byPhpGetterTypeName()
to generate the@return
phpdoc for getters. The result of the earlier example stub should become:Describe alternatives you've considered
N/A
Additional context
N/A
The text was updated successfully, but these errors were encountered: