0

次のように、渡された引数に応じてミューテーターまたはアクセサーとして動作する関数が与えられます。

// in PHP, you can pass any number of arguments to a function...
function cache($cacheName) {
    $arguments = func_get_args();

    if (count($arguments) >= 2) {   // two arguments passed. MUTATOR.

        $value = $arguments[1];             // use the second as the value
        $this->cache[$cacheName] = $value;  // *change* the stored value

    } else {    // 1 argument passed, ACCESSOR
        return $this->cache[$cacheName];  // *get* the stored value
    }
}

cache('foo', 'bar');  // nothing returned
cache('foo')          // 'bar' returned

これをPHPDocまたは同様の自動ドキュメント作成者でどのように文書化しますか?私はもともと次のように書いていました:

/**
 *  Blah blah blah, you can use this as both a mutator and an accessor:
 *    As an accessor:
 *    @param   $cacheName   name of the variable to GET
 *    @return  string       the value...
 *
 *    As a mutator:
 *    @param   $cacheName   name of the variable to SET
 *    @param   $value       the value to set
 *    @return  void
 */

ただし、これをphpDocで実行すると、2つのreturnタグがあり、最初の@param $cacheName説明が2番目の説明で上書きされるため、文句を言います。

これを回避する方法はありますか?

4

1 に答える 1

2

ご存知のように、1つの関数の2つの異なるシグニチャを文書化することはできません。ただし、 phpDocumentorを使用する場合にできることは、オプションの関数パラメーター複数の可能な戻り型を文書化することです。

/**
 * Blah blah blah, you can use this as both an accessor and a mutator, e.g.
 * <code>cache('name') // get cache value</code>
 *   and
 * <code>cache('name', 'value') // set new cache value</code>.
 *
 * @param   string  $cacheName  name of the variable to GET|SET
 * @param   string  $value      optional new value
 *
 * @return  string|void value of $cacheName or, in case of mutator, void
 */

わかりやすくするために、使用例も含めます。

于 2010-05-20T07:42:29.003 に答える