Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 98fd7ed

Browse files
authored
Merge pull request #4425 from Vinai/patch-23
Remove rules duplicating method signature info
2 parents a60a1ac + 4b7a418 commit 98fd7ed

File tree

1 file changed

+69
-53
lines changed

1 file changed

+69
-53
lines changed

guides/v2.2/coding-standards/docblock-standard-general.md

+69-53
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The following is assumed by default:
3333
The documentation should follow two simple principles:
3434

3535
1. Be as short as possible.
36-
2. Include all necessary information.
36+
2. Include all necessary information without duplication.
3737

3838
### Short documentation
3939

@@ -183,7 +183,7 @@ For example: namespace, class, interface, function, property, method, and so on.
183183

184184
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.
185185

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.
187187

188188
**DocBlock for a Class**
189189

@@ -257,7 +257,9 @@ require_once __DIR__ . '/../../functions.php';
257257
### Classes and interfaces
258258
{:#classes-interfaces}
259259

260-
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.
261263

262264
**Good:**
263265

@@ -288,7 +290,7 @@ use Magento\Stdlib\DateTime as StdlibDateTime;
288290
/**
289291
* @var Logger
290292
*/
291-
protected $_logger;
293+
private $logger;
292294

293295
/**
294296
* Description of method here.
@@ -297,7 +299,7 @@ protected $_logger;
297299
* @param StdlibDateTime $dateTime
298300
* @param int $number
299301
*/
300-
protected function doSomething(Random $mathRandom, StdlibDateTime $dateTime, $number)
302+
private function doSomething(Random $mathRandom, StdlibDateTime $dateTime, $number)
301303
{
302304

303305
}
@@ -326,32 +328,33 @@ class Profiler
326328
### Functions and methods
327329
{:#functions-methods}
328330

329-
Functions and methods must have:
331+
In general, typed method signature must be preferred over PHPDoc annotations whenever possible.
330332

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.
333338
For example:
334339

335340
* If a workaround or hack is implemented, explain why it is necessary and include any other details necessary to understand the algorithm.
336341
* 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.
337342
An implementation is non-obvious if another developer has questions about it.
338343

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).
343350
* 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.
345352

346353
**Exceptions to these rules:**
347354

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.
351356

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.
355358

356359
#### Things to include
357360

@@ -364,7 +367,7 @@ Functions and methods must have:
364367
For example: `@return Config|null`.
365368
The DockBlock needs to explain what situations return `null`.
366369

367-
Another example: `@param FileInterface | null`.
370+
Another example: `@param FileInterface|null`.
368371
The DocBlock needs to explain what happens when the value of the parameter is `null`.
369372

370373
Ideally, implementations such as these should be avoided.
@@ -425,7 +428,7 @@ In general, use the `@throws` tag when the code uses *throw*:
425428
*
426429
* @param string $elementId
427430
* @param string $attribute
428-
* @param mixed $value
431+
* @param int|string|float|bool|object|null $value
429432
* @return self
430433
* @throws \InvalidArgumentException
431434
*/
@@ -492,25 +495,51 @@ public function deleteDirectory($path)
492495
#### @return tag
493496
{:#return}
494497

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.
496521

497-
If the method returns itself, `return $this` should be used.
498522

499523
### Constants
500524
{:#constants}
501525

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.
503529

504530
For example, a global constant:
505531

506532

507533
```php
508534
/**
509-
* Directory separator shorthand
535+
* Directory separator shorthand, intended to make code more readable.
510536
*/
511537
define('DS', DIRECTORY_SEPARATOR);
538+
```
512539

513540
Or constants in a class:
541+
542+
```php
514543
class Profiler
515544
{
516545
/**
@@ -530,7 +559,7 @@ It's encouraged to replace existing DocBlock templates with regular DocBlock com
530559
## Structure of documentation space
531560
{:#documentation-space}
532561

533-
`@category`, `@package`, and `@subpackage` MUST NOT be used.
562+
`@author` ,`@category`, `@package`, and `@subpackage` MUST NOT be used.
534563
Documentation is organized with the use of namespaces.
535564

536565
## Other DocBlock tags
@@ -539,39 +568,29 @@ Documentation is organized with the use of namespaces.
539568
### @inheritdoc tag
540569
{:#inheritdoc}
541570

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.
550574

551575
**DocBlock for the Interface**
576+
552577
```php
553578
/**
554579
* Interface for mutable value object for integer values
555580
*/
556581
interface MutableInterface
557582
{
558583
/**
559-
* Get value
560-
*
561584
* Returns 0, if no value is available
562-
*
563-
* @return int
564585
*/
565-
public function getVal();
586+
public function getVal(): int;
566587

567588
/**
568-
* Set value
569-
*
570589
* Sets 0 in case a non-integer value is passed
571-
*
572-
* @param int $value
590+
*
591+
* @param int|string|bool|float|null $value
573592
*/
574-
public function setVal($value);
593+
public function setVal($value): void;
575594
}
576595
```
577596

@@ -582,19 +601,16 @@ interface MutableInterface
582601
*/
583602
class LimitedMutableClass implements MutableInterface
584603
{
585-
/**
586-
* @inheritdoc
587-
*/
588-
public function getVal()
604+
public function getVal(): int
589605
{
590606
}
591-
607+
592608
/**
593-
* Set value
594-
*
595-
* Sets 0 in case the value is bigger than max allowed value. {@inheritdoc}
609+
* Sets 0 in case a non-integer value is passed
610+
*
611+
* @param int|string|bool|float|null $value
596612
*/
597-
public function setVal($value)
613+
public function setVal($value): void
598614
{
599615
}
600616
}

0 commit comments

Comments
 (0)