81

我正在努力编写可读且易于理解的文档,这些文档描述了传递给函数的数组选项的多树结构。

这是一个示例数组结构。

$arr = [
   'fields' => [
       'title' => [
           'name'     => 'Document.title',
           'format'   => 'string',
           'readonly' => true
       ]
   ]
];

上述数组有许多可能的选项,但它被用作理解该结构的函数的参数。

function doSomething(array $arr) { ... }

我想记录应该如何在 PHPDoc 中构造数组,但我不确定正确的方法是什么。

这就是我现在所拥有的。

/**
 * Holds configuration settings for each field in a model.
 * Defining the field options
 *
 * array['fields'] array Defines the feilds to be shown by scaffolding.
 * array['fields'][fieldName] array Defines the options for a field, or just enables the field if array is not applied.
 * array['fields'][fieldName]['name'] string Overrides the field name (default is the array key)
 * array['fields'][fieldName]['model'] string (optional) Overrides the model if the field is a belongsTo assoicated value.
 * array['fields'][fieldName]['width'] string Defines the width of the field for paginate views. Examples are "100px" or "auto"
 * array['fields'][fieldName]['align'] string Alignment types for paginate views (left, right, center)
 * array['fields'][fieldName]['format'] string Formatting options for paginate fields. Options include ('currency','nice','niceShort','timeAgoInWords' or a valid Date() format)
 * array['fields'][fieldName]['title'] string Changes the field name shown in views.
 * array['fields'][fieldName]['desc'] string The description shown in edit/create views.
 * array['fields'][fieldName]['readonly'] boolean True prevents users from changing the value in edit/create forms.
 * array['fields'][fieldName]['type'] string Defines the input type used by the Form helper (example 'password')
 * array['fields'][fieldName]['options'] array Defines a list of string options for drop down lists.
 * array['fields'][fieldName]['editor'] boolean If set to True will show a WYSIWYG editor for this field.
 * array['fields'][fieldName]['default'] string The default value for create forms.
 *
 * @param array $arr (See above)
 * @return Object A new editor object.
 **/

我的问题是,当生成 HTML 文档时,它的格式不是很好。另外,我不确定上面是否清楚地解释了数组结构。

有替代方法吗?

4

9 回答 9

78

这就是我的做法:

/**
 * Class constructor.
 *
 * @param array $params Array containing the necessary params.
 *    $params = [
 *      'hostname'     => (string) DB hostname. Required.
 *      'databaseName' => (string) DB name. Required.
 *      'username'     => (string) DB username. Required.
 *      'password'     => (string) DB password. Required.
 *      'port'         => (int) DB port. Default: 1433.
 *      'sublevel'     => [
 *          'key' => (\stdClass) Value description.
 *      ]
 *    ]
 */
 public function __construct(array $params){}

认为它很干净,很容易理解$params应该是什么。

于 2016-04-22T09:25:23.413 回答
63

为 phpstorm 编写了一个插件,允许指定这样的键:

(基本上只是@siannone 格式的稍微严格的版本)

/**
 * @param array $arr = [
 *     'fields' => [ // Defines the feilds to be shown by scaffolding
 *         $anyKey => [
 *             'name' => 'sale', // Overrides the field name (default is the array key)
 *             'model' => 'customer', // (optional) Overrides the model if the field is a belongsTo associated value.
 *             'width' => '100px', // Defines the width of the field for paginate views. Examples are "100px" or "auto"
 *             'align' => 'center', // Alignment types for paginate views (left, right, center)
 *             'format' => 'nice', // Formatting options for paginate fields. Options include ('currency','nice','niceShort','timeAgoInWords' or a valid Date() format)
 *             'title' => 'Sale', // Changes the field name shown in views.
 *             'desc' => 'A deal another person that results in money', // The description shown in edit/create views.
 *             'readonly' => false, // True prevents users from changing the value in edit/create forms.
 *             'type' => 'password', // Defines the input type used by the Form helper
 *             'options' => ['option1', 'option2'], // Defines a list of string options for drop down lists.
 *             'editor' => false, // If set to True will show a WYSIWYG editor for this field.
 *             'default' => '', // The default value for create forms.
 *         ],
 *     ],
 * ]
 */
