PHPDoc: Documenting a function with a variable number of arguments

有些话、适合烂在心里 提交于 2019-12-19 05:03:08

问题


What is the recommended method for documenting a class method that accepts a variable number of arguments?

Maybe something like this?

<?php

class Foo {
    /**
     * Calculates the sum of all the arguments.
     *
     * @param mixed [$arg1, $arg2, ...]
     *
     * @return float the calculated sum
     */
    public static function sum() {
        return array_sum(func_get_args());
    }
}

Note: As a general rule, I imagine this type of thing should be avoided where possible. That being said, it would be nice to still document the remaining few cases where it cannot be avoided.


回答1:


If your using a variable number of arguments and also using PHP >= 5.6 then you are able to use variadic functions (allowing variable number of arguments) which still conforms to the PHPDoc ,... syntax already mentioned and PHPStorm will interpret the docs properly as well. Using variadic functions eliminates needing func_get_args() to capture arguments into an array.

/**
 * @param mixed $args,... Explainatorium!
 */
function variadiculous(...$args) {
    // NOTE: $args === func_get_args()
    foreach ( $args as $arg ) {
        /* do work */
    }
}

PHPStorm will auto-generate the docs as @param array $args because technically when inside the function variadiculous is_array($args) is true. I change it to read @param mixed $args,... as above and when I use the hotkey to display a function signature from somewhere else in my code PHPStorm displays variadiculous($args : ...array|mixed) -- I recommend using this method if your using PHP >= 5.6




回答2:


/**
 * @param mixed $numbers,... Description
 */
Public function sum ($numbers)

In the method, $numbers will not be used.




回答3:


In the case of the ... syntax, PHPStorm 2017.1 uses:

/**
 * @param Type[] ...$values One or more values.
 */
public function func(Type ...$values) {
    // ...
}



回答4:


Something worth noting is that with a doc block such as:

/**
 * @param Type[] ...$values One or more values.
 */
public function func(Type ...$values) {
    // ...
}

...it might seem like you could pass an array of Type, e.g.

Foo()->func([Type, Type, Type])

...this throws a fatal error, obviously now.

Where:

Foo()->func(...[Type, Type, Type])

...does not as you have destructured it on method call.

Anyway the point here is that I was experimenting with how PHPStorm would treat the doc block and depending on the way in which you orient your $args variable in your doc block, with either ...$args or $args,... determines the type of hints you get back in your IDE.

I'd really like a way to suggest the values you should pass by name into the method in the exemplified image below in the case where you have an optional length of function/method parameters, with a specific order in which they should be passed.

You might think, "just set default args, $arg1 = 'default', $arg2 = true" which typically makes sense but in cases

Pseudo-example:

/**
 * @param int ...$args Hour, Minute, Second, Timezone, $arg [, ...$args]
 */
public static function createA(int ...$args) {}

/**
 * @param int $args,... Hour, Minute, Second, Timezone, $arg [, ...$args]
 */
public static function createB(int ...$args) {}

/**
 * @param int[]|int $args,... Hour, Minute, Second, Timezone, $arg [, ...$args]
 */

public static function createC(int ...$args) {}

...so either of:

  • https://stackoverflow.com/a/38275992/1290575
  • https://stackoverflow.com/a/43622600/1290575

...is the correct answer (just keeping in mind orientaton: ...$args or $args,...)



来源:https://stackoverflow.com/questions/14513356/phpdoc-documenting-a-function-with-a-variable-number-of-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!