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 |
||
| 5 | class CRUDMessage { |
||
| 6 | private $message; |
||
| 7 | private $type; |
||
| 8 | private $icon; |
||
| 9 | private $title; |
||
| 10 | private $timeout; |
||
| 11 | private $_message; |
||
| 12 | |||
| 13 | public function __construct($message,$title="",$type="",$icon="",$timeout=null){ |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @return string |
||
| 24 | */ |
||
| 25 | public function getMessage() { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | public function getType() { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return mixed |
||
| 38 | */ |
||
| 39 | public function getIcon() { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function getTitle() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param string $message |
||
| 52 | */ |
||
| 53 | public function setMessage($message) { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $type |
||
| 60 | * @return $this |
||
| 61 | */ |
||
| 62 | public function setType($type) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $icon |
||
| 69 | * @return $this |
||
| 70 | */ |
||
| 71 | public function setIcon($icon) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $title |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | public function setTitle($title) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return integer |
||
| 87 | */ |
||
| 88 | public function getTimeout() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param integer $timeout |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | public function setTimeout($timeout) { |
||
| 100 | /** |
||
| 101 | * |
||
| 102 | * @param string $value |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function parse($value){ |
||
| 109 | |||
| 110 | View Code Duplication | public function parseContent($keyValues){ |
|
| 118 | |||
| 119 | } |
||
| 120 | |||
| 121 |
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.