在PHPDoc中记录数组选项的最佳方法?

Rea*_*lar 49 php cakephp phpdoc

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

这是一个示例数组结构.

$arr = [
   'fields' => [
       'title' => [
           'name'     => 'Document.title',
           'format'   => 'string',
           'readonly' => true
       ]
   ]
];
Run Code Online (Sandbox Code Playgroud)

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

function doSomething(array $arr) { ... }
Run Code Online (Sandbox Code Playgroud)

我想记录如何在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.
 **/
Run Code Online (Sandbox Code Playgroud)

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

有替代方法吗?

sia*_*one 54

派对有点太晚了,但这就是我这样做的方式:

/**
 * 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){}
Run Code Online (Sandbox Code Playgroud)

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

  • @JM-AGMS 我可能会做这样的事情:https://pastebin.com/0ebYpfpQ 我认为在评论中添加评论看起来不太好,但它让它从其他参数键中脱颖而出。 (2认同)

ale*_*ion 37

只需添加一些列表就可以使它看起来很好且易于理解

/**
 * 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.
 **/
Run Code Online (Sandbox Code Playgroud)

嵌套列表方法:

<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>
Run Code Online (Sandbox Code Playgroud)

结果:

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

如果你想要它看起来很花哨,有点Css它会创造奇迹!XD


Art*_*sun 35

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

/**
 * @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][''];
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

它还允许指定@return键:

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

在此输入图像描述


Art*_*sun 27

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

Psalm / PHPStan / phan格式

/** @param array{foo: string, bar: int} $args */
Run Code Online (Sandbox Code Playgroud)

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

WordPress格式

/** @param array{foo: string, bar: int} $args */
Run Code Online (Sandbox Code Playgroud)

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

  • [PHPStan](https://phpstan.org/writing-php-code/phpdoc-types#array-shapes)+1,因为 PHPStorm 默认支持它,不需要额外的插件。作者的数组看起来像 ``` /** @param array{fields: array{title: array{name: string, format: string, readonly: bool}}} $arr */ ``` (3认同)

jjo*_*jok 24

你能用对象代替数组吗?这将使文档变得容易.

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;
    }

    //...
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用需要类的类型提示.

/**
 * Do something.
 * @param FieldList $field_list The field.
 */
function doSomething(FieldList $field_list) {...}
Run Code Online (Sandbox Code Playgroud)

  • 必须对此 +1 并添加,请考虑移动到带有声明的公共变量的预定义公共类,作为关联数组的替代方案。它不仅速度更快,并且对于非常大的集合使用更少的内存,您还可以在类和属性上使用 phpdoc 注释和标签来生成一些不错的文档。 (2认同)
  • `Field[]` 是正确的表示法,而不是 `array[Field]` (2认同)

jk2*_*k2K 10

对象表示法(MSON)的Markdown语法可能是更好的选择.

/**
 * @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
 */
Run Code Online (Sandbox Code Playgroud)