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 implements IDsn { |
||
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 | 8 | View Code Duplication | public function setDbName(string $dbName) : self { |
39 | |||
40 | /** |
||
41 | * @return string |
||
42 | */ |
||
43 | 2 | public function getDbName() : string { |
|
46 | |||
47 | /** |
||
48 | * @param string $host |
||
49 | * @return BaseDns|static |
||
50 | * @throws EmptyArgumentException |
||
51 | */ |
||
52 | 8 | View Code Duplication | public function setHost(string $host) : self { |
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | 2 | public function getHost() : string { |
|
68 | |||
69 | /** |
||
70 | * @param int $port |
||
71 | * @return BaseDns|static |
||
72 | * @throws WrongArgumentException |
||
73 | */ |
||
74 | 8 | public function setPort(int $port) : self { |
|
83 | |||
84 | /** |
||
85 | * @return int |
||
86 | */ |
||
87 | 2 | public function getPort() : int { |
|
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | 2 | public function __toString() : string { |
|
97 | } |
||
98 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.