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 |
||
| 31 | View Code Duplication | class Checkbox extends Common |
|
|
|
|||
| 32 | { |
||
| 33 | /** |
||
| 34 | * the name of element |
||
| 35 | * |
||
| 36 | * @access private |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $_sType = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * the label of element |
||
| 43 | * |
||
| 44 | * @access private |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $_sLabel = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * the value of element |
||
| 51 | * |
||
| 52 | * @access private |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $_sValue = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * the value of the checked element |
||
| 59 | * |
||
| 60 | * @access private |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private $_aValuesChecked = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * constructor that it increment (static) for all use |
||
| 67 | * |
||
| 68 | * @access public |
||
| 69 | * @param string $sName name |
||
| 70 | * @param string $sLabel label of checkbox |
||
| 71 | * @param string $sValue value of checkbox |
||
| 72 | * @param array $aValuesChecked value checked of checkbox |
||
| 73 | */ |
||
| 74 | public function __construct(string $sName, string $sLabel, string $sValue, array $aValuesChecked = null) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * get the Value |
||
| 84 | * |
||
| 85 | * @access public |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function getValue() : string |
||
| 92 | |||
| 93 | /** |
||
| 94 | * set the Value |
||
| 95 | * |
||
| 96 | * @access public |
||
| 97 | * @param string $sValue Value of input; |
||
| 98 | * @return Checkbox |
||
| 99 | */ |
||
| 100 | public function setValue(string $sValue) : Checkbox |
||
| 105 | |||
| 106 | /** |
||
| 107 | * get the Value Checked |
||
| 108 | * |
||
| 109 | * @access public |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | public function getValuesChecked() : array |
||
| 116 | |||
| 117 | /** |
||
| 118 | * set the Value Checked |
||
| 119 | * |
||
| 120 | * @access public |
||
| 121 | * @param array $aValuesChecked Values of input; |
||
| 122 | * @return Checkbox |
||
| 123 | */ |
||
| 124 | public function setValuesChecked(array $aValuesChecked) : Checkbox |
||
| 129 | |||
| 130 | /** |
||
| 131 | * get the Label |
||
| 132 | * |
||
| 133 | * @access public |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function getLabel() : string |
||
| 140 | |||
| 141 | /** |
||
| 142 | * set the Label |
||
| 143 | * |
||
| 144 | * @access public |
||
| 145 | * @param string $sLabel Label of input; |
||
| 146 | * @return Checkbox |
||
| 147 | */ |
||
| 148 | public function setLabel(string $sLabel) : Checkbox |
||
| 153 | |||
| 154 | /** |
||
| 155 | * if the button is clicked |
||
| 156 | * |
||
| 157 | * @access public |
||
| 158 | * @param string $sType type of input; |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | public function isClicked(string $sType) : bool |
||
| 170 | |||
| 171 | /** |
||
| 172 | * get the <html> |
||
| 173 | * |
||
| 174 | * @access public |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function fetch() : string |
||
| 187 | } |
||
| 188 |
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.