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 |
||
| 15 | abstract class BaseDns { |
||
| 16 | /** @var string */ |
||
| 17 | protected $dbName; |
||
| 18 | |||
| 19 | /** @var string */ |
||
| 20 | protected $host; |
||
| 21 | |||
| 22 | /** @var int */ |
||
| 23 | protected $port; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param string $dbName |
||
| 27 | * @return BaseDns|static |
||
|
|
|||
| 28 | * @throws EmptyArgumentException |
||
| 29 | */ |
||
| 30 | View Code Duplication | public function setDbName(string $dbName) : self { |
|
| 39 | |||
| 40 | /** |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | public function getDbName() : string { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $host |
||
| 49 | * @return BaseDns|static |
||
| 50 | * @throws EmptyArgumentException |
||
| 51 | */ |
||
| 52 | View Code Duplication | public function setHost(string $host) : self { |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public function getHost() : string { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param int $port |
||
| 71 | * @return BaseDns|static |
||
| 72 | * @throws WrongArgumentException |
||
| 73 | */ |
||
| 74 | public function setPort(int $port) : self { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return int |
||
| 86 | */ |
||
| 87 | public function getPort() : int { |
||
| 90 | } |
||
| 91 |
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.