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 |
||
21 | class HtmlSemDoubleElement extends HtmlDoubleElement { |
||
22 | use BaseTrait; |
||
23 | protected $_popup=NULL; |
||
24 | protected $_dimmer=NULL; |
||
25 | protected $_params=array (); |
||
26 | |||
27 | |||
28 | View Code Duplication | public function __construct($identifier, $tagName="p", $baseClass="ui", $content=NULL) { |
|
36 | |||
37 | /** |
||
38 | * Defines the popup attributes |
||
39 | * @param string $variation |
||
40 | * @param string $popupEvent |
||
41 | */ |
||
42 | public function setPopupAttributes($variation=NULL, $popupEvent=NULL) { |
||
46 | |||
47 | /** |
||
48 | * Adds a popup to the element |
||
49 | * @param string $title |
||
50 | * @param string $content |
||
51 | * @param string $variation |
||
52 | * @param array $params |
||
53 | * @return HtmlSemDoubleElement |
||
54 | */ |
||
55 | public function addPopup($title="", $content="", $variation=NULL, $params=array()) { |
||
59 | |||
60 | /** |
||
61 | * Adds an html popup to the element |
||
62 | * @param string $html |
||
63 | * @param string $variation |
||
64 | * @param array $params |
||
65 | * @return HtmlSemDoubleElement |
||
66 | */ |
||
67 | public function addPopupHtml($html="", $variation=NULL, $params=array()) { |
||
73 | |||
74 | /** |
||
75 | * Adds a Dimmer to the element |
||
76 | * @param array $params |
||
77 | * @param mixed $content |
||
78 | * @return HtmlDimmer |
||
79 | */ |
||
80 | public function addDimmer($params=array(), $content=NULL) { |
||
87 | |||
88 | /** |
||
89 | * Adds a label to the element |
||
90 | * @param mixed $label |
||
91 | * @param boolean $before |
||
92 | * @param string $icon |
||
93 | * @return mixed|HtmlLabel |
||
94 | */ |
||
95 | public function addLabel($label, $before=false, $icon=NULL) { |
||
107 | |||
108 | /** |
||
109 | * Adds an attached label to the element |
||
110 | * @param mixed $label |
||
111 | * @param string $side |
||
112 | * @param string $direction |
||
113 | * @param string $icon |
||
114 | * @return HtmlSemDoubleElement |
||
115 | */ |
||
116 | public function attachLabel($label,$side=Side::TOP,$direction=Direction::NONE,$icon=NULL){ |
||
121 | |||
122 | /** |
||
123 | * Transforms the element into a link |
||
124 | * @return HtmlSemDoubleElement |
||
125 | */ |
||
126 | public function asLink($href=NULL,$target=NULL) { |
||
133 | |||
134 | /** |
||
135 | * Returns the script displaying the dimmer |
||
136 | * @param boolean $show |
||
137 | * @return string |
||
138 | */ |
||
139 | public function jsShowDimmer($show=true) { |
||
145 | |||
146 | /** |
||
147 | * {@inheritDoc} |
||
148 | * @see BaseHtml::compile() |
||
149 | */ |
||
150 | public function compile(JsUtils $js=NULL, &$view=NULL) { |
||
155 | |||
156 | /** |
||
157 | * {@inheritDoc} |
||
158 | * @see HtmlDoubleElement::run() |
||
159 | */ |
||
160 | public function run(JsUtils $js) { |
||
169 | |||
170 | public function addList($elements,$ordered=false){ |
||
176 | |||
177 | /* |
||
178 | * public function __call($name, $arguments){ |
||
179 | * $type=\substr($name, 0,3); |
||
180 | * $name=\strtolower(\substr($name, 3)); |
||
181 | * $names=\array_merge($this->_variations,$this->_states); |
||
182 | * $argument=@$arguments[0]; |
||
183 | * if(\array_search($name, $names)!==FALSE){ |
||
184 | * switch ($type){ |
||
185 | * case "set": |
||
186 | * if($argument===false){ |
||
187 | * $this->removePropertyValue("class", $name); |
||
188 | * }else { |
||
189 | * $this->setProperty("class", $this->_baseClass." ".$name); |
||
190 | * } |
||
191 | * break; |
||
192 | * case "add": |
||
193 | * $this->addToPropertyCtrl("class", $name,array($name)); |
||
194 | * break; |
||
195 | * default: |
||
196 | * throw new \Exception("Méthode ".$type.$name." inexistante."); |
||
197 | * } |
||
198 | * }else{ |
||
199 | * throw new \Exception("Propriété ".$name." inexistante."); |
||
200 | * } |
||
201 | * return $this; |
||
202 | * } |
||
203 | */ |
||
204 | } |
||
205 |
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.