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 |
||
17 | class HtmlButton extends HtmlSemDoubleElement { |
||
18 | use LabeledIconTrait; |
||
19 | |||
20 | /** |
||
21 | * Constructs an HTML Semantic button |
||
22 | * @param string $identifier HTML id |
||
23 | * @param string $value value of the Button |
||
24 | * @param string $cssStyle btn-default, btn-primary... |
||
25 | * @param string $onClick JS Code for click event |
||
26 | */ |
||
27 | View Code Duplication | public function __construct($identifier, $value="", $cssStyle=null, $onClick=null) { |
|
37 | |||
38 | /** |
||
39 | * Set the button value |
||
40 | * @param string $value |
||
41 | * @return \Ajax\semantic\html\HtmlButton |
||
42 | */ |
||
43 | public function setValue($value) { |
||
47 | |||
48 | /** |
||
49 | * define the button style |
||
50 | * @param string|int $cssStyle |
||
51 | * @return \Ajax\semantic\html\HtmlButton default : "" |
||
52 | */ |
||
53 | public function setStyle($cssStyle) { |
||
56 | |||
57 | public function setFocusable($value=true) { |
||
65 | |||
66 | public function setAnimated($content, $animation="") { |
||
78 | |||
79 | /** |
||
80 | * |
||
81 | * @param string|HtmlIcon $icon |
||
82 | * @return \Ajax\semantic\html\elements\HtmlButton |
||
83 | */ |
||
84 | public function asIcon($icon) { |
||
93 | |||
94 | public function asSubmit() { |
||
98 | |||
99 | /** |
||
100 | * Add and return a button label |
||
101 | * @param string $caption |
||
102 | * @param string $before |
||
103 | * @param string $icon |
||
104 | * @return \Ajax\semantic\html\elements\HtmlLabel |
||
105 | */ |
||
106 | View Code Duplication | public function addLabel($label, $before=false, $icon=NULL) { |
|
118 | |||
119 | /* |
||
120 | * (non-PHPdoc) |
||
121 | * @see \Ajax\common\html\BaseHtml::fromArray() |
||
122 | */ |
||
123 | public function fromArray($array) { |
||
130 | |||
131 | /** |
||
132 | * hint towards a positive consequence |
||
133 | * @return \Ajax\semantic\html\elements\HtmlButton |
||
134 | */ |
||
135 | public function setPositive() { |
||
138 | |||
139 | /** |
||
140 | * hint towards a negative consequence |
||
141 | * @return \Ajax\semantic\html\elements\HtmlButton |
||
142 | */ |
||
143 | public function setNegative() { |
||
146 | |||
147 | /** |
||
148 | * formatted to toggle on/off |
||
149 | * @return \Ajax\semantic\html\elements\HtmlButton |
||
150 | */ |
||
151 | public function setToggle() { |
||
154 | |||
155 | /** |
||
156 | * |
||
157 | * @return \Ajax\semantic\html\elements\HtmlButton |
||
158 | */ |
||
159 | public function setCircular() { |
||
162 | |||
163 | /** |
||
164 | * button is less pronounced |
||
165 | * @return \Ajax\semantic\html\elements\HtmlButton |
||
166 | */ |
||
167 | public function setBasic() { |
||
170 | |||
171 | public function setEmphasis($value) { |
||
174 | |||
175 | public function setLoading() { |
||
178 | |||
179 | public static function social($identifier, $social, $value=NULL) { |
||
186 | |||
187 | public static function labeled($identifier, $value, $icon, $before=true) { |
||
192 | |||
193 | public static function icon($identifier, $icon) { |
||
198 | |||
199 | public function asLink($href=NULL) { |
||
204 | } |
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.