public static function processForm($arr)
{
    $fieldName = 'sale';
    $arr['fields'][$fieldName][''];
}

在此处输入图像描述

它还允许指定@return键:

/**
 * @return array [
 *     'success' => true,
 *     'formObject' => new Form,
 *     'errors' => [],
 * ]
 */
public static function processForm($arr);

在此处输入图像描述

于 2018-06-25T21:42:16.527 回答
49

只需添加一些表格即可使其看起来不错且易于理解

/**
 * Holds configuration settings for each field in a model.
 * Defining the field options
 *
 * array['fields']              array Defines the fields to be shown by scaffolding.
 *          [fieldName]         array Defines the options for a field, or just enables the field if array is not applied.
 *              ['name']        string Overrides the field name (default is the array key)
 *              ['model']       string (optional) Overrides the model if the field is a belongsTo associated value.
 *              ['width']       string Defines the width of the field for paginate views. Examples are "100px" or "auto"
 *              ['align']       string Alignment types for paginate views (left, right, center)
 *              ['format']      string Formatting options for paginate fields. Options include ('currency','nice','niceShort','timeAgoInWords' or a valid Date() format)
 *              ['title']       string Changes the field name shown in views.
 *              ['desc']        string The description shown in edit/create views.
 *              ['readonly']    boolean True prevents users from changing the value in edit/create forms.
 *              ['type']        string Defines the input type used by the Form helper (example 'password')
 *              ['options']     array Defines a list of string options for drop down lists.
 *              ['editor']      boolean If set to True will show a WYSIWYG editor for this field.
 *              ['default']     string The default value for create forms.
 *
 * @param array $arr (See above)
 * @return Object A new editor object.
 **/

嵌套列表方法:

<ul>
    <li>
        array['fields'] array Defines the fields to be shown by scaffolding.
        <ul>
            <li>
                [fieldName]             array Defines the options for a field, or just enables the field if array is not applied.
                <ul>
                    <li> ['name']       <i><u>string</u></i> Overrides the field name (default is the array key) </li>
                    <li> ['model']      <i><u>string</u></i> (optional) Overrides the model if the field is a belongsTo associated value.</li>
                    <li> ['width']      <i><u>string</u></i> Defines the width of the field for paginate views. Examples are "100px" or "auto"</li>
                    <li> ['align']      <i><u>string</u></i> Alignment types for paginate views (left, right, center)</li>
                    <li> ['format']     <i><u>string</u></i> Formatting options for paginate fields. Options include ('currency','nice','niceShort','timeAgoInWords' or a valid Date() format)</li>
                    <li> ['title']      <i><u>string</u></i> Changes the field name shown in views.</li>
                    <li> ['desc']       <i><u>string</u></i> The description shown in edit/create views.</li>
                    <li> ['readonly']   <i><u>boolean</u></i> True prevents users from changing the value in edit/create forms.</li>
                    <li> ['type']       <i><u>string</u></i> Defines the input type used by the Form helper (example 'password')</li>
                    <li> ['options']    <i><u>array</u></i> Defines a list of string options for drop down lists.</li>
                    <li> ['editor']     <i><u>boolean</u></i> If set to True will show a WYSIWYG editor for this field.</li>
                    <li> ['default']    <i><u>string</u></i> The default value for create forms.</li>
                </ul>
            </li>
        </ul>
    </li>
 </ul>

结果:

  • array['fields'] array 定义要通过脚手架显示的字段。
    • [fieldName] array 定义字段的选项,或者如果未应用 array,则仅启用该字段。
      • ['name'] 字符串覆盖字段名称(默认为数组键)
      • ['model'] 字符串(可选)如果字段是 belongsTo 关联值,则覆盖模型。
      • ['width'] 字符串定义分页视图的字段宽度。例如“100px”或“auto”
      • ['align'] 字符串分页视图的对齐类型(左、右、中)
      • ['format'] 字符串分页字段的格式选项。选项包括('currency','nice','niceShort','timeAgoInWords' 或有效的 Date() 格式)
      • ['title'] 字符串更改视图中显示的字段名称。
      • ['desc'] 字符串编辑/创建视图中显示的描述。
      • ['readonly'] boolean True 防止用户更改编辑/创建表单中的值。
      • ['type'] string定义表单助手使用的输入类型(例如'password')
      • ['options'] 数组定义下拉列表的字符串选项列表。
      • ['editor'] boolean如果设置为 True 将显示该字段的所见即所得编辑器。
      • ['default'] 字符串创建表单的默认值。

