PHPDoc 用于可变长度参数数组

发布于 2024-08-16 11:48:40 字数 1072 浏览 2 评论 0原文

是否有用于记录采用单个配置数组而不是单个参数的函数的语法?

我特别想到 CodeIgniter 风格的库,它使用与此类似的机制:

<?php

//
// Library definition
//

class MyLibrary {
  var $foo;
  var $bar;
  var $baz;
  // ... and many more vars...


  /* Following is how CodeIgniter documents their built-in libraries,
   * which is mostly useless.  AFAIK they should be specifying a name
   * and description for their @param (which they don't) and omitting
   * @return for constructors 
   */

  /** 
   * @access public
   * @param array
   * @return void
   */
  function MyLibrary($config = array()) {
    foreach ($config as $key => $value) {
      $this->$key = $value;
    }
  }
}

//
// Library usage:
//

// Iniitialize our configuration parameters
$config['foo'] = 'test';
$config['bar'] = 4;
$config['baz'] = array('x', 'y', 'z');

$x = new MyLibrary($config);

?>

所以我的问题是,除了纯粹的文本描述之外,是否有某种受支持的方法来记录配置数组?实际上指定一个正确的@param [type] [name] [desc]来允许PHPDoc解析出有用的值?

顺便说一句,CodeIgniter 实际上只是用上面通过 $config 数组传入的值覆盖它自己的值,从而有效地允许您破坏私有成员。我不是粉丝,但我坚持下去。

Is there a syntax for documenting functions which take a single configuration array, rather than individual parameters?

I'm thinking specifically of CodeIgniter-style libraries, which use a mechanism similar to this:

<?php

//
// Library definition
//

class MyLibrary {
  var $foo;
  var $bar;
  var $baz;
  // ... and many more vars...


  /* Following is how CodeIgniter documents their built-in libraries,
   * which is mostly useless.  AFAIK they should be specifying a name
   * and description for their @param (which they don't) and omitting
   * @return for constructors 
   */

  /** 
   * @access public
   * @param array
   * @return void
   */
  function MyLibrary($config = array()) {
    foreach ($config as $key => $value) {
      $this->$key = $value;
    }
  }
}

//
// Library usage:
//

// Iniitialize our configuration parameters
$config['foo'] = 'test';
$config['bar'] = 4;
$config['baz'] = array('x', 'y', 'z');

$x = new MyLibrary($config);

?>

So my question is, is there some supprted way of documenting the configuration array beyond just the purely textual description? Actually specifying a proper @param [type] [name] [desc] that allows PHPDoc to parse out useful values?

As an aside, CodeIgniter really does just overwrite it's own values with those passed in via the $config array as above, effectively allowing you to clobber private members. I'm not a fan, but I'm stuck with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

月朦胧 2024-08-23 11:48:40

