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 |
||
15 | class Select extends BaseElement |
||
16 | { |
||
17 | use Autofocus; |
||
18 | use Disabled; |
||
19 | use Name; |
||
20 | use Required; |
||
21 | use Readonly; |
||
22 | |||
23 | /** @var string */ |
||
24 | protected $tag = 'select'; |
||
25 | |||
26 | /** @var array */ |
||
27 | protected $options = []; |
||
28 | |||
29 | /** @var string|iterable */ |
||
30 | protected $value = ''; |
||
31 | |||
32 | /** |
||
33 | * @return static |
||
34 | */ |
||
35 | public function multiple() |
||
51 | |||
52 | /** |
||
53 | * @param iterable $options |
||
54 | * |
||
55 | * @return static |
||
56 | */ |
||
57 | View Code Duplication | public function options($options) |
|
70 | |||
71 | /** |
||
72 | * @param string $label |
||
73 | * @param iterable $options |
||
74 | * |
||
75 | * @return static |
||
76 | */ |
||
77 | View Code Duplication | public function optgroup($label, $options) |
|
90 | |||
91 | /** |
||
92 | * @param string|null $text |
||
93 | * |
||
94 | * @return static |
||
95 | */ |
||
96 | public function placeholder($text) |
||
105 | |||
106 | /** |
||
107 | * @param string|iterable $value |
||
108 | * |
||
109 | * @return static |
||
110 | */ |
||
111 | public function value($value = null) |
||
121 | |||
122 | protected function hasSelection() |
||
126 | |||
127 | protected function applyValueToOptions() |
||
137 | |||
138 | protected function applyValueToElements($value, Collection $children) |
||
152 | } |
||
153 |
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.