Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 28 | class ResourceStack |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private $stack; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param array $stack |
||
| 37 | */ |
||
| 38 | 28 | public function __construct($stack) |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Get the current version resource. |
||
| 47 | * |
||
| 48 | * @return PuliResource |
||
| 49 | */ |
||
| 50 | 10 | public function getCurrent() |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Get the current version number. |
||
| 57 | * |
||
| 58 | * @return integer |
||
|
|
|||
| 59 | */ |
||
| 60 | 12 | View Code Duplication | public function getCurrentVersion() |
| 70 | |||
| 71 | /** |
||
| 72 | * Get the current version resource. |
||
| 73 | * |
||
| 74 | * @return PuliResource |
||
| 75 | */ |
||
| 76 | 10 | public function getFirst() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get the first version number. |
||
| 83 | * |
||
| 84 | * @return integer |
||
| 85 | */ |
||
| 86 | 12 | View Code Duplication | public function getFirstVersion() |
| 96 | |||
| 97 | /** |
||
| 98 | * Get a specific version resource from the stack by its version number. |
||
| 99 | * |
||
| 100 | * @param int $version The version number (first is 0). |
||
| 101 | * |
||
| 102 | * @return PuliResource |
||
| 103 | */ |
||
| 104 | 15 | public function get($version) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Get an array of the available versions of this resource. |
||
| 122 | * |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | 23 | public function getVersions() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the stack contents as array. |
||
| 132 | * |
||
| 133 | * @return PuliResource[] The resources in the stack. |
||
| 134 | */ |
||
| 135 | public function toArray() |
||
| 139 | } |
||
| 140 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.