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 |
||
| 18 | class MySqlUnixSocketDsn implements IDsn { |
||
| 19 | /** @var string */ |
||
| 20 | private $unixSocket; |
||
| 21 | |||
| 22 | /** @var string */ |
||
| 23 | private $dbName; |
||
| 24 | |||
| 25 | 3 | public function __construct(string $unixSocket, string $dbName) { |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $unixSocket |
||
| 32 | * @return MySqlUnixSocketDsn |
||
|
|
|||
| 33 | * @throws EmptyArgumentException |
||
| 34 | */ |
||
| 35 | 3 | public function setUnixSocket(string $unixSocket) : self { |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | 1 | public function getUnixSocket() : string { |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $dbName |
||
| 54 | * @return MySqlUnixSocketDsn |
||
| 55 | * @throws EmptyArgumentException |
||
| 56 | */ |
||
| 57 | 3 | View Code Duplication | public function setDbName(string $dbName) : self { |
| 66 | |||
| 67 | /** |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | 1 | public function getDbName() : string { |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | 1 | public function toString() : string { |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | 1 | public function __toString() : string { |
|
| 87 | } |
||
| 88 |
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.