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 | /** |
||
| 46 | * layoutLine constructor. |
||
| 47 | * @param float $startX |
||
| 48 | * @param float $startY |
||
| 49 | * @param object[] $elements |
||
| 50 | * @param int $margin |
||
| 51 | * @throws \Exception |
||
| 52 | */ |
||
| 53 | 1 | public function __construct($startX, $startY, $elements = [], $margin = 0) |
|
| 64 | |||
| 65 | 1 | protected function updateSize() |
|
| 78 | |||
| 79 | 1 | public function setPosition($x, $y) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param mixed $startX |
||
| 87 | * @return |
||
| 88 | */ |
||
| 89 | public function setX($startX) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param mixed $startY |
||
| 98 | * @return |
||
| 99 | */ |
||
| 100 | public function setY($startY) |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Render the XML element |
||
| 109 | * |
||
| 110 | * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered |
||
| 111 | * @return \DOMElement |
||
| 112 | */ |
||
| 113 | 1 | public function render(\DOMDocument $domDocument) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Get the Script Features |
||
| 135 | * |
||
| 136 | * @return ScriptFeature[] |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function getScriptFeatures() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @return mixed |
||
| 152 | */ |
||
| 153 | public function getX() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return mixed |
||
| 160 | */ |
||
| 161 | public function getY() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return float |
||
| 168 | */ |
||
| 169 | 1 | public function getHeight() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param float $height |
||
| 176 | */ |
||
| 177 | public function setHeight($height) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param float $width |
||
| 184 | * @return layoutRow |
||
| 185 | */ |
||
| 186 | public function setWidth($width) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return float|int |
||
| 195 | */ |
||
| 196 | 1 | public function getWidth() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @param Renderable $element |
||
| 203 | */ |
||
| 204 | 1 | public function addChild(Renderable $element) |
|
| 209 | |||
| 210 | public function getChildren() |
||
| 214 | |||
| 215 | |||
| 216 | 1 | public function addClass($class) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Add a new child |
||
| 223 | * |
||
| 224 | * @api |
||
| 225 | * @param Renderable $child Child Control to add |
||
| 226 | * @return static |
||
| 227 | * @deprecated Use addChild() |
||
| 228 | * @see Container::addChild() |
||
| 229 | */ |
||
| 230 | public function add(Renderable $child) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Add new children |
||
| 237 | * |
||
| 238 | * @api |
||
| 239 | * @param Renderable[] $children Child Controls to add |
||
| 240 | * @return static |
||
| 241 | */ |
||
| 242 | public function addChildren(array $children) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Remove all children |
||
| 249 | * |
||
| 250 | * @api |
||
| 251 | * @return static |
||
| 252 | */ |
||
| 253 | public function removeAllChildren() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Remove all children |
||
| 260 | * |
||
| 261 | * @api |
||
| 262 | * @return static |
||
| 263 | * @deprecated Use removeAllChildren() |
||
| 264 | * @see Container::removeAllChildren() |
||
| 265 | */ |
||
| 266 | public function removeChildren() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the Format |
||
| 273 | * |
||
| 274 | * @api |
||
| 275 | * @param bool $createIfEmpty If the format should be created if it doesn't exist yet |
||
| 276 | * @return Format |
||
| 277 | * @deprecated Use Style |
||
| 278 | * @see Style |
||
| 279 | */ |
||
| 280 | public function getFormat($createIfEmpty = true) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Set the Format |
||
| 287 | * |
||
| 288 | * @api |
||
| 289 | * @param Format $format New Format |
||
| 290 | * @return static |
||
| 291 | * @deprecated Use Style |
||
| 292 | * @see Style |
||
| 293 | */ |
||
| 294 | public function setFormat(Format $format = null) |
||
| 298 | |||
| 299 | 1 | private function setSize($sizeX, $sizeY) |
|
| 304 | } |
||
| 305 |
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.