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 |
||
| 24 | class Text extends Element |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * __construct |
||
| 28 | * |
||
| 29 | * @param string|array $caption Caption or array of all attributes |
||
| 30 | * @param string $name name attribute |
||
| 31 | * @param integer $size Size |
||
| 32 | * @param integer $maxlength Maximum length of text |
||
| 33 | * @param string $value Initial text |
||
| 34 | * @param string $placeholder placeholder for this element. |
||
| 35 | */ |
||
| 36 | 5 | View Code Duplication | public function __construct($caption, $name = null, $size = 10, $maxlength = 64, $value = '', $placeholder = '') |
| 37 | { |
||
| 38 | 5 | if (is_array($caption)) { |
|
| 39 | 3 | parent::__construct($caption); |
|
| 40 | } else { |
||
| 41 | 2 | parent::__construct([]); |
|
| 42 | 2 | $this->setWithDefaults('caption', $caption, ''); |
|
| 43 | 2 | $this->setWithDefaults('name', $name, 'name_error'); |
|
| 44 | 2 | $this->setWithDefaults('size', $size, 10); |
|
| 45 | 2 | $this->setWithDefaults('maxlength', $maxlength, 64); |
|
| 46 | 2 | $this->set('value', $value); |
|
| 47 | 2 | $this->setIfNotEmpty('placeholder', $placeholder); |
|
| 48 | } |
||
| 49 | 5 | $this->setIfNotSet('type', 'text'); |
|
| 50 | 5 | $this->setIfNotSet('value', ''); |
|
| 51 | 5 | } |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Get size |
||
| 55 | * |
||
| 56 | * @return int |
||
| 57 | */ |
||
| 58 | 1 | public function getSize() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Get maximum text length |
||
| 65 | * |
||
| 66 | * @return int |
||
| 67 | */ |
||
| 68 | 1 | public function getMaxlength() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Get placeholder for this element |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | 1 | public function getPlaceholder() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Prepare HTML for output |
||
| 85 | * |
||
| 86 | * @return string HTML |
||
| 87 | */ |
||
| 88 | 4 | public function render() |
|
| 99 | } |
||
| 100 |