Best way to document Array options in PHPDoc?

前端 未结 9 581
天命终不由人
天命终不由人 2020-12-04 09:57

I\'m struggling to write readable and easy to understand documentation that describes the multi-tree structure for Array options that are passed to a function.

Here

相关标签:
9条回答
  • 2020-12-04 10:31

    I wrote a plugin for phpstorm that allows specifying keys like this:

    (basically just a slightly stricter version of @siannone's format)

    /**
     * @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][''];
    }
    

    It allows to specify @return keys as well:

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

    0 讨论(0)
  • 2020-12-04 10:33

    Markdown Syntax for Object Notation (MSON) may be a better choice.

    example

    /**
     * @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
     */
    
    0 讨论(0)
  • 2020-12-04 10:34

    Can you use objects instead of arrays? That would make documentation easy.

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

    Then you can use a type hint where the class is required.

    /**
     * Do something.
     * @param FieldList $field_list The field.
     */
    function doSomething(FieldList $field_list) {...}
    
    0 讨论(0)
提交回复
热议问题