PageRenderTime 56ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/PSR.md

http://github.com/phpDocumentor/phpDocumentor2
Markdown | 1464 lines | 1076 code | 388 blank | 0 comment | 0 complexity | 4caca718510d19c52ae0b0cbdeb5417c MD5 | raw file
Possible License(s): LGPL-3.0
  1. PSR-n: PHPDoc
  2. =============
  3. Author(s):
  4. Mike van Riel (@mvriel) <mike.vanriel@naenius.com>
  5. Acknowledgements:
  6. The authors wish to thank Chuck Burgess (@ashnazg),
  7. Gary Jones (@GaryJ) and all other people who commented on
  8. various versions of this proposal.
  9. Obsoletes:
  10. De-facto PHPDoc Standard (http://www.phpdoc.org)
  11. ## Table Of Contents
  12. 1. Introduction
  13. 2. Conventions Used In This Document
  14. 3. Definitions
  15. 4. Basic Principles
  16. 5. The PHPDoc Format
  17. 5.1. Short Description
  18. 5.2. Long Description
  19. 5.3. Tags
  20. 5.3.1. Tag Name
  21. 5.3.2. Tag Signature
  22. 5.4. Examples
  23. 6. Inheritance
  24. 6.1. Class Or Interface
  25. 6.2. Function Or Method
  26. 6.3. Constant Or Property
  27. 7. Tags
  28. 7.1. @api
  29. 7.2. @author
  30. 7.3. @category [deprecated]
  31. 7.4. @copyright
  32. 7.5. @deprecated
  33. 7.6. @example
  34. 7.7. @global
  35. 7.8. @internal
  36. 7.9. @license
  37. 7.10. @link
  38. 7.11. @method
  39. 7.12. @package
  40. 7.13. @param
  41. 7.14. @property
  42. 7.15. @return
  43. 7.16. @see
  44. 7.17. @since
  45. 7.18. @subpackage [deprecated]
  46. 7.19. @throws
  47. 7.20. @todo
  48. 7.21. @type
  49. 7.22. @uses
  50. 7.23. @var
  51. 7.24. @version
  52. Appendix A. Types
  53. Appendix B. Differences Compared With The De-facto PHPDoc Standard
  54. ## 1. Introduction
  55. The main purpose of this PSR is to provide a complete and formal definition of
  56. the PHPDoc standard. This PSR deviates from its predecessor, the de-facto PHPDoc
  57. Standard associated with [phpDocumentor 1.x](http://www.phpdoc.org), to provide
  58. support for newer features in the PHP language and to address some of the
  59. shortcomings of its predecessor.
  60. This document SHALL NOT:
  61. * Describe a standard for implementing annotations via PHPDoc. Although it does
  62. offer versatility which makes it possible to create a subsequent PSR based on
  63. current practices. See [chapter 5.3](#53-tags) for more information on this
  64. topic.
  65. * Describe best practices or recommendations for Coding Standards on the
  66. application of the PHPDoc standard. This document is limited to a formal
  67. specification of syntax and intention.
  68. ## 2. Conventions Used In This Document
  69. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
  70. "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
  71. document are to be interpreted as described in
  72. [RFC 2119](http://www.ietf.org/rfc/rfc2119.txt).
  73. ## 3. Definitions
  74. * "PHPDoc" is a section of documentation which provides information on several
  75. aspects of a "Structural Element".
  76. > It is important to note that the PHPDoc and the DocBlock are two separate
  77. > entities. The DocBlock is the combination of a DocComment, which is a type
  78. > of comment, and a PHPDoc entity. It is the PHPDoc entity that describes the
  79. > Short Description, Long Description and Tags.
  80. * "Structural Element" is a collection of Programming Constructs which SHOULD be
  81. preceded by a DocBlock. The collection contains the following constructs:
  82. * namespace
  83. * require(_once)
  84. * include(_once)
  85. * class
  86. * interface
  87. * trait
  88. * function (including methods)
  89. * property
  90. * constant
  91. * variables, both local and global scope.
  92. It is RECOMMENDED to precede a "Structural Element" with a DocBlock with its
  93. definition and not with each usage.
  94. Example:
  95. ```php
  96. /** @type int This is a counter. */
  97. $int = 0;
  98. // there should be no docblock here
  99. $int++;
  100. ```
  101. Or:
  102. ```php
  103. /**
  104. * This class acts as an example on where to position a DocBlock.
  105. */
  106. class Foo
  107. {
  108. /** @type string|null Should contain a description if available */
  109. protected $description = null;
  110. /**
  111. * This method sets a description.
  112. *
  113. * @param string $description A text with a maximum of 80 characters.
  114. *
  115. * @return void
  116. */
  117. public function setDescription($description)
  118. {
  119. // there should be no docblock here
  120. $this->description = $description;
  121. }
  122. }
  123. ```
  124. An example of use that falls beyond the scope of this Standard is to document
  125. the variable in a foreach explicitly; many IDEs use this information to help
  126. you with auto-completion.
  127. This Standard does not cover this specific instance as a foreach is not
  128. considered to be a "Structural Element" but a Control Flow structure.
  129. ```php
  130. /** @type \Sqlite3 $sqlite */
  131. foreach($connections as $sqlite) {
  132. // there should be no docblock here
  133. $sqlite->open('/my/database/path');
  134. <...>
  135. }
  136. ```
  137. * "DocComment" is a special type of comment which starts with `/**`, ends
  138. with `*/` and may contain any number of lines in between.
  139. In case a DocComment spans multiple lines should every line start with an
  140. asterisk that is aligned with the first asterisk of the opening clause.
  141. Single line example:
  142. ```php
  143. /** <...> */
  144. ```
  145. Multiline example:
  146. ```php
  147. /**
  148. * <...>
  149. */
  150. ```
  151. * "DocBlock" is a "DocComment" containing a single "PHPDoc" and represents the
  152. basic in-source representation.
  153. * "Tag" is a single piece of meta information regarding a "Structural Element"
  154. or a component thereof.
  155. * "Type" is the determination of what type of data is associated with an element.
  156. This is commonly used when determining the exact values of arguments, constants,
  157. properties and more.
  158. See Appendix A for more detailed information about types.
  159. ## 4. Basic Principles
  160. * A PHPDoc MUST always be contained in a "DocComment"; the combination of these
  161. two is called a "DocBlock".
  162. * A DocBlock MUST precede a "Structural Element"
  163. > An exception to this principle is the File-level DocBlock which must be
  164. > placed at the top of a PHP source code file.
  165. ## 5. The PHPDoc Format
  166. The PHPDoc format has the following [ABNF](http://www.ietf.org/rfc/rfc5234.txt)
  167. definition:
  168. PHPDoc = [short-description] [long-description] [tags]
  169. short-description = *CHAR ("." 1*CRLF / 2*CRLF)
  170. long-description = 1*(CHAR / inline-tag) 1*CRLF ; any amount of characters
  171. ; with inline tags inside
  172. tags = *(tag 1*CRLF)
  173. inline-tag = "{" tag "}"
  174. tag = "@" tag-name [tag-details]
  175. tag-name = (ALPHA / "\") *(ALPHA / DIGIT / "\" / "-" / "_")
  176. tag-details = *SP (SP tag-description / tag-signature)
  177. tag-description = 1*CHAR
  178. tag-signature = "(" *tag-argument ")"
  179. tag-argument = *SP 1*CHAR [","] *SP
  180. Examples of use are included in chapter 5.4.
  181. ### 5.1. Short Description
  182. A short description MUST contain an abstract of the "Structural Element"
  183. defining the purpose. It is recommended for short descriptions to span a single
  184. line or at most two but not more than that.
  185. A short description must end with either a full stop (.) followed by a line
  186. break or two sequential line breaks.
  187. If a long description is provided then it MUST be preceded by a short
  188. description. Otherwise the long description will be considered being the short
  189. description until the stop of the short description is encountered.
  190. Tags do not necessarily have to be preceded by a short description.
  191. ### 5.2. Long Description
  192. The long description is OPTIONAL but SHOULD be included when the
  193. "Structural Element", which this DocBlock precedes, contains more operations, or
  194. more complex operations, than can be described in the short description alone.
  195. Any application parsing the long description SHOULD support the Markdown
  196. mark-up language for this field so that it is possible for the author to provide
  197. formatting and a clear way of representing code examples.
  198. Common uses for the long description are (amongst others):
  199. * To provide more detail to the casual reader than the short description on
  200. what this method does.
  201. * To specify of what child elements an input or output array, or object, is
  202. composed.
  203. * To provide a set of common use cases or scenarios in which the
  204. "Structural Element" may be applied.
  205. ### 5.3. Tags
  206. Tags provide a way for authors to supply concise meta-data regarding the
  207. succeeding "Structural Element". They commonly consist of a name followed by
  208. white-space and a description. The description MAY span multiple lines and MAY
  209. follow a strict format dictated by the type of tag, as indicated by its name.
  210. The meta-data supplied by tags could result in a change of behaviour of the
  211. succeeding "Structural Element", in which case the term "Annotation" is
  212. commonly used instead of "Tag".
  213. A variation of this is where, instead of a description, a tag-signature is used;
  214. in most cases the tag will in fact be an annotation. The tag-signature is able
  215. to provide the annotation with parameters regarding its operation.
  216. If a tag-signature is present then there MUST NOT be a description present in
  217. the same tag.
  218. Annotations will not be described in further detail in this specification as
  219. this falls beyond the scope. This specification provides a basis on top of which
  220. annotations may be implemented.
  221. #### 5.3.1. Tag Name
  222. Tag names indicate what type of information is represented by this tag or, in
  223. case of annotations, which behaviour must be injected into the succeeding
  224. "Structural Element".
  225. In support of annotations, it is allowable to introduce a set of tags designed
  226. specifically for an individual application or subset of applications (and thus
  227. not covered by this specification).
  228. These tags, or annotations, MUST provide a namespace by either
  229. * prefixing the tag name with a PHP-style namespace, or by
  230. * prefixing the tag name with a single vendor-name followed by a hyphen.
  231. Example of a tag name prefixed with a php-style namespace (the prefixing slash
  232. is OPTIONAL):
  233. @\Doctrine\Orm\Mapping\Entity()
  234. > *Note*: The PHPDoc Standard DOES NOT make assumptions on the meaning of a tag
  235. > unless specified in this document or subsequent additions or extensions.
  236. >
  237. > This means that you CAN use namespace aliases as long as a prefixing namespace
  238. > element is provided. Thus the following is legal as well:
  239. >
  240. > @Mapping\Entity()
  241. >
  242. > Your own library or application may check for namespace aliases and make a
  243. > FQCN from this; this has no impact on this standard.
  244. > *Important*: Individual Documentation Generation Applications (DGAs) MAY
  245. > interpret namespaces that are registered with that application and apply
  246. > custom behaviour.
  247. Example of a tag name prefixed with a vendor name and hyphen:
  248. @phpdoc-event transformer.transform.pre
  249. Tag names that are not prefixed with a vendor or namespace MUST be described in
  250. this specification (see chapter 7) and/or any official addendum.
  251. #### 5.3.2. Tag Signature
  252. Tag signatures are commonly used for annotations to supply additional meta-data
  253. specific to the current tag.
  254. The supplied meta-data can influence the behavior of the owning annotation and
  255. as such influence the behavior of the succeeding "Structural Element".
  256. The contents of a signature are to be determined by the tag type (as described
  257. in the tag-name) and fall beyond the scope of this specification. However, a
  258. tag-signature MUST NOT be followed by a description or other form of meta-data.
  259. ### 5.4. Examples
  260. The following examples serve to illustrate the basic use of DocBlocks; it is
  261. advised to read through the list of tags in chapter 7.
  262. A complete example could look like the following example:
  263. ```php
  264. /**
  265. * This is a short description.
  266. *
  267. * This is a long description. It may span multiple lines
  268. * or contain 'code' examples using the _Markdown_ markup
  269. * language.
  270. *
  271. * @see Markdown
  272. *
  273. * @param int $parameter1 A parameter description.
  274. * @param \Exception $e Another parameter description.
  275. *
  276. * @\Doctrine\Orm\Mapper\Entity()
  277. *
  278. * @return string
  279. */
  280. function test($parameter1, $e)
  281. {
  282. }
  283. ```
  284. It is also allowed to omit the long description:
  285. ```php
  286. /**
  287. * This is a short description.
  288. *
  289. * @see Markdown
  290. *
  291. * @param int $parameter1 A parameter description.
  292. * @param \Exception $parameter2 Another parameter description.
  293. *
  294. * @\Doctrine\Orm\Mapper\Entity()
  295. *
  296. * @return string
  297. */
  298. function test($parameter1, $parameter2)
  299. {
  300. }
  301. ```
  302. Or even omit the tags section as well (though in the following example is not
  303. encouraged as you are missing information on the parameters and return value):
  304. ```php
  305. /**
  306. * This is a short description.
  307. */
  308. function test($parameter1, $parameter2)
  309. {
  310. }
  311. ```
  312. A DocBlock may also span a single line as shown in the following example.
  313. ```php
  314. /** @var \ArrayObject $array */
  315. public $array = null;
  316. ```
  317. ## 6. Inheritance
  318. PHPDoc's also have the ability to inherit information when the succeeding
  319. "Structural Element" has a super-element (such as a super-class or a method with
  320. the same name in a super-class or implemented in a super-interface).
  321. Every "Structural Element" MUST inherit the following PHPDoc parts by default:
  322. * [Short description](#51-short-description)
  323. * [Long description](#52-long-description)
  324. * A specific subset of [tags](#53-tags)
  325. * [@version](#724-version)
  326. * [@author](#72-author)
  327. * [@copyright](#74-copyright)
  328. Each specific "Structural Element" MUST also inherit a specialized subset as
  329. defined in the sub-chapters.
  330. The PHPDoc parts MUST NOT be inherited when a replacement is available in the
  331. sub-element. The exception to this rule is when the {@inheritdoc} inline tag is
  332. present in the long description. When present the parser MUST insert the
  333. super-element's long description at the location of the {@inheritdoc} inline
  334. tag.
  335. Inheritance takes place from the root of a class hierarchy graph to its leafs.
  336. This means that anything inherited in the bottom of the tree MUST 'bubble' up to
  337. the top unless overridden.
  338. > Note: a special circumstance here would be when the Long Description must be
  339. > overridden but the Short Description should stay intact. It would be difficult
  340. > for a reader to distinguish which is overridden.
  341. >
  342. > In this case MUST the writer use the {@inheritdoc} inline tag as
  343. > Short Description and override the Long Description with the intended text.
  344. >
  345. > Without the {@inheritdoc} inline tag MUST the reader interpret any text
  346. > as if the Short Description would be overridden and MAY long description
  347. > appear overridden if the block of text contains a Short Description ending
  348. > as defined in the ABNF.
  349. ### 6.1. Class Or Interface
  350. In addition to the inherited descriptions and tags as defined in this chapter's
  351. root MUST a class or interface inherit the following tags:
  352. * [@package](#712-package)
  353. A class or interface SHOULD inherit the following deprecated tags if supplied:
  354. * [@subpackage](#718-subpackage)
  355. The @subpackage MUST NOT be inherited if the @package annotation of the
  356. super-class (or interface) is not the same as the @package of the child class
  357. (or interface).
  358. Example:
  359. ```php
  360. /**
  361. * @package Framework
  362. * @subpackage Controllers
  363. */
  364. class Framework_ActionController
  365. {
  366. <...>
  367. }
  368. /**
  369. * @package My
  370. *
  371. class My_ActionController extends Framework_ActionController
  372. {
  373. <...>
  374. }
  375. ```
  376. In the example above the My_ActionController MUST not inherit the subpackage
  377. _Controllers_.
  378. ### 6.2. Function Or Method
  379. In addition to the inherited descriptions and tags as defined in this chapter's
  380. root MUST a class or interface inherit the following tags:
  381. * [@param](#713-param)
  382. * [@return](#715-return)
  383. * [@throws](#719-throws)
  384. ### 6.3. Constant Or Property
  385. In addition to the inherited descriptions and tags as defined in this chapter's
  386. root MUST a class or interface inherit the following tags:
  387. * [@type](#721-type)
  388. A constant or property SHOULD inherit the following deprecated tags if supplied:
  389. * [@var](#723-var)
  390. ## 7. Tags
  391. Unless specifically mentioned in the description MAY each tag occur zero or more
  392. times in each "DocBlock".
  393. ### 7.1. @api
  394. The @api tag is used to declare "Structural Elements" as being suitable for
  395. consumption by third parties.
  396. #### Syntax
  397. @api
  398. #### Description
  399. The @api tag represents those "Structural Elements" with a public visibility
  400. which are intended to be the public API components for a library or framework.
  401. Other "Structural Elements" with a public visibility serve to support the
  402. internal structure and are not recommended to be used by the consumer.
  403. The exact meaning of "Structural Elements" tagged with @api MAY differ per
  404. project. It is however RECOMMENDED that all tagged "Structural Elements" SHOULD
  405. NOT change after publication unless the new version is tagged as breaking
  406. Backwards Compatibility.
  407. #### Examples
  408. ```php
  409. /**
  410. * This method will not change until a major release.
  411. *
  412. * @api
  413. *
  414. * @return void
  415. */
  416. function showVersion()
  417. {
  418. <...>
  419. }
  420. ```
  421. ### 7.2. @author
  422. The @author tag is used to document the author of any "Structural Element".
  423. #### Syntax
  424. @author [name] [<email address>]
  425. #### Description
  426. The @author tag can be used to indicate who has created a"Structural Element"
  427. or has made significant modifications to it. This tag MAY also contain an
  428. e-mail address. If an e-mail address is provided it MUST follow
  429. the author's name and be contained in chevrons, or angle brackets, and MUST
  430. adhere to the syntax defined in RFC 2822.
  431. #### Examples
  432. ```php
  433. /**
  434. * @author My Name
  435. * @author My Name <my.name@example.com>
  436. */
  437. ```
  438. ### 7.3. @category [deprecated]
  439. The @category tag is used to organize groups of packages together but is
  440. deprecated in favour of occupying the top-level with the @package tag.
  441. As such usage of this tag is NOT RECOMMENDED.
  442. #### Syntax
  443. @category [description]
  444. #### Description
  445. The @category tag was meant in the original de-facto Standard to group several
  446. "Structural Elements" their @packages into one category. These categories could
  447. then be used to aid in the generation of API documentation.
  448. This was necessary since the @package tag as defined in the original Standard did
  449. not contain more then one hierarchy level; since this has changed this tag SHOULD
  450. NOT be used.
  451. Please see the documentation for `@package` for details of its usage.
  452. This tag MUST NOT occur more than once in a "DocBlock".
  453. #### Examples
  454. ```php
  455. /**
  456. * Page-Level DocBlock
  457. *
  458. * @category MyCategory
  459. * @package MyPackage
  460. */
  461. ```
  462. ### 7.4. @copyright
  463. The @copyright tag is used to document the copyright information of any
  464. "Structural element".
  465. #### Syntax
  466. @copyright <description>
  467. #### Description
  468. The @copyright tag defines who holds the copyright over the "Structural Element".
  469. The copyright indicates with this tag applies to the "Structural Element" to
  470. which it applies and all child elements unless otherwise noted.
  471. The format of the description if governed by the coding standard of each
  472. individual project. It is RECOMMENDED to mention the year or years which are
  473. covered by this copyright and the organization involved.
  474. #### Examples
  475. ```php
  476. /**
  477. * @copyright 1997-2005 The PHP Group
  478. */
  479. ```
  480. ### 7.5. @deprecated
  481. The @deprecated tag is used to indicate which 'Structural elements' are
  482. deprecated and are to be removed in a future version.
  483. #### Syntax
  484. @deprecated [<version>] [<description>]
  485. #### Description
  486. The @deprecated tag declares that the associated 'Structural elements' will
  487. be removed in a future version as it has become obsolete or its usage is otherwise
  488. not recommended.
  489. This tag MAY also contain a version number up till which it is guaranteed to be
  490. included in the software. Starting with the given version will the function be
  491. removed or may be removed without further notice.
  492. If is RECOMMENDED (but not required) to provide an additional description stating
  493. why the associated element is deprecated.
  494. If it is superceded by another method it is RECOMMENDED to add a @see tag in the
  495. same 'PHPDoc' pointing to the new element.
  496. #### Examples
  497. ```php
  498. /**
  499. * @deprecated
  500. * @deprecated 1.0.0
  501. * @deprecated No longer used by internal code and not recommended.
  502. * @deprecated 1.0.0 No longer used by internal code and not recommended.
  503. */
  504. ```
  505. ### 7.6. @example
  506. The @example tag is used to link to an external source code file which contains
  507. an example of use for the current "Structural element". An inline variant exists
  508. with which code from an example file can be shown inline with the Long
  509. Description.
  510. #### Syntax
  511. @example [URI] [<description>]
  512. or inline:
  513. {@example [URI] [:<start>..<end>]}
  514. #### Description
  515. The example tag refers to a file containing example code demonstrating the
  516. purpose and use of the current "Structural element". Multiple example tags may
  517. be used per "Structural element" in case several scenarios are described.
  518. The URI provided with the example tag is resolved according to the following
  519. rules:
  520. 1. If a URI is proceeded by a scheme or root folder specifier such as `phar://`,
  521. `http://`, `/` or `C:\` then it is considered to be an absolute path.
  522. 2. If the URI is deemed relative and a location for the example files has been
  523. provided then the path relative to the given location is resolved.
  524. 3. If the previous path was not readable or the user has not provided a path
  525. than the application should try to search for a folder examples in the
  526. same folder as the source file featuring the example tag. If found then an
  527. attempt to resolve the path by combining the relative path given in the
  528. example tag and the found folder should be made.
  529. 4. If the application was unable to resolve a path given the previous rules than
  530. it should check if a readable folder 'examples' is found in the root folder
  531. of the project containing the "Structural Element" his source file.
  532. > The root folder of a project is the highest folder common to all files
  533. > that are being processed by a consuming application.
  534. If a consumer intends to display the contents of the example file then it is
  535. RECOMMENDED to use a syntax highlighting solution to improve user experience.
  536. The rules as described above apply to both the inline as the normal tags. The
  537. inline tag has 2 additional parameters with which to limit which lines of code
  538. are shown in the Long Description. Due to this consuming applications MUST
  539. show the example code in case an inline example tag is used.
  540. The start and end argument may be omitted but the ellipsis should remain in
  541. case either is used to give a clear visual indication. The same rules as
  542. specified with the [substr](http://nl.php.net/manual/en/function.substr.php)
  543. function of PHP are in effect with regards to the start and end limit.
  544. > A consuming application MAY choose to support the limit format as used in the
  545. > previous standard but it is deprecated per this PSR.
  546. > The previous syntax was: {@example [URI] [<start>] [<end>]} and did not support
  547. > the same rules as the substr function.
  548. #### Examples
  549. ```php
  550. /**
  551. * Counts the number of items.
  552. * {@example http://example.com/foo-inline.https:2..8}
  553. *
  554. * @example http://example.com/foo.phps
  555. *
  556. * @return integer Indicates the number of items.
  557. */
  558. function count()
  559. {
  560. <...>
  561. }
  562. ```
  563. ### 7.7. @global
  564. ### 7.8. @internal
  565. The @internal tag is used to denote that the associated "Structural Element" is
  566. a structure internal to this application or library. It may also be used inside
  567. a long description to insert a piece of text that is only applicable for
  568. the developers of this software.
  569. #### Syntax
  570. @internal
  571. or inline:
  572. {@internal [description]}}
  573. The inline version of this tag may, contrary to other inline tags, contain
  574. text but also other inline tags. To increase readability and ease parsing should
  575. the tag be terminated with a double closing brace, instead of a single one.
  576. #### Description
  577. The @internal tag can be used as counterpart of the @api tag, indicating that
  578. the associated "Structural Element" is used purely for the internal workings of
  579. this piece of software.
  580. When generating documentation from PHPDoc comments it is RECOMMENDED to hide the
  581. associated element unless the user has explicitly indicated that internal elements
  582. should be included.
  583. An additional use of @internal is to add internal comments or additional
  584. description text inline to the Long Description. This may be done, for example,
  585. to withhold certain business-critical or confusing information when generating
  586. documentation from the source code of this piece of software.
  587. #### Examples
  588. Mark the count function as being internal to this project:
  589. ```php
  590. /**
  591. * @internal
  592. *
  593. * @return integer Indicates the number of items.
  594. */
  595. function count()
  596. {
  597. <...>
  598. }
  599. ```
  600. ```php
  601. /**
  602. * Counts the number of Foo.
  603. *
  604. * {@internal Silently adds one extra Foo to compensate for lack of Foo }}
  605. *
  606. * @return integer Indicates the number of items.
  607. */
  608. function count()
  609. {
  610. <...>
  611. }
  612. ```
  613. ### 7.9. @license
  614. The @license tag is used to indicate which license is applicable for the associated
  615. 'Structural Elements'.
  616. #### Syntax
  617. @license [<url>] [name]
  618. #### Description
  619. The @license tag provides the user with the name and URL of the license that is
  620. applicable to 'Structural Elements' and any of their child elements.
  621. It is NOT RECOMMENDED to apply @license tags to any 'PHPDoc' other than
  622. file-level PHPDocs as this may cause confusion which license applies at which
  623. time.
  624. Whenever multiple licenses apply MUST there be one @license tag per applicable
  625. license.
  626. Instead of providing a URL MAY an identifier as identified in the
  627. [SPDX Open Source License Registry](http://www.spdx.org/licenses/) be provided
  628. and SHOULD this be interpreted as if having the URL mentioned in the registry.
  629. #### Examples
  630. ```php
  631. /**
  632. * @license MIT
  633. * @license http://www.spdx.org/licenses/MIT MIT License
  634. */
  635. ```
  636. ### 7.10. @link
  637. The @link tag indicates a custom relation between the associated
  638. "Structural Element" and a website, which is identified by an absolute URI.
  639. #### Syntax
  640. @link [URI] [description]
  641. or inline
  642. {@link [URI] [description]}
  643. #### Description
  644. The @link tag can be used to define a relation, or link, between the
  645. "Structural Element", or part of the long description when used inline,
  646. to an URI.
  647. The URI MUST be complete and welformed as specified in RFC 2396.
  648. The @link tag MAY have a description appended to indicate the type of relation
  649. defined by this occurrence.
  650. #### Examples
  651. ```php
  652. /**
  653. * @link http://example.com/my/bar Documentation of Foo.
  654. *
  655. * @return integer Indicates the number of items.
  656. */
  657. function count()
  658. {
  659. <...>
  660. }
  661. ```
  662. ```php
  663. /**
  664. * This method counts the occurences of Foo.
  665. *
  666. * When no more Foo ({@link http://example.com/my/bar}) are given this
  667. * function will add one as there must always be one Foo.
  668. *
  669. * @return integer Indicates the number of items.
  670. */
  671. function count()
  672. {
  673. <...>
  674. }
  675. ```
  676. ### 7.11. @method
  677. The @method allows a class to know which 'magic' methods are callable.
  678. #### Syntax
  679. @method [return type] [name]([type] [parameter], [...]) [description]
  680. #### Description
  681. The @method tag is used in situation where a class contains the __call() magic
  682. method and defines some definite uses.
  683. An example of this is a child class whose parent has a __call() to have dynamic
  684. getters or setters for predefined properties. The child knows which getters and
  685. setters need to be present but relies on the parent class to use the __call()
  686. method to provide it. In this situation, the child class would have a @method
  687. tag for each magic setter or getter method.
  688. The @method tag allows the author to communicate the type of the arguments and
  689. return value by including those types in the signature.
  690. When the intended method does not have a return value then the return type MAY
  691. be omitted; in which case 'void' is implied.
  692. @method tags MUST NOT be used in a PHPDoc that is not associated with a
  693. *class* or *interface*.
  694. #### Examples
  695. ```php
  696. class Parent
  697. {
  698. public function __call()
  699. {
  700. <...>
  701. }
  702. }
  703. /**
  704. * @method string getString()
  705. * @method void setInteger(integer $integer)
  706. * @method setString(integer $integer)
  707. */
  708. class Child extends Parent
  709. {
  710. <...>
  711. }
  712. ```
  713. ### 7.12. @package
  714. The @package tag is used to categorize "Structural Elements" into logical
  715. subdivisions.
  716. #### Syntax
  717. @package [level 1]\[level 2]\[etc.]
  718. #### Description
  719. The @package tag can be used as a counterpart or supplement to Namespaces.
  720. Namespaces provide a functional subdivision of "Structural Elements" where the
  721. @package tag can provide a *logical* subdivision in which way the elements can
  722. be grouped with a different hierarchy.
  723. If, across the board, both logical and functional subdivisions are equal is
  724. it NOT RECOMMENDED to use the @package tag, to prevent maintenance overhead.
  725. Each level in the logical hierarchy MUST separated with a backslash (`\`) to
  726. be familiar to Namespaces. A hierarchy MAY be of endless depth but it is
  727. RECOMMENDED to keep the depth at less or equal than six levels.
  728. Please note that the @package applies to different "Structural Elements"
  729. depending where it is defined.
  730. 1. If the @package is defined in the *file-level* DocBlock then it only applies
  731. to the following elements in the applicable file:
  732. * global functions
  733. * global constants
  734. * global variables
  735. * requires and includes
  736. 2. If the @package is defined in a *namespace-level* or *class-level* DocBlock
  737. then the package applies to that namespace, class or interface and their
  738. contained elements.
  739. This means that a function which is contained in a namespace with the
  740. @package tag assumes that package.
  741. This tag MUST NOT occur more than once in a "DocBlock".
  742. #### Examples
  743. ```php
  744. /**
  745. * @package PSR\Documentation\API
  746. */
  747. ```
  748. ### 7.13. @param
  749. ### 7.14. @property
  750. ### 7.15. @return
  751. The @return tag is used to document the return value of functions or methods.
  752. #### Syntax
  753. @return <"Type"> [description]
  754. #### Description
  755. With the @return tag it is possible to document the return type and function of a
  756. function or method. When provided it MUST contain a "Type" (See Appendix A)
  757. to indicate what is returned; the description on the other hand is OPTIONAL yet
  758. RECOMMENDED in case of complicated return structures, such as associative arrays.
  759. The @return tag MAY have a multi-line description and does not need explicit
  760. delimiting.
  761. It is RECOMMENDED when documenting to use this tag with every function and
  762. method. Exceptions to this recommendation are:
  763. 1. **constructors**, the @return tag MAY be omitted here, in which case an
  764. interpreter MUST interpret this as if `@return self` is provided.
  765. 2. **functions and methods without a `return` value**, the @return tag MAY be
  766. omitted here, in which case an interpreter MUST interpret this as if
  767. `@return void` is provided.
  768. This tag MUST NOT occur more than once in a "DocBlock" and is limited to the
  769. "DocBlock" of a "Structural Element" or type method or function.
  770. #### Examples
  771. ```php
  772. /**
  773. * @return integer Indicates the number of items.
  774. */
  775. function count()
  776. {
  777. <...>
  778. }
  779. ```
  780. ```php
  781. /**
  782. * @return string|null The label's text or null if none provided.
  783. */
  784. function getLabel()
  785. {
  786. <...>
  787. }
  788. ```
  789. ### 7.16. @see
  790. ### 7.17. @since
  791. #### Description
  792. It is NOT RECOMMENDED for this tag to occur more than once in a "DocBlock".
  793. ### 7.18. @subpackage [deprecated]
  794. This tag MUST NOT occur more than once in a "DocBlock".
  795. ### 7.19. @throws
  796. ### 7.20. @todo
  797. ### 7.21. @type
  798. You may use the @type tag to document the "Type" of the following
  799. "Structural Elements":
  800. * Constants, both class and global
  801. * Properties
  802. * Variables, both global and local scope
  803. #### Syntax
  804. @type <"Type"> [description]
  805. #### Description
  806. The @type tag is the successor of the @var tag and serves the purpose of defining
  807. which type of data is contained in a Constant, Property or Variable.
  808. Each Constant or Property *definition* MUST be preceded by a DocBlock
  809. containing the @type tag. Each Variable, where the type is ambiguous or unknown,
  810. SHOULD be preceded by a DocBlock containing the @type tag. Any other
  811. variable MAY be preceeded with a similar DocBlock.
  812. It is NOT RECOMMENDED to use the @var alias unless it is necessary in order for
  813. the application, or associated tools, to function correctly.
  814. This tag MUST NOT occur more than once in a "DocBlock".
  815. #### Examples
  816. ```php
  817. /** @type int This is a counter. */
  818. $int = 0;
  819. // there should be no docblock here
  820. $int++;
  821. ```
  822. Or:
  823. ```php
  824. class Foo
  825. {
  826. /** @type string|null Should contain a description if available */
  827. protected $description = null;
  828. public function setDescription($description)
  829. {
  830. // there should be no docblock here
  831. $this->description = $description;
  832. }
  833. }
  834. ```
  835. Another example is to document the variable in a foreach explicitly; many IDEs
  836. use this information to help you with auto-completion:
  837. ```php
  838. /** @type \Sqlite3 $sqlite */
  839. foreach($connections as $sqlite) {
  840. // there should be no docblock here
  841. $sqlite->open('/my/database/path');
  842. <...>
  843. }
  844. ```
  845. ### 7.22. @uses
  846. ### 7.23. @var [deprecated]
  847. Is a **deprecated** alias for `@type`, please see the documentation for `@type`
  848. for details of its usage.
  849. This tag MUST NOT occur more than once in a "DocBlock".
  850. ### 7.24. @version
  851. ## Appendix A. Types
  852. ### ABNF
  853. type-expression = 1*(array-of-type-expression|array-of-type|type ["|"])
  854. array-of-type-expression = "(" type-expression ")[]"
  855. array-of-type = type "[]"
  856. type = class-name|keyword
  857. class-name = 1*CHAR
  858. keyword = "string"|"integer"|"int"|"boolean"|"bool"|"float"
  859. |"double"|"object"|"mixed"|"array"|"resource"
  860. |"void"|"null"|"callback"|"false"|"true"|"self"
  861. ### Additional details
  862. When a "Type" is used the user will expect a value, or set of values, as
  863. detailed below.
  864. When the "Type" may consist of multiple types then these MUST be separated
  865. with the vertical bar sign (|). Any application supporting this specification MUST
  866. recognize this and split the "Type" before processing.
  867. For example: `@return int|null`
  868. The value represented by "Type" can be an array. The type MUST be defined
  869. following the format of one of the following options:
  870. 1. unspecified, no definition of the contents of the represented array is given.
  871. Example: `@return array`
  872. 2. specified containing a single type, the Type definition informs the reader of
  873. the type of each array element. Only one type is then expected as element for
  874. a given array.
  875. Example: `@return int[]`
  876. Please note that _mixed_ is also a single type and with this keyword it is
  877. possible to indicate that each array element contains any possible type.
  878. 3. specified containing multiple types, the Type definition informs the reader
  879. of the type of each array element. Each element can be of any of the given
  880. types.
  881. Example: `@return (int|string)[]`
  882. The supported atomic types are either a *valid class name* or *keyword*.
  883. ### Valid Class Name
  884. A valid class name seen from the context where this type is mentioned. Thus
  885. this may be either a Fully Qualified Class Name (FQCN) or if present in a
  886. namespace a local name.
  887. > It is RECOMMENDED for applications to expand any local name into a FQCN
  888. > for easier processing and comparisons.
  889. The element to which this type applies is either an instance of this class
  890. or an instance of a class that is a (sub-)child to the given class.
  891. > Due to the above nature it is RECOMMENDED for applications that
  892. > collect and shape this information to show a list of child classes
  893. > with each representation of the class. This would make it obvious
  894. > for the user which classes are acceptable as type.
  895. ### Keyword
  896. A keyword defining the purpose of this type. Not every element is determined
  897. by a class but still worth of a classification to assist the developer in
  898. understanding the code covered by the DocBlock.
  899. > Note: most of these keywords are allowed as class names in PHP and as
  900. > such are hard to distinguish from real classes. As such the keywords MUST
  901. > be lowercase, as most class names start with an uppercase first character,
  902. > and you SHOULD NOT use classes with these names in your code.
  903. > There are more reasons to not name classes with the names of these
  904. > keywords but that falls beyond the scope of this specification.
  905. The following keywords are recognized by this PSR:
  906. 1. 'string', the element to which this type applies is a string of
  907. binary characters.
  908. 2. 'integer' or 'int', the element to which this type applies is a whole
  909. number or integer.
  910. 3. 'boolean' or 'bool', the element to which this type applies only has
  911. state true or false.
  912. 4. 'float' or 'double', the element to which this type applies is a continuous,
  913. or real, number.
  914. 5. 'object', the element to which this type applies is the instance of an
  915. undetermined class.
  916. This could be considered an alias for providing the class stdClass, as this
  917. is the base class of all classes, but the intention of the type differs.
  918. Providing stdClass will imply the intention that the related element contains
  919. an actual object of class stdClass or direct descendant, whereas object
  920. implies that it is completely unknown of which class the contained
  921. object will be.
  922. 6. 'mixed', the element to which this type applies can be of any type as
  923. specified here. It is not known on compile time which type will be used.
  924. 7. 'array', the element to which this type applies is an array of values.
  925. 8. 'resource', the element to which this type applies is a resource per
  926. the definition of PHP at
  927. http://www.php.net/manual/en/language.types.resource.php.
  928. 9. 'void', this type is commonly only used when defining the return type of a
  929. method or function.
  930. The basic definition is that the element indicated with this type does not
  931. contain a value and the user should not rely on any retrieved value.
  932. For example:
  933. ```php
  934. /**
  935. * @return void
  936. */
  937. function outputHello()
  938. {
  939. echo 'Hello world';
  940. }
  941. ***
  942. In the example above no return statement is specified and thus is the return
  943. value not determined.
  944. Example 2:
  945. ```php
  946. /**
  947. * @param boolean $hi when true 'Hello world' is echo-ed.
  948. *
  949. * @return void
  950. */
  951. function outputHello($quiet)
  952. {
  953. if ($quiet} {
  954. return;
  955. }
  956. echo 'Hello world';
  957. }
  958. ```
  959. In this example the function contains a return statement without a given
  960. value. Because there is no actual value specified does this also constitute
  961. as type 'void'.
  962. 10. 'null', the element to which this type applies is a NULL value or, in
  963. technical terms, does not exist.
  964. A big difference compared to void is that this type is used in any situation
  965. where the described element may at any given time contain an explicit NULL
  966. value.
  967. Example:
  968. ```php
  969. /**
  970. * @return null
  971. */
  972. function foo()
  973. {
  974. echo 'Hello world';
  975. return null;
  976. }
  977. ```
  978. This type is commonly used in conjunction with another type to indicate that
  979. it is possible that nothing is returned.
  980. Example:
  981. ```
  982. /**
  983. * @param boolean $create_new When true returns a new stdClass.
  984. *
  985. * @return stdClass|null
  986. */
  987. function foo($create_new)
  988. {
  989. if ($create_new) {
  990. return new stdClass();
  991. }
  992. return null;
  993. }
  994. ```
  995. 11. 'callback', the element to which this type applies is a pointer to a
  996. function call. This may be any type of callback as defined in the PHP manual
  997. at http://php.net/manual/en/language.pseudo-types.php.
  998. 12. 'false' or 'true', the element to which this type applies will have
  999. the value true or false. No other value will be returned from this
  1000. element.
  1001. 13. 'self', the element to which this type applies is of the same Class,
  1002. or any of its children, as which the documented element is originally
  1003. contained.
  1004. For example:
  1005. Method C() is contained in class A. The DocBlock states
  1006. that its return value is of type `self`. As such method C()
  1007. returns an instance of class A.
  1008. This may lead to confusing situations when inheritance is involved.
  1009. For example (previous example situation still applies):
  1010. Class B extends Class A and does not redefine method C(). As such
  1011. it is possible to invoke method C() from class B.
  1012. In this situation ambiguity may arise as `self` could be interpreted as
  1013. either class A or B. In these cases `self` MUST be interpreted as being
  1014. an instance of the Class where the DocBlock containing the `self` type
  1015. is written or any of its child classes.
  1016. In the examples above `self` MUST always refer to class A or B, since
  1017. it is defined with method C() in class A.
  1018. If method C() was to be redefined in class B, including the type
  1019. definition in the DocBlock, then `self` would refer to class B or any
  1020. of its children.
  1021. > Due to the above nature it is RECOMMENDED for applications that
  1022. > collect and shape this information to show a list of child classes
  1023. > with each representation of the class. This would make it obvious
  1024. > for the user which classes are acceptable as type.
  1025. ## Appendix B. Differences Compared With The De-facto PHPDoc Standard
  1026. This specification intends to improve upon the de-facto PHPDoc standard by
  1027. expanding syntax and deprecating redundant elements.
  1028. The following changes may be observed and a concise rationale is provided.
  1029. ### Syntax Changes
  1030. #### Added Support For Namespaces
  1031. #### Added 'self' As "Type"
  1032. #### Added Array Notation For "Type"
  1033. #### Expanded Basic Syntax To Support Doctrine2/Symfony2 Style Annotations
  1034. ### Tag Additions
  1035. #### @api
  1036. Framework and library developers needed a way for consumers to distinguish
  1037. between public methods intended for internal use or public methods fit for
  1038. consuming.
  1039. #### @type
  1040. This tag is meant to replace @var and better convey the meaning and intention:
  1041. to tell the user which *type* of data is contained in the succeeding
  1042. "Structural Element".
  1043. Additionally constants did not have a tag which identified the type of the
  1044. contained data. Instead of re-using @var, which is inappropriate since
  1045. a constant is not variable, or defining a *new* tag @const, which would make
  1046. usage of the right tag even more complex, a new tag @type was created to replace
  1047. @var and serve as new tag for constants.
  1048. ### Tag Changes
  1049. #### @package
  1050. The @package tag replaces the function of the @category and @subpackage by
  1051. allowing a layered hierarchy using the '\' operator.
  1052. See the documentation on the @package tag for details.
  1053. #### @ignore
  1054. The @ignore tag has not been included in this specification as it is not a tag
  1055. but an annotation specific to phpDocumentor and derived documentation
  1056. generators.
  1057. #### @link
  1058. In the de-facto standard, introduced by phpDocumentor, @link supported the use
  1059. of internal locators for "Structural Element". This use is actually superceded
  1060. by the @see tag and thus removed.
  1061. It is NOT RECOMMENDED to use the @link tag for referencing "Structural Elements".
  1062. #### @method
  1063. phpDocumentor had a different signature where the method name was repeated. Since
  1064. this repetition only caused overhead it has been removed in this specification.
  1065. ### Deprecated Tags
  1066. #### @category
  1067. The @package tag replaces the function of the @category and @subpackage by
  1068. allowing a layered hierarchy using the '\' operator.
  1069. See the documentation on the @package tag for details.
  1070. #### @subpackage
  1071. The @package tag replaces the function of the @category and @subpackage by
  1072. allowing a layered hierarchy using the '\' operator.
  1073. See the documentation on the @package tag for details.
  1074. #### @var
  1075. The @var tag is considered ambiguous and non-semantic; as such it is superceded
  1076. by the @type tag which also allows the type of constants to be documented.