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 |
||
| 13 | class LayoutRow implements Renderable, ScriptFeatureable, Container |
||
| 14 | { |
||
| 15 | |||
| 16 | protected $frameClasses = []; |
||
| 17 | /** |
||
| 18 | * @var float|int |
||
| 19 | */ |
||
| 20 | protected $height = 0; |
||
| 21 | /** |
||
| 22 | * @var float|int |
||
| 23 | */ |
||
| 24 | protected $width = 0; |
||
| 25 | |||
| 26 | /** @var Control[] */ |
||
| 27 | protected $elements = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var float|int |
||
| 31 | */ |
||
| 32 | protected $margin = 0; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var float|int |
||
| 36 | */ |
||
| 37 | protected $posX; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var float|int |
||
| 41 | */ |
||
| 42 | protected $posy; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var float|int |
||
| 46 | */ |
||
| 47 | protected $startY = 0; |
||
| 48 | |||
| 49 | /** @var float */ |
||
| 50 | protected $additionalHeight; |
||
| 51 | |||
| 52 | protected $hAlign = "left"; |
||
| 53 | protected $vAlign = "top"; |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * layoutLine constructor. |
||
| 58 | * |
||
| 59 | * @param float $posX |
||
| 60 | * @param float $posY |
||
| 61 | * @param object[] $elements |
||
| 62 | * @param int $margin |
||
| 63 | * |
||
| 64 | * @throws \Exception |
||
| 65 | */ |
||
| 66 | public function __construct($posX, $posY, $elements = [], $margin = 1, $additionalHeight = 0) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Update the size of the layout according to all the elements in it. |
||
| 81 | */ |
||
| 82 | protected function updateSize() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Set position. |
||
| 99 | * |
||
| 100 | * @param double $x |
||
| 101 | * @param double $y |
||
| 102 | * @return LayoutRow |
||
| 103 | */ |
||
| 104 | public function setPosition($x, $y) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param mixed $posX |
||
| 114 | * |
||
| 115 | * @return LayoutRow |
||
| 116 | */ |
||
| 117 | public function setX($posX) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param mixed $posY |
||
| 126 | * |
||
| 127 | * @return LayoutRow |
||
| 128 | */ |
||
| 129 | public function setY($posY) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param float|int $startY |
||
| 138 | */ |
||
| 139 | public function setStartY($startY) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Render the XML element |
||
| 146 | * |
||
| 147 | * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered |
||
| 148 | * @return \DOMElement |
||
| 149 | */ |
||
| 150 | public function render(\DOMDocument $domDocument) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get the Script Features |
||
| 171 | * |
||
| 172 | * @return ScriptFeature[] |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function getScriptFeatures() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @return mixed |
||
| 188 | */ |
||
| 189 | public function getX() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return mixed |
||
| 196 | */ |
||
| 197 | public function getY() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return float |
||
| 204 | */ |
||
| 205 | public function getHeight() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param float $height |
||
| 212 | */ |
||
| 213 | public function setHeight($height) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param float $width |
||
| 220 | * @return LayoutRow |
||
| 221 | */ |
||
| 222 | public function setWidth($width) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return float|int |
||
| 231 | */ |
||
| 232 | public function getWidth() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param Renderable $element |
||
| 239 | */ |
||
| 240 | public function addChild(Renderable $element) |
||
| 245 | |||
| 246 | public function getChildren() |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $class |
||
| 254 | * @return LayoutRow |
||
| 255 | */ |
||
| 256 | public function addClass($class) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Add a new child |
||
| 265 | * |
||
| 266 | * @api |
||
| 267 | * @param Renderable $child Child Control to add |
||
| 268 | * @deprecated Use addChild() |
||
| 269 | * @see Container::addChild() |
||
| 270 | */ |
||
| 271 | public function add(Renderable $child) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Add new children |
||
| 278 | * |
||
| 279 | * @api |
||
| 280 | * |
||
| 281 | * @param Renderable[] $children Child Controls to add |
||
| 282 | * |
||
| 283 | */ |
||
| 284 | public function addChildren(array $children) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Remove all children |
||
| 293 | * |
||
| 294 | * @api |
||
| 295 | * |
||
| 296 | */ |
||
| 297 | public function removeAllChildren() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Remove all children |
||
| 308 | * |
||
| 309 | * @api |
||
| 310 | * @deprecated Use removeAllChildren() |
||
| 311 | * @see Container::removeAllChildren() |
||
| 312 | */ |
||
| 313 | public function removeChildren() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get the Format |
||
| 320 | * |
||
| 321 | * @api |
||
| 322 | * @param bool $createIfEmpty If the format should be created if it doesn't exist yet |
||
| 323 | * @deprecated Use Style |
||
| 324 | * @see Style |
||
| 325 | */ |
||
| 326 | public function getFormat($createIfEmpty = true) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Set the Format |
||
| 333 | * |
||
| 334 | * @api |
||
| 335 | * @param Format $format New Format |
||
| 336 | * @deprecated Use Style |
||
| 337 | * @see Style |
||
| 338 | */ |
||
| 339 | public function setFormat(Format $format = null) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param float $sizeX |
||
| 346 | * @param float $sizeY |
||
| 347 | * @return LayoutRow |
||
| 348 | */ |
||
| 349 | private function setSize($sizeX, $sizeY) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param string $hAling |
||
| 360 | * @param string $vAlign |
||
| 361 | * @return LayoutRow |
||
| 362 | */ |
||
| 363 | public function setAlign($hAling = "left", $vAlign = "top") |
||
| 370 | |||
| 371 | public function getHorizontalAlign() |
||
| 375 | |||
| 376 | public function getVerticalAlign() |
||
| 380 | } |
||
| 381 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.