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 |
||
| 12 | View Code Duplication | class layoutRow implements Renderable, ScriptFeatureable |
|
|
|
|||
| 13 | { |
||
| 14 | private $height = 0; |
||
| 15 | private $width = 0; |
||
| 16 | |||
| 17 | /** @var Control[] */ |
||
| 18 | private $elements = []; |
||
| 19 | |||
| 20 | private $margin = 1; |
||
| 21 | /** |
||
| 22 | * @var |
||
| 23 | */ |
||
| 24 | private $startX; |
||
| 25 | /** |
||
| 26 | * @var |
||
| 27 | */ |
||
| 28 | private $startY; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * layoutLine constructor. |
||
| 32 | * @param $startX |
||
| 33 | * @param $startY |
||
| 34 | * @param Control[] $elements |
||
| 35 | * @param int $margin |
||
| 36 | * @throws \Exception |
||
| 37 | */ |
||
| 38 | public function __construct($startX, $startY, $elements = [], $margin = 0) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Render the XML element |
||
| 57 | * |
||
| 58 | * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered |
||
| 59 | * @return \DOMElement |
||
| 60 | */ |
||
| 61 | public function render(\DOMDocument $domDocument) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the Script Features |
||
| 78 | * |
||
| 79 | * @return ScriptFeature[] |
||
| 80 | */ |
||
| 81 | public function getScriptFeatures() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param mixed $startX |
||
| 95 | * @return static |
||
| 96 | */ |
||
| 97 | public function setX($startX) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param mixed $startY |
||
| 106 | * @return static |
||
| 107 | */ |
||
| 108 | public function setY($startY) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return mixed |
||
| 117 | */ |
||
| 118 | public function getX() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | public function getY() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return float |
||
| 133 | */ |
||
| 134 | public function getHeight() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param float $height |
||
| 141 | */ |
||
| 142 | public function setHeight($height) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param float $width |
||
| 149 | * @return layoutRow |
||
| 150 | */ |
||
| 151 | public function setWidth($width) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return float|int |
||
| 160 | */ |
||
| 161 | public function getWidth() |
||
| 165 | |||
| 166 | public function addElement($element) |
||
| 172 | |||
| 173 | } |
||
| 174 |
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.