TomTom
TomTom

Reputation: 1885

How to format PHPDoc comment for complex return values

I have a function which returns something like the following structure:

array('form' => $form, 'options' => $options)

How to correctly format this return value in function comment?

My guess is:

@return array('form' => CForm, 'options' => array) ... comment ...

Any suggestions?

Upvotes: 0

Views: 3156

Answers (2)

Jan.
Jan.

Reputation: 1995

When you must return a weird array then document it just simple and easy to understand.

Here's how I would do it. Feel free to use @return array instead of mixed[]:

<?php
/**
 * Method to do something
 *
 * @param Some_User_Class $user
 * @param string $blockUserId
 * @throws Some_Model_Relations_ErrorException
 * @return mixed[] see example
 * @example The returned array consists of this
 *  and that and might have foo and bar.
 *  so this makes me feel like the code should be refactored.
 */
public function unblockUser(Some_User_Class $user, $unblockUserId) {
}

Upvotes: 1

Decent Dabbler
Decent Dabbler

Reputation: 22773

Perhaps a bit overkill, but you could return an object with a specific interface, in stead of a regular array. Something like:

/**
 * @return ReturnObject
 */
public function yourMethod()
{
    return new ReturnObjectImpl( $theForm, $theOptions );
}

interface ReturnObject
{
    public function getCForm();
    public function getOptions();
}

class ReturnObjectImpl
    implements ReturnObject
{
    protected $_form;
    protected $_options;

    public function __construct( CForm $form, array $options )
    {
        $this->_form = $form;
        $this->_options = $options;
    }

    public function getCForm()
    {
        return $this->_form;
    }

    public function getOptions()
    {
        return $this->_options;
    }
}

Upvotes: 0

Related Questions