如果你想让它看起来花哨,用一点 Css 就会创造奇迹!xd

于 2013-03-14T16:35:59.443 回答
42

在广泛接受的密钥类型文档格式中,我想在这里提几个流行的:

诗篇/ PHPStan / phan格式

/** @param array{foo: string, bar: int} $args */

作为奖励,还可以使用他们的工具进行静态代码分析

WordPress格式

/**
 * @param array $args {
 *     Optional. An array of arguments.
 *
 *     @type type $key Description. Default 'value'. Accepts 'value', 'value'.
 *                     (aligned with Description, if wraps to a new line)
 *     @type type $key Description.
 * }
 */

两者都由deep-assoc-completion插件支持

于 2020-04-22T16:07:29.740 回答
32

你可以使用对象而不是数组吗?这将使文档变得容易。

class Field {

    /**
     * The name of the thing.
     * @var string
     */
    protected $name;

    protected $model;
    protected $width;
    //...

    public function getName() {...}
    public function setName() {...}
    //...
}

class FieldList implements SomeKindOfIterator {

    /**
     * Some fields.
     * @var Field[]
     */
    protected $fields = array();

    /**
     * ...
     */
    public function push(Field $field) {
         $this->fields[] = $field;
    }

    //...
}

然后,您可以在需要该类的地方使用类型提示。

/**
 * Do something.
 * @param FieldList $field_list The field.
 */
function doSomething(FieldList $field_list) {...}
于 2013-03-14T16:29:49.427 回答
16

Markdown Syntax for Object Notation (MSON) 可能是更好的选择。

例子

/**
 * @param array $config
 *   + app (string, required) - app directory name
 *   + view (string, required) - view directory name
 *   + type (enum[string]) - site type
 *     + pc - PC version
 *     + wap - mobile version
 *     + other - other, default value
 *   + table_prefix (string) - database table prefix
 */
于 2017-05-19T04:40:11.283 回答
3

我比较喜欢这个:

 * @param array $doc
 *          'type'=>Doc::MY_DOC_TYPE,
 *          'created'=>$now,
 *          'modified'=>$now

我只是将代码粘贴到它被初始化的地方,快速而简单。

于 2014-11-14T19:47:29.887 回答
2

PHP 中的数组实际上更像是匿名结构。

对于任意数据结构,有许多模式验证器,但不幸的是,它在类型提示中并未得到广泛支持。也许一些常见的方案有插件?问题是事情只在一个或几个地方工作。您可能会为您的 IDE 找到正确的工作,但运行静态分析,这一切都可能陷入困境。

需要注意将事物分开,以便如果可能,其他不支持方案的工具(例如通过插件)将简单地忽略它。

PHPDoc 往往在所有地方都受到支持,但也非常有限。

经常有提案,但没有真正好的标准。大多数解决方案都是非标准的、没有得到广泛支持的、有限制的部分破解或纯粹肤浅的(文档)。

在特定的地方有特定的实现,但没有广泛和专用于 PHP 本身。尽管您可以在 PHP IDE 中创建自己的模式,但往往缺乏像样的代码桥,即使是那些名称中有 PHP 的模式。

您应该单独定义您的字段结构。您的外部数据结构是伪代码@key fields field[],而不是表示为多维数组。从概念上讲,您可以做到以下几点:

@start struct custom
@key \stdClass *
@end struct

@start struct fields
@key string hostname
@key string databaseName
@key string password
@key int port=1433
@key custom|undefined sublevel
@end struct

@start struct connection
@key fields fields
@end struct

