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

PHP variadic function wrong variable name #135

Closed
jfcherng opened this issue Jul 14, 2019 · 8 comments
Closed

PHP variadic function wrong variable name #135

jfcherng opened this issue Jul 14, 2019 · 8 comments

Comments

@jfcherng
Copy link

jfcherng commented Jul 14, 2019

Problem

Variadic function is introduced in PHP 5.6. Here's a simple example.

function variadicFunction(...$args): array
{
    return $args;
}

The generated PHPDoc looks like the following.

/**
 * { function_description }
 *
 * @param ... $args The arguments
 *
 * @return ...|array ( description_of_the_return_value )
 */
function variadicFunction(...$args): array
{
    return $args;
}

It looks like the ... is treated as the variable type of $arg but actually it is not and no type is declared here.

Expected Result

/**
 * { function_description }
 *
 * @param <type> ...$args The arguments
 *
 * @return array ( description_of_the_return_value )
 */
function variadicFunction(...$args): array
{
    return $args;
}

Just a note. Here, the <type> is the type of the element of $args rather than $args itself.

Reference: phpDocumentor/fig-standards#121 (comment)

Another Problem that May Be Related

Say, now I have my doc edited manually as the following.

/**
 * This is a variadic function.
 *
 * @param mixed ...$args The arguments
 *
 * @return array
 */
function variadicFunction(...$args): array
{
    return $args;
}

Now I press Alt+Q(doxy_comment_nearest_entity). The doc becomes

/**
 * This is a variadic function.
 *
 * @param mixed $args The arguments
 *
 * @return array
 */
function variadicFunction(...$args): array
{
    return $args;
}

The ... is removed by plugin.

@20Tauri
Copy link
Owner

20Tauri commented Jul 15, 2019

Thanks for reporting,

As the current generated comment match no recommendation, I have implemented your suggestion ("old" PhpStorm syntax)...

But the syntax seems tools dependant (discussed here on StackOverflow )

PhpDoc documentation defines:

/**
 * @param mixed $args,... The arguments
 */

This option seem to be a better choice...
What is your opinion about this syntax ?
Is it compliant with your editor ?

@jfcherng
Copy link
Author

jfcherng commented Jul 16, 2019

image

Yes, it's okay in my use case as well. Either way, it's better than the current one. Thanks!

@20Tauri
Copy link
Owner

20Tauri commented Jul 16, 2019

Thanks

0.71.0 is now released on the evolution branch
Will be pushed in the regular branch next week

@jfcherng
Copy link
Author

Works for me. Thanks.

@jfcherng
Copy link
Author

It looks like my static analyzer (phan) doesn't like this syntax. I have to check with the author.

@jfcherng
Copy link
Author

jfcherng commented Jul 17, 2019

I check some most famous PHP static analysis tools.

All of them are using @param <type> ...$args as the doc of a variadic funciton.
Since PHPStorm also accepts that form, I suggest we use it as the default one for best compatibility.
Or, hopefully make it configurable as an user preference.

@20Tauri
Copy link
Owner

20Tauri commented Jul 17, 2019

Thanks again,

0.71.1 is now released. On comment update the "...$args" notation will be applied

@jfcherng
Copy link
Author

LGTM. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants