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 layoutLine implements Renderable, ScriptFeatureable, Container |
||
| 14 | { |
||
| 15 | protected $frameClasses = []; |
||
| 16 | |||
| 17 | protected $frameId = null; |
||
| 18 | |||
| 19 | /** @var float */ |
||
| 20 | protected $width = 0.; |
||
| 21 | |||
| 22 | /** @var float */ |
||
| 23 | protected $height = 0.; |
||
| 24 | |||
| 25 | /** @var Control[] */ |
||
| 26 | protected $elements = []; |
||
| 27 | |||
| 28 | /** @var float */ |
||
| 29 | private $margin = 2.; |
||
| 30 | /** |
||
| 31 | * @var float |
||
| 32 | */ |
||
| 33 | protected $startX = 0.; |
||
| 34 | /** |
||
| 35 | * @var float |
||
| 36 | */ |
||
| 37 | protected $startY = 0.; |
||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | protected $hAlign = "left"; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | protected $vAlign = "top"; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * layoutLine constructor. |
||
| 47 | * @param float $startX |
||
| 48 | * @param float $startY |
||
| 49 | * @param object[] $elements |
||
| 50 | * @param float $margin |
||
| 51 | * @throws \Exception |
||
| 52 | */ |
||
| 53 | public function __construct($startX, $startY, $elements = [], $margin = 0.) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Render the XML element |
||
| 74 | * |
||
| 75 | * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered |
||
| 76 | * @return \DOMElement |
||
| 77 | */ |
||
| 78 | public function render(\DOMDocument $domDocument) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param Control $element |
||
| 105 | * @return float|int |
||
| 106 | */ |
||
| 107 | private function getRelativeStartPosition($element) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the Script Features |
||
| 118 | * |
||
| 119 | * @return ScriptFeature[] |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function getScriptFeatures() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param mixed $startX |
||
| 135 | * @return layoutLine |
||
| 136 | */ |
||
| 137 | public function setX($startX) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param mixed $startY |
||
| 146 | * @return layoutLine |
||
| 147 | */ |
||
| 148 | public function setY($startY) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return float |
||
| 157 | */ |
||
| 158 | public function getX() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return mixed |
||
| 165 | */ |
||
| 166 | public function getY() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return float |
||
| 173 | */ |
||
| 174 | public function getWidth() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param float $width |
||
| 181 | */ |
||
| 182 | public function setWidth($width) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return float |
||
| 189 | */ |
||
| 190 | public function getHeight() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param float $height |
||
| 197 | */ |
||
| 198 | public function setHeight($height) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param object $element |
||
| 205 | */ |
||
| 206 | public function addChild(Renderable $element) |
||
| 212 | |||
| 213 | public function getChildren() |
||
| 217 | |||
| 218 | |||
| 219 | public function addClass($class) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Add a new child |
||
| 226 | * |
||
| 227 | * @api |
||
| 228 | * @param Renderable $child Child Control to add |
||
| 229 | * @return static |
||
| 230 | * @deprecated Use addChild() |
||
| 231 | * @see Container::addChild() |
||
| 232 | */ |
||
| 233 | public function add(Renderable $child) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Add new children |
||
| 240 | * |
||
| 241 | * @api |
||
| 242 | * @param Renderable[] $children Child Controls to add |
||
| 243 | * @return static |
||
| 244 | */ |
||
| 245 | public function addChildren(array $children) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Remove all children |
||
| 254 | * |
||
| 255 | * @api |
||
| 256 | * @return static |
||
| 257 | */ |
||
| 258 | public function removeAllChildren() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Remove all children |
||
| 269 | * |
||
| 270 | * @api |
||
| 271 | * @return static |
||
| 272 | * @deprecated Use removeAllChildren() |
||
| 273 | * @see Container::removeAllChildren() |
||
| 274 | */ |
||
| 275 | public function removeChildren() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the Format |
||
| 282 | * |
||
| 283 | * @api |
||
| 284 | * @param bool $createIfEmpty If the format should be created if it doesn't exist yet |
||
| 285 | * @return Format |
||
| 286 | * @deprecated Use Style |
||
| 287 | * @see Style |
||
| 288 | */ |
||
| 289 | public function getFormat($createIfEmpty = true) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Set the Format |
||
| 296 | * |
||
| 297 | * @api |
||
| 298 | * @param Format $format New Format |
||
| 299 | * @return void |
||
| 300 | * @deprecated Use Style |
||
| 301 | * @see Style |
||
| 302 | */ |
||
| 303 | public function setFormat(Format $format = null) |
||
| 307 | |||
| 308 | public function setAlign($hAling = "left", $vAlign = "top") |
||
| 315 | |||
| 316 | |||
| 317 | public function setPosition($x, $y) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return null|string |
||
| 327 | */ |
||
| 328 | public function getId() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param null|string $frameId |
||
| 335 | */ |
||
| 336 | public function setId($frameId) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function getHorizontalAlign(): string |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param string $hAlign |
||
| 353 | */ |
||
| 354 | public function setHorizontalAlign(string $hAlign) |
||
| 360 | |||
| 361 | } |
||
| 362 |
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.