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 |
||
| 9 | class ListField extends Field |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Max list size. |
||
| 13 | * |
||
| 14 | * @var int |
||
| 15 | */ |
||
| 16 | protected $max; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Minimum list size. |
||
| 20 | * |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | protected $min = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $value = ['']; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Set Max list size. |
||
| 32 | * |
||
| 33 | * @param int $size |
||
| 34 | * |
||
| 35 | * @return $this |
||
| 36 | */ |
||
| 37 | public function max(int $size) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Set Minimum list size. |
||
| 46 | * |
||
| 47 | * @param int $size |
||
| 48 | * |
||
| 49 | * @return $this |
||
| 50 | */ |
||
| 51 | public function min(int $size) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Fill data to the field. |
||
| 60 | * |
||
| 61 | * @param array $data |
||
| 62 | * |
||
| 63 | * @return void |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function fill($data) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | public function getValidator(array $input) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | protected function setupScript() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | public function prepare($value) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | public function render() |
||
| 153 | } |
||
| 154 |
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.