PHP Documentor中的注释关联数组

Abe*_*nil 19 php arrays associative-array phpdoc

我在PHP应用程序中使用了几个关联数组,我使用PHP文档来评论我的源代码.我从来没有真正为数组中的数组指定注释,但现在我需要这样做而不知道如何.

$array = array('id' => 'test', 'class' => 'tester', 'options' => array('option1' => 1, 'option2' => 2))
Run Code Online (Sandbox Code Playgroud)

如何以正确的方式@var@param评论对此数组进行注释?我可以这样做,但我不知道这是否正确:

@param string $array['id']
@param string $array['class']
@param int $array['options']['option1']
Run Code Online (Sandbox Code Playgroud)

但是如何为这@var部分做到这一点?

Ste*_*hry 30

你不能记录每个键,但你可以告诉phpDocumentor它是什么类型.

你可以这样做:

/**
 * Form the array like this:
 * <code>
 * $array = array(
 *   'id'      => 'foo',          // the id
 *   'class'   => 'myClass',     // the class
 * );
 * 
 * </code>
 *
 * @var array[string]string 
 */
$array;
Run Code Online (Sandbox Code Playgroud)

  • 我想知道,在任何IDE中都已确认可以使用自动完成/智能感知吗?根据[phpDoc ABNF for type definitions](http://www.phpdoc.org/docs/latest/for-users/types.html#abnf),不允许为数组索引指定类型.它将数组指定为`@var string []`(`array`组件仅适用于"未指定"数组). (2认同)
  • Sepster评论中提到的ABNF更新链接:https://www.phpdoc.org/docs/latest/references/phpdoc/types.html (2认同)

小智 10

Phpstorm 中的工作原理是:

/**
 * @return array{ hand: Hand, card: CardType | null }
 */
Run Code Online (Sandbox Code Playgroud)


Tom*_*ger 8

我会看一下WordPress内联文档参考资料中的一些提示,尽管它目前还不全面.

使用@param或@var或@property,取决于您的上下文中的哪个

根据这些指南,您可以像这样记录您的关联数组:

/**
 * @property array $my_array {
 *     An array of parameters that customize the way the parser works.
 *
 *     @type boolean $ignore_whitespace Whether to gobble up whitespace. Default true.
 *     @type string $error_level What the error reporting level is. Default 'none'.
 *                               Accepts 'none', 'low', 'high'.
 * }
 */
Run Code Online (Sandbox Code Playgroud)

  • 尽管在 2013-14 年就添加它进行了认真的讨论,但这种用于记录数组结构的符号从未成为官方 PHPDoc 规范。 (2认同)
  • 似乎有一些相关的讨论 https://github.com/phpDocumentor/phpDocumentor2/issues/650 (2认同)

小智 5

对我来说,这在 PhpStorm 中很好用,以获得很好的返回值描述:

/**
 * @param string $requestUri
 * @return array[
 *  'controller' => string,
 *  'action' => string
 * ]
 */
Run Code Online (Sandbox Code Playgroud)

  • 在 Webstorm 2020.1 EAP 中尝试了此参数描述,但它破坏了帮助弹出窗口。根据我的经验,这是行不通的。 (2认同)