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 | public function __construct($startX, $startY, $elements = [], $margin = 0) |
||
| 64 | |||
| 65 | protected function updateSize() { |
||
| 77 | |||
| 78 | public function setPosition($x, $y) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param mixed $startX |
||
| 86 | * @return |
||
| 87 | */ |
||
| 88 | public function setX($startX) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param mixed $startY |
||
| 97 | * @return |
||
| 98 | */ |
||
| 99 | 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 | public function render(\DOMDocument $domDocument) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the Script Features |
||
| 134 | * |
||
| 135 | * @return ScriptFeature[] |
||
| 136 | */ |
||
| 137 | View Code Duplication | public function getScriptFeatures() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return mixed |
||
| 150 | */ |
||
| 151 | public function getX() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | public function getY() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return float |
||
| 166 | */ |
||
| 167 | public function getHeight() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param float $height |
||
| 174 | */ |
||
| 175 | public function setHeight($height) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param float $width |
||
| 182 | * @return layoutRow |
||
| 183 | */ |
||
| 184 | public function setWidth($width) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return float|int |
||
| 193 | */ |
||
| 194 | public function getWidth() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param Renderable $element |
||
| 201 | */ |
||
| 202 | public function addChild(Renderable $element) |
||
| 207 | |||
| 208 | public function getChildren() |
||
| 212 | |||
| 213 | |||
| 214 | public function addClass($class) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Add a new child |
||
| 221 | * |
||
| 222 | * @api |
||
| 223 | * @param Renderable $child Child Control to add |
||
| 224 | * @return static |
||
| 225 | * @deprecated Use addChild() |
||
| 226 | * @see Container::addChild() |
||
| 227 | */ |
||
| 228 | public function add(Renderable $child) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Add new children |
||
| 235 | * |
||
| 236 | * @api |
||
| 237 | * @param Renderable[] $children Child Controls to add |
||
| 238 | * @return static |
||
| 239 | */ |
||
| 240 | public function addChildren(array $children) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Remove all children |
||
| 247 | * |
||
| 248 | * @api |
||
| 249 | * @return static |
||
| 250 | */ |
||
| 251 | public function removeAllChildren() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Remove all children |
||
| 258 | * |
||
| 259 | * @api |
||
| 260 | * @return static |
||
| 261 | * @deprecated Use removeAllChildren() |
||
| 262 | * @see Container::removeAllChildren() |
||
| 263 | */ |
||
| 264 | public function removeChildren() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Get the Format |
||
| 271 | * |
||
| 272 | * @api |
||
| 273 | * @param bool $createIfEmpty If the format should be created if it doesn't exist yet |
||
| 274 | * @return Format |
||
| 275 | * @deprecated Use Style |
||
| 276 | * @see Style |
||
| 277 | */ |
||
| 278 | public function getFormat($createIfEmpty = true) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Set the Format |
||
| 285 | * |
||
| 286 | * @api |
||
| 287 | * @param Format $format New Format |
||
| 288 | * @return static |
||
| 289 | * @deprecated Use Style |
||
| 290 | * @see Style |
||
| 291 | */ |
||
| 292 | public function setFormat(Format $format = null) |
||
| 296 | |||
| 297 | private function setSize($sizeX, $sizeY) |
||
| 302 | } |
||
| 303 |
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.