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 |
||
| 14 | class layoutRow implements Renderable, ScriptFeatureable, Container |
||
| 15 | { |
||
| 16 | |||
| 17 | protected $frameClasses = []; |
||
| 18 | /** |
||
| 19 | * @var float|int |
||
| 20 | */ |
||
| 21 | protected $height = 0; |
||
| 22 | /** |
||
| 23 | * @var float|int |
||
| 24 | */ |
||
| 25 | protected $width = 0; |
||
| 26 | |||
| 27 | /** @var Control[] */ |
||
| 28 | protected $elements = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var float|int |
||
| 32 | */ |
||
| 33 | protected $margin = 1; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var float|int |
||
| 37 | */ |
||
| 38 | protected $startX; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var float|int |
||
| 42 | */ |
||
| 43 | protected $startY; |
||
| 44 | |||
| 45 | protected $hAlign = "left"; |
||
| 46 | protected $vAlign = "top"; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * layoutLine constructor. |
||
| 51 | * @param float $startX |
||
| 52 | * @param float $startY |
||
| 53 | * @param object[] $elements |
||
| 54 | * @param int $margin |
||
| 55 | * @throws \Exception |
||
| 56 | */ |
||
| 57 | 1 | public function __construct($startX, $startY, $elements = [], $margin = 0) |
|
| 68 | |||
| 69 | 1 | protected function updateSize() |
|
| 82 | |||
| 83 | 1 | public function setPosition($x, $y) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @param mixed $startX |
||
| 91 | * @return |
||
| 92 | */ |
||
| 93 | public function setX($startX) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param mixed $startY |
||
| 102 | * @return |
||
| 103 | */ |
||
| 104 | public function setY($startY) |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Render the XML element |
||
| 114 | * |
||
| 115 | * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered |
||
| 116 | * @return \DOMElement |
||
| 117 | */ |
||
| 118 | 1 | public function render(\DOMDocument $domDocument) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Get the Script Features |
||
| 139 | * |
||
| 140 | * @return ScriptFeature[] |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function getScriptFeatures() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @return mixed |
||
| 156 | */ |
||
| 157 | public function getX() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return mixed |
||
| 164 | */ |
||
| 165 | public function getY() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return float |
||
| 172 | */ |
||
| 173 | public function getHeight() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param float $height |
||
| 180 | */ |
||
| 181 | public function setHeight($height) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param float $width |
||
| 188 | * @return layoutRow |
||
| 189 | */ |
||
| 190 | public function setWidth($width) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return float|int |
||
| 199 | */ |
||
| 200 | public function getWidth() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param Renderable $element |
||
| 207 | */ |
||
| 208 | 1 | public function addChild(Renderable $element) |
|
| 213 | |||
| 214 | public function getChildren() |
||
| 218 | |||
| 219 | |||
| 220 | 1 | public function addClass($class) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Add a new child |
||
| 227 | * |
||
| 228 | * @api |
||
| 229 | * @param Renderable $child Child Control to add |
||
| 230 | * @return static |
||
| 231 | * @deprecated Use addChild() |
||
| 232 | * @see Container::addChild() |
||
| 233 | */ |
||
| 234 | public function add(Renderable $child) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Add new children |
||
| 241 | * |
||
| 242 | * @api |
||
| 243 | * @param Renderable[] $children Child Controls to add |
||
| 244 | * @return static |
||
| 245 | */ |
||
| 246 | public function addChildren(array $children) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Remove all children |
||
| 253 | * |
||
| 254 | * @api |
||
| 255 | * @return static |
||
| 256 | */ |
||
| 257 | public function removeAllChildren() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Remove all children |
||
| 264 | * |
||
| 265 | * @api |
||
| 266 | * @return static |
||
| 267 | * @deprecated Use removeAllChildren() |
||
| 268 | * @see Container::removeAllChildren() |
||
| 269 | */ |
||
| 270 | public function removeChildren() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get the Format |
||
| 277 | * |
||
| 278 | * @api |
||
| 279 | * @param bool $createIfEmpty If the format should be created if it doesn't exist yet |
||
| 280 | * @return Format |
||
| 281 | * @deprecated Use Style |
||
| 282 | * @see Style |
||
| 283 | */ |
||
| 284 | public function getFormat($createIfEmpty = true) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Set the Format |
||
| 291 | * |
||
| 292 | * @api |
||
| 293 | * @param Format $format New Format |
||
| 294 | * @return static |
||
| 295 | * @deprecated Use Style |
||
| 296 | * @see Style |
||
| 297 | */ |
||
| 298 | public function setFormat(Format $format = null) |
||
| 302 | |||
| 303 | 1 | private function setSize($sizeX, $sizeY) |
|
| 308 | |||
| 309 | public function setAlign($hAling = "left", $vAlign = "top") |
||
| 314 | |||
| 315 | |||
| 316 | } |
||
| 317 |
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.