Arrays phpDoc是否有办法将对象数组记录为参数?
在phpDoc生成的文档中,我可以使用Arrays phpDoc是否有办法将对象数组记录为参数?,arrays,phpdoc,custom-type,Arrays,Phpdoc,Custom Type,在phpDoc生成的文档中,我可以使用 @param CustomType $variablename 这很有效。但是,我当前记录的代码需要CustomType[]参数,即所述CustomType的数组。我希望文档清楚地表明需要一个数组,但是当我使用 @param CustomType[] $variablename phpDoc不再识别该类型,因此无法链接到其定义。在这种情况下,这非常重要——我正在记录一个API,它需要提供一些相当复杂的类型 我为此尝试了几种不同的语法,它们要么将条目视为
@param CustomType $variablename
这很有效。但是,我当前记录的代码需要CustomType[]参数,即所述CustomType的数组。我希望文档清楚地表明需要一个数组,但是当我使用
@param CustomType[] $variablename
phpDoc不再识别该类型,因此无法链接到其定义。在这种情况下,这非常重要——我正在记录一个API,它需要提供一些相当复杂的类型
我为此尝试了几种不同的语法,它们要么将条目视为单独的变量类型,要么在文档中破坏类型识别
除此之外,我将在参数注释中对其进行注释,但在类型中显示参数的数组性似乎更清晰
编辑
使用phpDocumentor 2(与DocBlox合并)时
语法是有效的,正如@Styx的答案中所指出的,PhpStorm支持使用该语法进行类型暗示
已接受的答案已适当更新。您能做的最好的事情是:
@param array $variablename an array of {@link CustomType} objects
这将帮助读者实现$variablename的真正数据类型,同时指示数组包含的内容
当涉及到使用$variablename中的成员并希望显示CustomType的属性/方法时,这不足以帮助IDE自动完成。目前确实没有办法获得这种行为。新版PHP文档支持
/**@var sometype[]*/
语法。更复杂的是:/**@var(sometype | othertype)[*/
。
PHPStorm也支持此语法。请参见以下示例: 其中描述了输入参数的数组结构
/**
* Set the OAuth 2.0 access token using the string that resulted from calling authenticate()
* or Google_Client#getAccessToken().
* @param string $accessToken JSON encoded string containing in the following format:
* {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
* "expires_in":3600, "id_token":"TOKEN", "created":1320790426}
*/
/**
* Insert a new file. (files.insert)
*
* @param Google_DriveFile $postBody
* @param array $optParams Optional parameters.
*
* @opt_param bool convert Whether to convert this file to the corresponding Google Docs format.
* @opt_param string targetLanguage Target language to translate the file to. If no sourceLanguage is provided, the API will attempt to detect the language.
* @opt_param string sourceLanguage The language of the original file to be translated.
* @opt_param string ocrLanguage If ocr is true, hints at the language to use. Valid values are ISO 639-1 codes.
* @opt_param bool pinned Whether to pin the head revision of the uploaded file.
* @opt_param bool ocr Whether to attempt OCR on .jpg, .png, or .gif uploads.
* @opt_param string timedTextTrackName The timed text track name.
* @opt_param string timedTextLanguage The language of the timed text.
* @return Google_DriveFile
*/
phpdoc文档注释位于 排列 未知类型的变量集合。可以指定数组成员的类型,有关详细信息,请参阅有关数组的章节
而且。。。没有链接,也没有“关于数组”的章节。不,这看起来像是即将推出的功能。注意:这个答案是对其他答案的补充 要记录对象数组,可以使用
@param ClassName[]$classInstance Description
。
但是请注意,在PHP7中,可以使用参数类型声明(类型提示),在本例中,类型必须是array
例如:
提示:您还应该使用
declare(strict\u types=1)代码>已批准,正在努力使数据类型签名语法为“CustomObject[]”=>“一个CustomObject成员数组”。一旦它在文档生成器中可用,我希望IDE可能会遵循它来支持它的含义。这正是我所愿意接受的,但是docblox的链接可能值得遵循。谢谢可能重复:不是真的;它们是互补的——他问的是IDE中的类型暗示,而我问的是phpDoc文档本身——tpe暗示在我的例子中只是一个很好的副作用,请参阅-一种方法是在-phpdoc网站/参考或建议的PSR5文档中未提及@opt_param受支持-无论是谁编写了您复制的docblock,都可以输入@this_wont_work_param(!)来记录数组的键,这一点正在讨论中
/**
* Set the OAuth 2.0 access token using the string that resulted from calling authenticate()
* or Google_Client#getAccessToken().
* @param string $accessToken JSON encoded string containing in the following format:
* {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
* "expires_in":3600, "id_token":"TOKEN", "created":1320790426}
*/
/**
* Insert a new file. (files.insert)
*
* @param Google_DriveFile $postBody
* @param array $optParams Optional parameters.
*
* @opt_param bool convert Whether to convert this file to the corresponding Google Docs format.
* @opt_param string targetLanguage Target language to translate the file to. If no sourceLanguage is provided, the API will attempt to detect the language.
* @opt_param string sourceLanguage The language of the original file to be translated.
* @opt_param string ocrLanguage If ocr is true, hints at the language to use. Valid values are ISO 639-1 codes.
* @opt_param bool pinned Whether to pin the head revision of the uploaded file.
* @opt_param bool ocr Whether to attempt OCR on .jpg, .png, or .gif uploads.
* @opt_param string timedTextTrackName The timed text track name.
* @opt_param string timedTextLanguage The language of the timed text.
* @return Google_DriveFile
*/