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 = 1; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var float|int |
||
| 36 | */ |
||
| 37 | protected $startX; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var float|int |
||
| 41 | */ |
||
| 42 | protected $startY; |
||
| 43 | |||
| 44 | protected $hAlign = "left"; |
||
| 45 | protected $vAlign = "top"; |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * layoutLine constructor. |
||
| 50 | * @param float $startX |
||
| 51 | * @param float $startY |
||
| 52 | * @param object[] $elements |
||
| 53 | * @param int $margin |
||
| 54 | * @throws \Exception |
||
| 55 | */ |
||
| 56 | public function __construct($startX, $startY, $elements = [], $margin = 0) |
||
| 67 | |||
| 68 | protected function updateSize() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param double $x |
||
| 84 | * @param double $y |
||
| 85 | * @return LayoutRow |
||
| 86 | */ |
||
| 87 | public function setPosition($x, $y) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param mixed $startX |
||
| 97 | * @return LayoutRow |
||
| 98 | */ |
||
| 99 | public function setX($startX) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param mixed $startY |
||
| 108 | * @return LayoutRow |
||
| 109 | */ |
||
| 110 | public function setY($startY) |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Render the XML element |
||
| 120 | * |
||
| 121 | * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered |
||
| 122 | * @return \DOMElement |
||
| 123 | */ |
||
| 124 | public function render(\DOMDocument $domDocument) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the Script Features |
||
| 143 | * |
||
| 144 | * @return ScriptFeature[] |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function getScriptFeatures() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @return mixed |
||
| 160 | */ |
||
| 161 | public function getX() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | public function getY() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return float |
||
| 176 | */ |
||
| 177 | public function getHeight() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param float $height |
||
| 184 | */ |
||
| 185 | public function setHeight($height) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param float $width |
||
| 192 | * @return LayoutRow |
||
| 193 | */ |
||
| 194 | public function setWidth($width) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return float|int |
||
| 203 | */ |
||
| 204 | public function getWidth() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param Renderable $element |
||
| 211 | */ |
||
| 212 | public function addChild(Renderable $element) |
||
| 217 | |||
| 218 | public function getChildren() |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $class |
||
| 226 | * @return LayoutRow |
||
| 227 | */ |
||
| 228 | public function addClass($class) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Add a new child |
||
| 237 | * |
||
| 238 | * @api |
||
| 239 | * @param Renderable $child Child Control to add |
||
| 240 | * @deprecated Use addChild() |
||
| 241 | * @see Container::addChild() |
||
| 242 | */ |
||
| 243 | public function add(Renderable $child) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Add new children |
||
| 250 | * |
||
| 251 | * @api |
||
| 252 | * |
||
| 253 | * @param Renderable[] $children Child Controls to add |
||
| 254 | * |
||
| 255 | */ |
||
| 256 | public function addChildren(array $children) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Remove all children |
||
| 265 | * |
||
| 266 | * @api |
||
| 267 | * |
||
| 268 | */ |
||
| 269 | public function removeAllChildren() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Remove all children |
||
| 280 | * |
||
| 281 | * @api |
||
| 282 | * @deprecated Use removeAllChildren() |
||
| 283 | * @see Container::removeAllChildren() |
||
| 284 | */ |
||
| 285 | public function removeChildren() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get the Format |
||
| 292 | * |
||
| 293 | * @api |
||
| 294 | * @param bool $createIfEmpty If the format should be created if it doesn't exist yet |
||
| 295 | * @deprecated Use Style |
||
| 296 | * @see Style |
||
| 297 | */ |
||
| 298 | public function getFormat($createIfEmpty = true) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Set the Format |
||
| 305 | * |
||
| 306 | * @api |
||
| 307 | * @param Format $format New Format |
||
| 308 | * @deprecated Use Style |
||
| 309 | * @see Style |
||
| 310 | */ |
||
| 311 | public function setFormat(Format $format = null) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param float $sizeX |
||
| 318 | * @param float $sizeY |
||
| 319 | * @return LayoutRow |
||
| 320 | */ |
||
| 321 | private function setSize($sizeX, $sizeY) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param string $hAling |
||
| 332 | * @param string $vAlign |
||
| 333 | * @return LayoutRow |
||
| 334 | */ |
||
| 335 | public function setAlign($hAling = "left", $vAlign = "top") |
||
| 342 | |||
| 343 | public function getHorizontalAlign() |
||
| 347 | |||
| 348 | public function getVerticalAlign() |
||
| 352 | } |
||
| 353 |
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.