Skip to content

gen_stub.php: Allowed the use of formats like @param array<> #18924

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 1 commit into from
Jun 25, 2025

Conversation

SakiTakamachi
Copy link
Member

@SakiTakamachi SakiTakamachi commented Jun 24, 2025

This doesn’t change any output, but it makes it easier to provide hints for PHPStan or IDEs.

@SakiTakamachi SakiTakamachi marked this pull request as ready for review June 24, 2025 06:58
@DanielEScherzer
Copy link
Member

Do you have an example where this would be used?

@SakiTakamachi
Copy link
Member Author

@DanielEScherzer

No, it doesn’t seem necessary for the current code.
I noticed this while creating a prototype for a method that might be proposed as a new addition.
Do you think it’s better not to add it yet?

@DanielEScherzer
Copy link
Member

I was just wondering what that prototype was, in case it could be accomplished with the existing support

@SakiTakamachi
Copy link
Member Author

@DanielEScherzer

Ah, I see.
It’s array<mixed, Number|string|int>.

@DanielEScherzer
Copy link
Member

That is definitely already possible for return values, e.g.

* @return array<int|string, int|string|array>

And from what I can see the @param and @return parsing have the same logic

What error are you getting if you try @param array<mixed, Number|string|int> $foo now?
What about @param array<string|int, Number|string|int> $foo ?

@SakiTakamachi
Copy link
Member Author

SakiTakamachi commented Jun 24, 2025

@DanielEScherzer

The exception I am encountering is this one:

throw new Exception("@$this->name doesn't contain a variable name or has an invalid format \"$value\"");

And from what I can see the @param and @return parsing have the same logic

No, different regular expressions are used for param and return.

edit: Sorry, that might not be correct ↑

edit2: Understood. This method is probably not used for return.

case 'param':

In param, if <> follows array, it fails to extract the variable name.

if ($this->name === "param") {

@DanielEScherzer
Copy link
Member

Ah, okay, so the type gets extracted, but not the name? Then this patch makes sense to me, but I'll leave final approval for @kocsismate

@kocsismate
Copy link
Member

The original rationale behind the current behavior is that stubs should contain type info that is directly consumed by php-src or php-doc. That's why return types can contain detailed array shape type doc because the optimizer uses the information.

Later on, this was slightly changed. I think Bob made some extended syntax possible (see the comment above the changed code). So I'm approving the changes.

@SakiTakamachi SakiTakamachi merged commit 8b61c49 into php:master Jun 25, 2025
9 checks passed
@SakiTakamachi SakiTakamachi deleted the gen_stub_array branch June 25, 2025 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants