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 ChannelClose extends Frame |
|
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | private $replyCode; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $replyText; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | private $classId; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | private $methodId; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param int $channel |
||
| 40 | * @param int $replyCode |
||
| 41 | * @param string $replyText |
||
| 42 | * @param int $classId |
||
| 43 | * @param int $methodId |
||
| 44 | */ |
||
| 45 | public function __construct($channel, $replyCode, $replyText, $classId, $methodId) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * ReplyCode. |
||
| 57 | * |
||
| 58 | * @return int |
||
| 59 | */ |
||
| 60 | public function getReplyCode() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * ReplyText. |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function getReplyText() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Failing method class. |
||
| 77 | * |
||
| 78 | * @return int |
||
| 79 | */ |
||
| 80 | public function getClassId() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Failing method ID. |
||
| 87 | * |
||
| 88 | * @return int |
||
| 89 | */ |
||
| 90 | public function getMethodId() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function encode() |
||
| 108 | } |
||
| 109 |
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.