我从来没有见过任何“好的”方式来记录这一点——而且我也从未见过任何可以被 IDE 使用的东西(例如 Eclipse PDT) 来暗示参数:-(

我会已经说过“像你的框架那样做”,但正如你所说,它在这里所做的还不够好......

不过,也许可能的键的快速/排序列表可能比没有好;有点像这样:

@param array $config [key1=>int, otherKey=>string]

不确定 phpDocumentor 或 IDE 将如何解释它...但可能值得一试?

顺便说一句,这就是我倾向于避免这种传递参数的方式的原因之一 - 至少当方法没有太多(可选)参数时。

I've never seen any "good" way of documenting this -- and I've never seen anything that could be used by IDEs (such as Eclipse PDT) for parameters hinting either :-(

I would have said "do like your framework does", but as you said, what it does, here, is not quite good enough...

Maybe a quick/sort list of possible keys might be better than nothing, though ; a bit like this :

@param array $config [key1=>int, otherKey=>string]

Not sure how it would be interpreted by phpDocumentor or an IDE... But might be worth a try ?

This is, btw, one reason why I tend to avoid that kind of way of passing parameters -- at least when there are not too many (optional) parameters to a method.

当爱已成负担 2024-08-23 11:48:40

数组的正确数组 @param 表示法在 PHPlint 中指定,

您可以使用它以有用的方式记录配置数组:

示例:

 /**
 * Does stuff
 *
 * @param array[int|string]array[string]Object $config
 *
 * @return array[int]string
 */
public function foo(array $config)
{
    // do stuff here

    return array('foo', 'bar', 'baz');
}

The correct array @param notation for arrays is as specified in PHPlint

You can use it to document a config array in a useful manner:

Example:

 /**
 * Does stuff
 *
 * @param array[int|string]array[string]Object $config
 *
 * @return array[int]string
 */
public function foo(array $config)
{
    // do stuff here

    return array('foo', 'bar', 'baz');
}
眼前雾蒙蒙 2024-08-23 11:48:40

你可以这样做:

/**
 * @param array $param1
 * @param string $param1['hello']
 */
function hey($param1)
{
}

netbeans 会拾取它,但 phpdoc 会弄乱文档

You can do this:

/**
 * @param array $param1
 * @param string $param1['hello']
 */
function hey($param1)
{
}

and netbeans will pick it up but phpdoc messes up the documentation

叫思念不要吵 2024-08-23 11:48:40

在这种情况下我总是使用

 标签。例如:

/**
 * @param array $ops An array of options with the following keys:<pre>
 *     foo: (string) Some description...
 *     bar: (array) An array of bar data, with the following keys:
 *         boo: (string) ...
 *         far: (int) ...
 *     baz: (bool) ...
 * </pre>
 */

我使用过的大多数 IDE 和文档生成器似乎都以合理的方式呈现此内容,尽管它们当然不提供任何类型检查或数组参数检查。

I always use <pre> tags in situations like this. Ex.:

/**
 * @param array $ops An array of options with the following keys:<pre>
 *     foo: (string) Some description...
 *     bar: (array) An array of bar data, with the following keys:
 *         boo: (string) ...
 *         far: (int) ...
 *     baz: (bool) ...
 * </pre>
 */

Most IDEs and documentation generators I have used seem to render this in a reasonable way, though of course they don't provide any type checking or inspection of the array parameters.

女皇必胜 2024-08-23 11:48:40

目前没有“官方”(如“由多种工具支持”)的方式来做到这一点。

PHP Fig 目前正在 https://groups.google 上讨论该问题.com/d/topic/php-fig/o4ko1XsGtAw/discussion

There is currently no "official" (as in 'supported by multiple tools') way to do this.

The PHP FIG is discussing it at the moment at https://groups.google.com/d/topic/php-fig/o4ko1XsGtAw/discussion

丿*梦醉红颜 2024-08-23 11:48:40

文本描述,无论您想要何种程度的完整性,实际上都是您唯一的选择。您可以根据需要使其清晰易读,但代码分析工具(phpDocumentor、IDE 支持)无法了解您的 $array 在运行时的实际结构。

我同意许多评论者的观点,即以这种方式编写代码可以用编码的便利性换取代码的易读性。

A text description, to whatever degree of completeness you want, is really your only option. You can make it as legible as you want, but code analysis tools (phpDocumentor, IDE support) have no way to know how your $array is actually structured at runtime.

I agree with the many commenters that writing code this way exchanges coding convenience for code legibility.

茶花眉 2024-08-23 11:48:40

我用过类。

<?php
class MyLibrary {
    var $foo;
    var $bar;
    var $baz;

    /**
     * @param MyLibraryConfig|null $config
     */
    function MyLibrary( $config = null ) {
        if ( isset( $config->foo ) ) {
            $this->foo = $config->foo;
        }
        if ( isset( $config->baz ) ) {
            $this->baz = $config->baz;
        }
        if ( isset( $config->bar ) ) {
            $this->bar = $config->bar;
        }
    }
}

/**
 * @property string $foo
 * @property int    $bar
 * @property array  $baz
 */
class MyLibraryConfig {
}

它工作得相当好,主要问题是代码中充斥着特定的类。它们可以嵌套,以便可以重用部分配置。

I've used classes.

<?php
class MyLibrary {
    var $foo;
    var $bar;
    var $baz;

    /**
     * @param MyLibraryConfig|null $config
     */
    function MyLibrary( $config = null ) {
        if ( isset( $config->foo ) ) {
            $this->foo = $config->foo;
        }
        if ( isset( $config->baz ) ) {
            $this->baz = $config->baz;
        }
        if ( isset( $config->bar ) ) {
            $this->bar = $config->bar;
        }
    }
}

/**
 * @property string $foo
 * @property int    $bar
 * @property array  $baz
 */
class MyLibraryConfig {
}

It works fairly well, main problem is that the code becomes littered with specific classes. They can be nested so parts of configuration can be reused.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文