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 |
||
18 | class HtmlButton extends HtmlSemDoubleElement { |
||
19 | use LabeledIconTrait; |
||
20 | |||
21 | /** |
||
22 | * Constructs an HTML Semantic button |
||
23 | * @param string $identifier HTML id |
||
24 | * @param string $value value of the Button |
||
25 | * @param string $cssStyle btn-default, btn-primary... |
||
26 | * @param string $onClick JS Code for click event |
||
27 | */ |
||
28 | View Code Duplication | public function __construct($identifier, $value="", $cssStyle=null, $onClick=null) { |
|
38 | |||
39 | /** |
||
40 | * Set the button value |
||
41 | * @param string $value |
||
42 | * @return HtmlButton |
||
43 | */ |
||
44 | public function setValue($value) { |
||
48 | |||
49 | /** |
||
50 | * define the button style |
||
51 | * @param string|int $cssStyle |
||
52 | * @return HtmlButton default : "" |
||
53 | */ |
||
54 | public function setStyle($cssStyle) { |
||
57 | |||
58 | public function setFocusable($value=true) { |
||
66 | |||
67 | public function setAnimated($content, $animation="") { |
||
79 | |||
80 | /** |
||
81 | * |
||
82 | * @param string|HtmlIcon $icon |
||
83 | * @return HtmlButton |
||
84 | */ |
||
85 | public function asIcon($icon) { |
||
94 | |||
95 | public function asSubmit() { |
||
99 | |||
100 | /** |
||
101 | * Add and return a button label |
||
102 | * @param string $label |
||
103 | * @param boolean $before |
||
104 | * @param string $icon |
||
105 | * @return HtmlLabel |
||
106 | */ |
||
107 | public function addLabel($label, $before=false, $icon=NULL) { |
||
119 | |||
120 | /* |
||
121 | * (non-PHPdoc) |
||
122 | * @see \Ajax\common\html\BaseHtml::fromArray() |
||
123 | */ |
||
124 | public function fromArray($array) { |
||
131 | |||
132 | /** |
||
133 | * hint towards a positive consequence |
||
134 | * @return HtmlButton |
||
135 | */ |
||
136 | public function setPositive() { |
||
139 | |||
140 | public function setColor($color){ |
||
152 | |||
153 | /** |
||
154 | * hint towards a negative consequence |
||
155 | * @return HtmlButton |
||
156 | */ |
||
157 | public function setNegative() { |
||
160 | |||
161 | /** |
||
162 | * formatted to toggle on/off |
||
163 | * @return HtmlButton |
||
164 | */ |
||
165 | public function setToggle() { |
||
169 | |||
170 | /** |
||
171 | * |
||
172 | * @return HtmlButton |
||
173 | */ |
||
174 | public function setCircular() { |
||
177 | |||
178 | /** |
||
179 | * button is less pronounced |
||
180 | * @return HtmlButton |
||
181 | */ |
||
182 | public function setBasic() { |
||
185 | |||
186 | public function setEmphasis($value) { |
||
189 | |||
190 | public function setLoading() { |
||
193 | |||
194 | /** |
||
195 | * Returns a new social Button |
||
196 | * @param string $identifier |
||
197 | * @param string $social |
||
198 | * @param string $value |
||
199 | * @return HtmlButton |
||
200 | */ |
||
201 | public static function social($identifier, $social, $value=NULL) { |
||
208 | |||
209 | /** |
||
210 | * Returns a new labeled Button |
||
211 | * @param string $identifier |
||
212 | * @param string $value |
||
213 | * @param string $icon |
||
214 | * @param boolean $before |
||
215 | * @return \Ajax\semantic\html\elements\HtmlButton |
||
216 | */ |
||
217 | public static function labeled($identifier, $value, $icon, $before=true) { |
||
222 | |||
223 | /** |
||
224 | * Returns a new icon Button |
||
225 | * @param string $identifier |
||
226 | * @param string $icon |
||
227 | * @return HtmlButton |
||
228 | */ |
||
229 | public static function icon($identifier, $icon) { |
||
234 | |||
235 | /** |
||
236 | * {@inheritDoc} |
||
237 | * @see HtmlSemDoubleElement::asLink() |
||
238 | */ |
||
239 | public function asLink($href=NULL,$target=NULL) { |
||
244 | } |
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.