Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it allowed to set return to json in documentation?

Tags:

json

string

php

So, while documenting a php code I'm writing, I stopped where I usually said @return string The json output, on functions that I actually returned json.

So, I was wondering if it is right to set

*
* @return json
*/
public function test()
{
    $test = array('hola' => array('en' => 'hello', 'ro' => 'salut'));
    return json_encode($test);
}

instead of

*
* @return string
*/
public function test()
{
    $test = array('hola' => array('en' => 'hello', 'ro' => 'salut'));
    return json_encode($test);
}

I searched for related question, and overlooked manuals, but non that I have seen, had mentioned my doubt.

http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.return.pkg.html

Update

Just as a reference, of where I got all this started. I saw a couple of times, the following:

*
* @return View
*/

So, I guess that is a proper return?

like image 540
Alex Avatar asked Jul 10 '13 15:07

Alex


People also ask

What does. JSON return?

json() The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON .

What does the. JSON function do?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

How do I get JSON response?

json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error). Python requests are generally used to fetch the content from a particular resource URI.


2 Answers

As orangepill commented, you should use the type string and add JSON in the description.

@return string JSON

PHPDoc Manual

@return datatype description
@return datatype1|datatype2 description

In reference to the datatype, the manual states

The datatype should be a valid PHP type (int, string, bool, etc), a class name for the type of object returned, or simply "mixed". If you want to explicitly show multiple possible return types, list them pipe-delimited without spaces (e.g. "@return int|string")

like image 113
user20232359723568423357842364 Avatar answered Oct 23 '22 23:10

user20232359723568423357842364


"json" is not a primitive type in PHP, or in fact any type. You need to document returned types, not what the content of these types mean. If you specify json as return "type", this implies an object of class json, because json has no other meaning in PHP.

You can only document it as returning a string.

like image 38
deceze Avatar answered Oct 23 '22 22:10

deceze