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 HtmlButton extends HtmlBsDoubleElement { |
||
16 | |||
17 | /** |
||
18 | * Constructs an HTML Bootstrap button |
||
19 | * @param string $identifier HTML id |
||
20 | * @param string $value value of the Button |
||
21 | * @param string $cssStyle btn-default, btn-primary... |
||
22 | * @param string $onClick JS Code for click event |
||
23 | */ |
||
24 | View Code Duplication | public function __construct($identifier, $value="", $cssStyle=null, $onClick=null) { |
|
|
|||
25 | parent::__construct($identifier, "button"); |
||
26 | $this->setProperty("class", "btn btn-default"); |
||
27 | $this->setProperty("role", "button"); |
||
28 | $this->content=$value; |
||
29 | if (isset($cssStyle)) { |
||
30 | $this->setStyle($cssStyle); |
||
31 | } |
||
32 | if (isset($onClick)) { |
||
33 | $this->onClick($onClick); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Set the button value |
||
39 | * @param string $value |
||
40 | * @return \Ajax\bootstrap\html\HtmlButton |
||
41 | */ |
||
42 | public function setValue($value) { |
||
46 | |||
47 | /** |
||
48 | * define the button style |
||
49 | * avaible values : "btn-default","btn-primary","btn-success","btn-info","btn-warning","btn-danger" |
||
50 | * @param string|int $cssStyle |
||
51 | * @return \Ajax\bootstrap\html\HtmlButton default : "btn-default" |
||
52 | */ |
||
53 | public function setStyle($cssStyle) { |
||
56 | |||
57 | /** |
||
58 | * define the button size |
||
59 | * available values : "btn-lg","","btn-sm","btn-xs" |
||
60 | * @param string|int $size |
||
61 | * @return \Ajax\bootstrap\html\HtmlButton default : "" |
||
62 | */ |
||
63 | public function setSize($size) { |
||
69 | |||
70 | /** |
||
71 | * Sets the "active" css class to the button |
||
72 | * @return \Ajax\bootstrap\html\HtmlButton |
||
73 | */ |
||
74 | public function setActive() { |
||
79 | |||
80 | /** |
||
81 | * Disables the button |
||
82 | * @return \Ajax\bootstrap\html\HtmlButton |
||
83 | */ |
||
84 | public function setDisabled() { |
||
89 | |||
90 | public function setToggled() { |
||
93 | |||
94 | /* |
||
95 | * (non-PHPdoc) |
||
96 | * @see BaseHtml::run() |
||
97 | */ |
||
98 | public function run(JsUtils $js) { |
||
106 | |||
107 | |||
108 | |||
109 | /* |
||
110 | * (non-PHPdoc) |
||
111 | * @see \Ajax\bootstrap\html\BaseHtml::fromArray() |
||
112 | */ |
||
113 | public function fromArray($array) { |
||
120 | } |
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.