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 |
||
| 16 | View Code Duplication | class ConnectionStart extends Frame |
|
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | private $versionMajor; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var int |
||
| 25 | */ |
||
| 26 | private $versionMinor; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $serverProperties = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $mechanisms; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $locales; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param int $channel |
||
| 45 | * @param int $versionMajor |
||
| 46 | * @param int $versionMinor |
||
| 47 | * @param array $serverProperties |
||
| 48 | * @param string $mechanisms |
||
| 49 | * @param string $locales |
||
| 50 | */ |
||
| 51 | public function __construct($channel, $versionMajor, $versionMinor, $serverProperties, $mechanisms, $locales) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Protocol major version. |
||
| 64 | * |
||
| 65 | * @return int |
||
| 66 | */ |
||
| 67 | public function getVersionMajor() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Protocol minor version. |
||
| 74 | * |
||
| 75 | * @return int |
||
| 76 | */ |
||
| 77 | public function getVersionMinor() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Server properties. |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public function getServerProperties() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Available security mechanisms. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getMechanisms() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Available message locales. |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getLocales() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function encode() |
||
| 126 | } |
||
| 127 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.