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 |
||
| 11 | class Checkbox extends MultiSelect |
||
| 12 | { |
||
| 13 | protected $type = 'checkbox'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * maximum number of checkbox checked |
||
| 17 | * |
||
| 18 | * @var int|null |
||
| 19 | */ |
||
| 20 | protected $max; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * minimum number of checkbox checked |
||
| 24 | * |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | protected $min = 0; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * If show check all button. |
||
| 31 | * |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $showCheckAll = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Get maximum number. |
||
| 38 | * |
||
| 39 | * @return int|null |
||
| 40 | */ |
||
| 41 | public function getMax() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Set maximum number. |
||
| 48 | * |
||
| 49 | * @param int $value |
||
| 50 | * |
||
| 51 | * @return $this |
||
| 52 | */ |
||
| 53 | public function setMax(int $value) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get minimum number. |
||
| 64 | * |
||
| 65 | * @return int |
||
| 66 | */ |
||
| 67 | public function getMin() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Set minimum number. |
||
| 74 | * |
||
| 75 | * @param int $value |
||
| 76 | * |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | public function setMin(int $value) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Determine if show check all button. |
||
| 90 | * |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | public function isShowCheckAll() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Show check all button. |
||
| 100 | * |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | public function enableShowCheckAll() |
||
| 109 | |||
| 110 | View Code Duplication | public function toArray() |
|
| 118 | } |
||
| 119 |
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.