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 |
||
| 34 | class Container |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * complete view |
||
| 38 | * |
||
| 39 | * @access private |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $_sView = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * form library with its entity |
||
| 46 | * |
||
| 47 | * @access private |
||
| 48 | * @var Form |
||
| 49 | */ |
||
| 50 | private $_oForm = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Block the save in the entity if you don't call handleRequest |
||
| 54 | * |
||
| 55 | * @access private |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | private $_bHandleRequestActivate = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Request of the formular |
||
| 62 | * |
||
| 63 | * @access private |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private $_aRequest = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * get the Value |
||
| 70 | * |
||
| 71 | * @access public |
||
| 72 | * @return \stdClass |
||
| 73 | */ |
||
| 74 | public function createView() : \stdClass |
||
| 96 | |||
| 97 | /** |
||
| 98 | * set the Value |
||
| 99 | * |
||
| 100 | * @access public |
||
| 101 | * @param string $sView Display of form; |
||
| 102 | * @return \Venus\lib\Form\Container |
||
| 103 | */ |
||
| 104 | public function setView(string $sView) : Container |
||
| 109 | |||
| 110 | /** |
||
| 111 | * handle the request to do many actions on it |
||
| 112 | * |
||
| 113 | * @access public |
||
| 114 | * @param array $aRequest request like $_POST |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | public function handleRequest(array $aRequest) : bool |
||
| 166 | |||
| 167 | /** |
||
| 168 | * set Form lib with its entity |
||
| 169 | * |
||
| 170 | * @access public |
||
| 171 | * @param Form $oForm request like $_POST |
||
| 172 | * @return \Venus\lib\Form\Container |
||
| 173 | */ |
||
| 174 | public function setForm(Form $oForm) : Container |
||
| 179 | |||
| 180 | /** |
||
| 181 | * if this form is validate and save |
||
| 182 | * |
||
| 183 | * @access public |
||
| 184 | * @return boolean |
||
| 185 | */ |
||
| 186 | public function isValid() : bool |
||
| 190 | |||
| 191 | /** |
||
| 192 | * if this form is validate and save |
||
| 193 | * |
||
| 194 | * @access public |
||
| 195 | * @return boolean |
||
| 196 | */ |
||
| 197 | public function isSubmitted() : bool |
||
| 207 | |||
| 208 | /** |
||
| 209 | * if this form is validate and save |
||
| 210 | * |
||
| 211 | * @access public |
||
| 212 | * @param string $sElementName element name what we want test the click |
||
| 213 | * @return boolean |
||
| 214 | */ |
||
| 215 | public function isClicked(string $sElementName) : bool |
||
| 219 | |||
| 220 | /** |
||
| 221 | * if the element is valide or not (with the constraint) |
||
| 222 | * |
||
| 223 | * @access private |
||
| 224 | * @param object $oElement element of formular |
||
| 225 | * @return boolean |
||
| 226 | */ |
||
| 227 | private function _validate($oElement) : bool |
||
| 239 | } |
||
| 240 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.