You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: guides/v2.2/coding-standards/docblock-standard-general.md
+69-53
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ The following is assumed by default:
33
33
The documentation should follow two simple principles:
34
34
35
35
1. Be as short as possible.
36
-
2. Include all necessary information.
36
+
2. Include all necessary information without duplication.
37
37
38
38
### Short documentation
39
39
@@ -183,7 +183,7 @@ For example: namespace, class, interface, function, property, method, and so on.
183
183
184
184
If the source code file has one and only one standalone structural element (class, interface, function, and so on), as it may be required by language-specific coding standard, the file DocBlock is to be reused for this element.
185
185
186
-
So in general case, classes that are declared in dedicated files, must have one DocBlock, which refers to the class and file at the same time.
186
+
So classes that are declared in dedicated files, must have either no DocBlock or exactly one DocBlock, which refers to the class and file at the same time.
Classes and interfaces must have a short description that is a human-understandable intention of the class.
260
+
Classes and interfaces should have a short description that is a human-understandable intention of the class.
261
+
If the short description adds no additional information beyond what the type name already supplies, the
262
+
short description must be omitted.
261
263
262
264
**Good:**
263
265
@@ -288,7 +290,7 @@ use Magento\Stdlib\DateTime as StdlibDateTime;
288
290
/**
289
291
* @var Logger
290
292
*/
291
-
protected $_logger;
293
+
private $logger;
292
294
293
295
/**
294
296
* Description of method here.
@@ -297,7 +299,7 @@ protected $_logger;
297
299
* @param StdlibDateTime $dateTime
298
300
* @param int $number
299
301
*/
300
-
protected function doSomething(Random $mathRandom, StdlibDateTime $dateTime, $number)
302
+
private function doSomething(Random $mathRandom, StdlibDateTime $dateTime, $number)
301
303
{
302
304
303
305
}
@@ -326,32 +328,33 @@ class Profiler
326
328
### Functions and methods
327
329
{:#functions-methods}
328
330
329
-
Functions and methods must have:
331
+
In general, typed method signature must be preferred over PHPDoc annotations whenever possible.
330
332
331
-
* Short description
332
-
* Long description that explains the motivation behind the implementation.
333
+
Functions and methods should have:
334
+
335
+
* A short description in case it adds meaningful information beyond the method name.
336
+
* If the purpose of the method is not obvious, a long description that explains the motivation behind the implementation.
337
+
The comment must describe why method is implemented and not how.
333
338
For example:
334
339
335
340
* If a workaround or hack is implemented, explain why it is necessary and include any other details necessary to understand the algorithm.
336
341
* For non-obvious implementations where the implementation logic is complicated or does not correspond to the Technical Vision or other known best practices, include an explanation in the doc block's description.
337
342
An implementation is non-obvious if another developer has questions about it.
338
343
339
-
* Declaration of all arguments (if any) using `@param` tag.
340
-
Appropriate argument type must be specified.
341
-
* Declaration of return type using `@return` tag.
342
-
If there is no such operator, the `@return` tag must have `void` as the return value.
344
+
* The declaration of all arguments (if any) using `@param` tag, unless the argument type is indicated in the method signature.
345
+
All `@param` annotations must include the appropriate argument type.
346
+
If any argument requires a `@param` annotation, all arguments must be listed (all or none).
347
+
The `@param` annotations must be in the same order as the method arguments.
348
+
* The declaration of the return type using the `@return` tag must only be added if the method return type signature
349
+
does not supply all necessary information (see below for more information on return types).
343
350
* Declaration of possibly thrown exception using `@throws` tag, if the actual body of function triggers throwing an exception.
344
-
All occurrences of `@throws` in a DocBlock must be after `@param` and `@return`(if any).
351
+
All occurrences of `@throws` in a DocBlock must be after `@param` and `@return`annotations.
345
352
346
353
**Exceptions to these rules:**
347
354
348
-
* Constructors may not have short and/or long description
349
-
350
-
* Testing methods in Unit tests may not have doc blocks if the test's method name follows the convention (test<MethodName>)
355
+
* Testing methods in Unit tests may have doc blocks to describe the purpose of the test, for example referencing github issues.
351
356
352
-
* If the test does not follow the convention, it should have a doc block describing the covered methods
353
-
354
-
* Non-testing methods should have a doc block with description. It includes data providers and any helper methods
357
+
* Test method annotations may include data providers and other testing annotations.
355
358
356
359
#### Things to include
357
360
@@ -364,7 +367,7 @@ Functions and methods must have:
364
367
For example: `@return Config|null`.
365
368
The DockBlock needs to explain what situations return `null`.
366
369
367
-
Another example: `@param FileInterface | null`.
370
+
Another example: `@param FileInterface|null`.
368
371
The DocBlock needs to explain what happens when the value of the parameter is `null`.
369
372
370
373
Ideally, implementations such as these should be avoided.
@@ -425,7 +428,7 @@ In general, use the `@throws` tag when the code uses *throw*:
425
428
*
426
429
* @param string $elementId
427
430
* @param string $attribute
428
-
* @param mixed $value
431
+
* @param int|string|float|bool|object|null $value
429
432
* @return self
430
433
* @throws \InvalidArgumentException
431
434
*/
@@ -492,25 +495,51 @@ public function deleteDirectory($path)
492
495
#### @return tag
493
496
{:#return}
494
497
495
-
If there is no explicit return statement in a method or function, a `@return void` should be used in the documentation.
498
+
In general method return type signatures should be prefered over `@return` type annotations.
499
+
If that is not possible due to ambiguous return types or because of backward compatibility constraints, the `@return` type annotation must be used.
500
+
501
+
If there is no explicit return statement in a method or function or a return statement without a value, a `void` return type must be declared in the method signature. For example:
502
+
503
+
```php
504
+
function setName(string $name): void
505
+
{
506
+
$this->name = $name;
507
+
}
508
+
```
509
+
510
+
If the method returns itself, the method signature return type must be `self`. Here is an example:
511
+
512
+
```php
513
+
function withField(string $fieldName): self
514
+
{
515
+
$this->fields[] = $fieldName;
516
+
return $this;
517
+
}
518
+
```
519
+
520
+
If for backward compatibility reasons no return type can be added to the method signature, a `@return $this` annotation must be used.
496
521
497
-
If the method returns itself, `return $this` should be used.
498
522
499
523
### Constants
500
524
{:#constants}
501
525
502
-
Constants must have short description.
526
+
Constants may have a short description.
527
+
If the short description adds no additional information beyond what the constant name already supplies, the
528
+
short description must be omitted.
503
529
504
530
For example, a global constant:
505
531
506
532
507
533
```php
508
534
/**
509
-
* Directory separator shorthand
535
+
* Directory separator shorthand, intended to make code more readable.
510
536
*/
511
537
define('DS', DIRECTORY_SEPARATOR);
538
+
```
512
539
513
540
Or constants in a class:
541
+
542
+
```php
514
543
class Profiler
515
544
{
516
545
/**
@@ -530,7 +559,7 @@ It's encouraged to replace existing DocBlock templates with regular DocBlock com
530
559
## Structure of documentation space
531
560
{:#documentation-space}
532
561
533
-
`@category`, `@package`, and `@subpackage` MUST NOT be used.
562
+
`@author` ,`@category`, `@package`, and `@subpackage` MUST NOT be used.
534
563
Documentation is organized with the use of namespaces.
535
564
536
565
## Other DocBlock tags
@@ -539,39 +568,29 @@ Documentation is organized with the use of namespaces.
539
568
### @inheritdoc tag
540
569
{:#inheritdoc}
541
570
542
-
Whenever possible the `@inheritdoc` tag MUST be used for children to avoid duplication of doc blocks.
543
-
544
-
Even Though PHPDocumentor understands inheritance and uses the parent doc block by default (without `@inheritdoc` tag specified), including the tag helps ensure that the doc block is not missed at all.
545
-
546
-
Rules for usage of the tag:
547
-
548
-
* Use `@inheritdoc` (notice no braces around) to indicate that the entire doc block should be inherited from the parent method.
549
-
* Use the inline `{@inheritdoc}` tag (with braces around) in long descriptions to reuse the parent's long description. The tagged method MUST have its own short description.
571
+
The `@inheritdoc` tag SHOULD NOT be used.
572
+
If a child class method requires a long description to explain its purpose, it may use `@inheritdoc` to indicate the new description is intended as an addition to the parent method description.
573
+
In general such method overrides are a code smell and should be used as an incentive to make the code more self-documenting if possible.
550
574
551
575
**DocBlock for the Interface**
576
+
552
577
```php
553
578
/**
554
579
* Interface for mutable value object for integer values
555
580
*/
556
581
interface MutableInterface
557
582
{
558
583
/**
559
-
* Get value
560
-
*
561
584
* Returns 0, if no value is available
562
-
*
563
-
* @return int
564
585
*/
565
-
public function getVal();
586
+
public function getVal(): int;
566
587
567
588
/**
568
-
* Set value
569
-
*
570
589
* Sets 0 in case a non-integer value is passed
571
-
*
572
-
* @param int $value
590
+
*
591
+
* @param int|string|bool|float|null $value
573
592
*/
574
-
public function setVal($value);
593
+
public function setVal($value): void;
575
594
}
576
595
```
577
596
@@ -582,19 +601,16 @@ interface MutableInterface
582
601
*/
583
602
class LimitedMutableClass implements MutableInterface
584
603
{
585
-
/**
586
-
* @inheritdoc
587
-
*/
588
-
public function getVal()
604
+
public function getVal(): int
589
605
{
590
606
}
591
-
607
+
592
608
/**
593
-
* Set value
594
-
*
595
-
* Sets 0 in case the value is bigger than max allowed value. {@inheritdoc}
0 commit comments