php - Is there a way to specify a mandatory array key/index via PHPDoc?

855

If a function requires an incoming array to have a specific key/index, is there an eloquent way to express it via comments, specifically PHPDoc?

For example:

/**
 * Just an example function
 * @param array $arr My Example Array
 */

public function myFunction( $arr ){
    if(!array_key_exists('mykey', $arr)){
         echo 'Damnit, we needed an array that had mykey as an index!';
    }
}
944

Answer

Solution:

You could write a list of such keys, and place it in the @param description, or put it in the long description of the method's docblock. There is nothing "automated" or "rigid" that phpDocumentor could really do in the documentation here, e.g. link to some other documented element.

558

Answer

Solution:

No, if you need a specific variable, make it a separate parameter. In that case, if the parameter is empty, an error is thrown.

You can't specify the data-type of array values in PHP. So there is noeed to document that via PHPDoc.

784

Answer

Solution:

NO there is not. Best to just put it in the comments section.

People are also looking for solutions to the problem: php - Adding oembed plugin to 2amigos ckeditor widget in yii2

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.