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 Input extends AbstractUiElement implements Renderable, ScriptFeatureable |
||
| 15 | { |
||
| 16 | |||
| 17 | const TYPE_DEFAULT = "Basic"; |
||
| 18 | const TYPE_PASSWORD = "Password"; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $name; |
||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $default; |
||
| 28 | |||
| 29 | /** @var null|string */ |
||
| 30 | protected $action = null; |
||
| 31 | |||
| 32 | protected $textFormat = "Basic"; |
||
| 33 | |||
| 34 | /** @var null|string */ |
||
| 35 | protected $id = null; |
||
| 36 | |||
| 37 | protected $horizontalAlign = "left"; |
||
| 38 | |||
| 39 | View Code Duplication | public function __construct($name, $default = "", $width = 30, $textFormat = "Basic") |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Render the XML element |
||
| 50 | * |
||
| 51 | * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered |
||
| 52 | * @return \DOMElement |
||
| 53 | */ |
||
| 54 | public function render(\DOMDocument $domDocument) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | public function getName() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $name |
||
| 108 | */ |
||
| 109 | public function setName($name) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function getDefault() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $default |
||
| 126 | */ |
||
| 127 | public function setDefault($default) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return integer |
||
| 136 | */ |
||
| 137 | public function getWidth() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param float $width |
||
| 144 | */ |
||
| 145 | public function setWidth($width) |
||
| 151 | |||
| 152 | public function getHeight() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Prepare the given Script for rendering by adding the needed Labels, etc. |
||
| 159 | * |
||
| 160 | * @param Script $script Script to prepare |
||
| 161 | */ |
||
| 162 | public function prepare(Script $script) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get the Script Features |
||
| 172 | * |
||
| 173 | * @return ScriptFeature[] |
||
| 174 | */ |
||
| 175 | public function getScriptFeatures() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | public function getTextFormat() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $textFormat |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | public function setTextFormat($textFormat) |
||
| 198 | |||
| 199 | protected function getScriptMouseClick() |
||
| 209 | |||
| 210 | protected function getScriptEntrySubmit() |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * @return null|string |
||
| 223 | */ |
||
| 224 | public function getAction() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param null|string $action |
||
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | public function setAction($action) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return null|string |
||
| 242 | */ |
||
| 243 | public function getId(): string |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param null|string $id |
||
| 250 | */ |
||
| 251 | public function setId(string $id) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public function getHorizontalAlign(): string |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $horizontalAlign |
||
| 266 | */ |
||
| 267 | public function setHorizontalAlign(string $horizontalAlign) |
||
| 271 | } |
||
| 272 |
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.