@@ -15,11 +15,6 @@ PSR-5: PHPDoc
15
15
- [ 5.3.1. Tag Specialization] ( #531-tag-specialization )
16
16
- [ 5.3.2. Tag Name] ( #532-tag-name )
17
17
- [ 5.4. Examples] ( #54-examples )
18
- - [ Appendix A. Types] ( #appendix-a-types )
19
- - [ ABNF] ( #abnf )
20
- - [ Details] ( #details )
21
- - [ Valid Class Name] ( #valid-class-name )
22
- - [ Keyword] ( #keyword )
23
18
24
19
## 1. Introduction
25
20
@@ -45,10 +40,11 @@ interpreted as described in [RFC 2119][RFC2119].
45
40
preceded by a DocBlock. The collection contains the following constructs:
46
41
47
42
* ` require ` / ` include ` (and their ` \_once ` variants)
48
- * ` class ` / ` interface ` / ` trait `
43
+ * ` class ` / ` interface ` / ` trait ` / ` enum `
49
44
* ` function ` (both standalone functions and class methods)
50
45
* variables (local and global scope) and class properties
51
46
* constants (global constants via ` define ` and class constants)
47
+ * cases
52
48
53
49
It is RECOMMENDED to precede a "Structural Element" with a DocBlock where it
54
50
is defined. It is common practice to have the DocBlock precede a Structural
@@ -147,40 +143,6 @@ interpreted as described in [RFC 2119][RFC2119].
147
143
148
144
* "Tag" is a single piece of meta information regarding a "Structural Element".
149
145
150
- * "Type" is the determination of what type of data is associated with an element.
151
- This is used to determine the exact data type (primitive, class, object) of
152
- arguments, properties, constants, etc.
153
-
154
- See Appendix A for more detailed information about types.
155
-
156
- * "FQSEN" is an abbreviation for Fully Qualified Structural Element Name. This
157
- notation expands on the Fully Qualified Class Name and adds a notation to
158
- identify class / interface / trait members and re-apply the principles of the
159
- FQCN to Interfaces, Traits, Functions and global Constants.
160
-
161
- The following notations can be used per type of "Structural Element":
162
-
163
- - * Namespace* : ` \My\Space `
164
- - * Function* : ` \My\Space\myFunction() `
165
- - * Constant* : ` \My\Space\MY_CONSTANT `
166
- - * Class* : ` \My\Space\MyClass `
167
- - * Interface* : ` \My\Space\MyInterface `
168
- - * Trait* : ` \My\Space\MyTrait `
169
- - * Method* : ` \My\Space\MyClass::myMethod() `
170
- - * Property* : ` \My\Space\MyClass::$my_property `
171
- - * Class Constant* : ` \My\Space\MyClass::MY_CONSTANT `
172
-
173
- A FQSEN has the following [ ABNF] [ RFC5234 ] definition:
174
-
175
- FQSEN = fqnn / fqcn / constant / method / property / function
176
- fqnn = "\" [name] *("\" [name])
177
- fqcn = fqnn "\" name
178
- constant = ((fqnn "\") / (fqcn "::")) name
179
- method = fqcn "::" name "()"
180
- property = fqcn "::$" name
181
- function = fqnn "\" name "()"
182
- name = (ALPHA / "_") *(ALPHA / DIGIT / "_")
183
-
184
146
## 4. Basic Principles
185
147
186
148
* A PHPDoc MUST always be contained in a "DocComment"; the combination of these
@@ -384,189 +346,6 @@ A DocBlock may also span a single line:
384
346
public $array = null;
385
347
```
386
348
387
- ## Appendix A. Types
388
-
389
- ### ABNF
390
-
391
- A Type has the following [ ABNF] [ RFC5234 ] definition:
392
-
393
- type-expression = type *("|" type) *("&" type)
394
- type = class-name / keyword / array
395
- array = (type / array-expression) "[]"
396
- array-expression = "(" type-expression ")"
397
- class-name = ["\"] label *("\" label)
398
- label = (ALPHA / %x7F-FF) *(ALPHA / DIGIT / %x7F-FF)
399
- keyword = "array" / "bool" / "callable" / "false" / "float" / "int" / "iterable" / "mixed" / "never"
400
- keyword =/ "null" / "object" / "resource" / "self" / "static" / "string" / "true" / "void" / "$this"
401
-
402
- ### Details
403
-
404
- When a "Type" is used, the user will expect a value, or set of values, as detailed below.
405
-
406
- When the "Type" consists of multiple types, then these MUST be separated with
407
- either the vertical bar (|) for union type or the ampersand (&) for intersection
408
- type. Any interpreter supporting this specification MUST recognize this and
409
- split the "Type" before evaluating.
410
-
411
- Union type example:
412
- > ` @return int|null `
413
-
414
- Intersection type example:
415
- > ` @var \MyClass&\PHPUnit\Framework\MockObject\MockObject $myMockObject `
416
-
417
- #### Arrays
418
-
419
- The value represented by "Type" can be an array. The type MUST be defined
420
- following one of the following options:
421
-
422
- 1 . unspecified: no definition of the contents of the array is given.
423
- Example: ` @return array `
424
-
425
- 2 . specified as a specific type: each member of the array is the same one type.
426
- Example: ` @return int[] `
427
-
428
- Note that ` mixed ` is also a single type and thus can explicitly indicate that
429
- each member is any possible type.
430
-
431
- 3 . specified as containing multiple explicit types: each member can be of any
432
- of the given types.
433
- Example: ` @return (int|string)[] `
434
-
435
- ### Valid Class Name
436
-
437
- A valid class name is based on the context where this type is mentioned. This
438
- may be a Fully Qualified Class Name (FQCN) or a local name if present in a
439
- namespace.
440
-
441
- The element to which this type applies is either an instance of this class
442
- or an instance of a class that is a sub/child to the given class.
443
-
444
- > It is RECOMMENDED for applications that collect and shape this information to
445
- > show a list of child classes with each representation of the class. This makes
446
- > it more obvious for the user which classes are acceptable as this type.
447
-
448
- ### Keyword
449
-
450
- A keyword defines the purpose of this type. Not every element is determined by a
451
- class, but it is still worthy of classification to assist the developer in
452
- understanding the code covered by the DocBlock.
453
-
454
- > Some of these keywords are allowed as class names in PHP and can be difficult
455
- > to distinguish from real classes. As such, the keywords MUST be lowercase, as
456
- > most class names start with an uppercase first character... it is RECOMMENDED
457
- > that you not use classes with these names in your code.
458
-
459
- The following keywords are recognized by this PSR:
460
-
461
- 1 . ` bool ` : the element to which this type applies only has state ` TRUE ` or ` FALSE ` .
462
-
463
- 2 . ` int ` : the element to which this type applies is a whole number or integer.
464
-
465
- 3 . ` float ` : the element to which this type applies is a continuous, or real, number.
466
-
467
- 4 . ` string ` : the element to which this type applies is a string of binary characters.
468
-
469
- 5 . ` object ` : the element to which this type applies is the instance of an undetermined class.
470
-
471
- 6 . ` array ` : the element to which this type applies is an array of values.
472
-
473
- 7 . ` iterable ` : the element to which this type applies is an array or Traversable object per the [ definition of PHP] [ PHP_ITERABLE ] .
474
-
475
- 8 . ` resource ` : the element to which this type applies is a resource per the [ definition of PHP] [ PHP_RESOURCE ] .
476
-
477
- 9 . ` mixed ` : the element to which this type applies can be of any type as specified here. It is not known at compile
478
- time which type will be used.
479
-
480
- 10 . ` void ` : this type is commonly only used when defining the return type of a method or function, indicating
481
- "nothing is returned", and thus the user should not rely on any returned value.
482
-
483
- ``` php
484
- /**
485
- * @return void
486
- */
487
- function outputHello()
488
- {
489
- echo 'Hello world';
490
- }
491
- ```
492
-
493
- 11. `null`: the element to which this type applies is a `NULL` value or, in technical terms, does not exist.
494
-
495
- Compared to `void`, this type is used in any situation where the described element may at
496
- any given time contain an explicit `NULL` value.
497
-
498
- ```php
499
- /**
500
- * @return null
501
- */
502
- function foo()
503
- {
504
- echo 'Hello world';
505
- return null;
506
- }
507
- ```
508
-
509
- ```php
510
- /**
511
- * @param bool $create_new When true returns a new stdClass.
512
- *
513
- * @return stdClass|null
514
- */
515
- function foo($create_new)
516
- {
517
- if ($create_new) {
518
- return new stdClass();
519
- }
520
- return null;
521
- }
522
- ```
523
-
524
- 12. `callable`: the element to which this type applies is a pointer to a function call. This may be any type of callable
525
- as per the [definition of PHP][PHP_CALLABLE].
526
-
527
- 13. `false` or `true`: the element to which this type applies will have the exact value `TRUE` or `FALSE`. No other value will
528
- be returned from this element.
529
-
530
- 14. `self`: the element to which this type applies is of the same class in which the documented element is originally
531
- contained.
532
-
533
- **Example:**
534
-
535
- > Method *c* is contained in class *A*. The DocBlock states that its return value is of type `self`. As such, method
536
- > *c* returns an instance of class *A*.
537
-
538
- This may lead to confusing situations when inheritance is involved.
539
-
540
- **Example (previous example situation still applies):**
541
-
542
- > Class *B* extends class *A* and does not redefine method *c*. As such, it is possible to invoke method *c* from
543
- > class *B*.
544
-
545
- In this situation, ambiguity may arise as `self` could be interpreted as either class *A* or *B*. In these cases,
546
- `self` MUST be interpreted as being an instance of the class where the DocBlock containing the `self` type is
547
- written.
548
-
549
- In the examples above, `self` MUST always refer to class *A*, since it is defined with method *c* in class *A*.
550
-
551
- > Due to the above nature, it is RECOMMENDED for applications that collect and shape this information to show a list
552
- > of child classes with each representation of the class. This would make it obvious for the user which classes are
553
- > acceptable as type.
554
-
555
- 15. `static`: the element to which this type applies is of the same class in which the documented element is contained,
556
- or, when encountered in a subclass, is of type of that subclass instead of the original class.
557
-
558
- This keyword behaves the same way as the [keyword for late static binding][PHP_OOP5LSB] (not the static method,
559
- property, nor variable modifier) as defined by PHP.
560
-
561
- 16. `$this`: the element to which this type applies is the same exact instance as the current class in the given
562
- context. As such, this type is a stricter version of `static`, because the returned instance must not only be
563
- of the same class but also the same instance.
564
-
565
- This type is often used as return value for methods implementing the [Fluent Interface][FLUENT] design pattern.
566
-
567
- 17. `never`: denotes that element isn't going to return anything and always throws exception or terminates
568
- the program abnormally (such as by calling the library function `exit`).
569
-
570
349
[ RFC2119 ] : https://tools.ietf.org/html/rfc2119
571
350
[ RFC5234 ] : https://tools.ietf.org/html/rfc5234
572
351
[ PHP_RESOURCE ] : https://php.net/manual/language.types.resource.php
@@ -576,5 +355,4 @@ The following keywords are recognized by this PSR:
576
355
[ PHP_OOP5LSB ] : https://php.net/manual/language.oop5.late-static-bindings.php
577
356
[ DEFACTO ] : http://www.phpdoc.org/docs/latest/index.html
578
357
[ PHPDOC.ORG ] : http://www.phpdoc.org/
579
- [FLUENT]: https://en.wikipedia.org/wiki/Fluent_interface
580
358
[ TAG_PSR ] : TBD
0 commit comments