或者从C中窃取然后发明一种语言......

struct connection {
    struct fields {
        string hostname;
        string databaseName;
        string password;
        int port = 1433;
        custom optional sublevel {
            stdClass *;
        };
    };
};

您可以发明基于结构的模式以允许平面和嵌套。嵌套应该是默认设置,事物应该只被定义为可访问,因为它们需要重用。

一种不寻常的方法是使用对象。这不一定需要使用诸如数组访问之类的接口。在 PHP 中,对象为属性包装了一个数组。可以将数组转换为对象(没有实现,只有属性)并返回。

如果您将对象用作关联数组($array[$key] 与 $object->{$key}),那么您可以制作虚拟对象以至少欺骗 IDE...

final class PersonStruct {
    /** @var int */
    public $x;

    /** @var int $y */

    public int $z;
}

这三个选项中可能起作用也可能不起作用取决于所使用的工具。

然后你可以撒谎...

/** @var PersonStruct $ps */
$ps = (object)['x' => 0, 'y' => 1, 'z' => 2];

/**
 * @param PersonStruct $ps
 * @return PersonStruct
 */
function f(object $ps):object {
    return $ps;
}

/**
 * @param PersonStruct $ps
 * @return PersonStruct
 */
function f(stdClass $ps):stdClass {
    return $ps;
}

问题在于它意味着将数组转换为对象。这既有性能影响,也有通过值传递到引用传递的变化。哪个更快是有争议的。理论上数组应该更快,尽管对象在默认情况下具有引用的好处,并且与 PHP 不同,JSON 将对象与数组分开可以更好地工作。

对象也不支持在类型提示方面可能设置得不好的属性,即使对象中的属性只是一个 PHP 数组(使用->{key}而不是[key])。还有可能发生其他奇怪的事情。

如果性能是一个真正的问题,您可以将 PHP 转换为编译语言。以同样的方式,您可以扩展接口以使对象可编译,您可以对可能使用 OOP 和自动完成的所有操作执行相同的操作,但随后可以通过指定它包装的属性来执行内联类的等效操作,然后使用反射几乎可以用匹配方法的内容替换使用,需要一些额外的位(标记要内联或转换为程序的内容,要包装的单个属性或多个属性等)。

这个概念类似于装箱和拆箱。如果您真的对 SA 支持和对 IDE(自动完成、检查等)、分析器、工具等的广泛支持感到疯狂,那么它可能是唯一的方法。

于 2019-08-02T12:24:50.463 回答
1

因为这纯粹是显示而不是指令,并且应该在文档中保留空格格式,所以我倾向于使用缩进而不是字符墙来提高可读性:

 * array['fields'] array Defines the feilds to be shown by scaffolding.
 *           [fieldName] array Defines the options for a field, or just enables
 *                             the field if array is not applied.
 *                 ['name'] string Overrides the field name (default is the
 *                                  array key)
 *                 ['model'] string (optional) Overrides the model if the field is
 *                                  a belongsTo assoicated value.
 *                 ['width'] string Defines the width of the field for paginate
 *                                  views.
 *                                  Examples are "100px" or "auto"
 *                 ['align'] string Alignment types for paginate views (left, 
 *                                 right, center)
 *                 ['format'] string Formatting options for paginate fields.
 *                                   Options include 'currency', 'nice',
 *                                  'niceShort', 'timeAgoInWords' or a valid 
 *                                  Date() format)
 *                 ['title'] string Changes the field name shown in views.
 *                 ['desc'] string The description shown in edit/create views.
 *                 ['readonly'] boolean True prevents users from changing the
 *                                 value in edit/create forms.
 *                 ['type'] string Defines the input type used by the Form helper
 *                                  (example 'password')
 *                 ['options'] array Defines a list of string options for drop-
 *                                  down lists.
 *                 ['editor'] boolean If set to True will show a WYSIWYG editor
 *                                  for this field.
 *                 ['default'] string The default value for create forms.

尽管使用带有缩进的实际 PHP 数组定义更加简洁

于 2013-03-14T16:29:11.403 回答