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:
Complex classes like HtmlDropdown often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HtmlDropdown, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class HtmlDropdown extends HtmlButton { |
||
16 | protected $btnCaption="Dropdown button"; |
||
17 | protected $class="dropdown-toggle"; |
||
18 | protected $mClass="dropdown"; |
||
19 | protected $mTagName="div"; |
||
20 | protected $items=array (); |
||
21 | |||
22 | /** |
||
23 | * |
||
24 | * @param string $identifier the id |
||
25 | */ |
||
26 | public function __construct($identifier, $value="", $items=array(), $cssStyle=null, $onClick=null) { |
||
27 | parent::__construct($identifier, "", $cssStyle, $onClick); |
||
28 | $this->_template=include 'templates/tplDropdown.php'; |
||
29 | $this->btnCaption=$value; |
||
30 | $this->tagName="a"; |
||
31 | $this->fromArray($items); |
||
32 | if ($cssStyle!==NULL) { |
||
33 | $this->asButton($cssStyle); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Define the tagName of the main element |
||
39 | * @param string $value default : div |
||
40 | */ |
||
41 | public function setMTagName($value) { |
||
42 | $this->mTagName=$value; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * define the button style |
||
47 | * avaible values : "btn-default","btn-primary","btn-success","btn-info","btn-warning","btn-danger" |
||
48 | * @param string|int $cssStyle |
||
49 | * @return \Ajax\bootstrap\html\HtmlDropdown default : "btn-default" |
||
50 | */ |
||
51 | public function setStyle($cssStyle) { |
||
52 | if (is_int($cssStyle)) { |
||
53 | return $this->addToMember($this->class, CssRef::buttonStyles()[$cssStyle]); |
||
54 | } |
||
55 | if (PhalconUtils::startsWith($cssStyle, "btn-")===false) { |
||
56 | $cssStyle="btn".$cssStyle; |
||
57 | } |
||
58 | return $this->addToMemberCtrl($this->class, $cssStyle, CssRef::buttonStyles()); |
||
59 | } |
||
60 | |||
61 | /* |
||
62 | * (non-PHPdoc) |
||
63 | * @see \Ajax\bootstrap\html\HtmlButton::setValue() |
||
64 | */ |
||
65 | public function setValue($value) { |
||
66 | $this->btnCaption=$value; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * define the buttons size |
||
71 | * available values : "btn-group-lg","","btn-group-sm","btn-group-xs" |
||
72 | * @param string|int $size |
||
73 | * @return HtmlDropdown |
||
74 | * default : "" |
||
75 | */ |
||
76 | public function setSize($size) { |
||
77 | if (is_int($size)) { |
||
78 | return $this->addToProperty("class", CssRef::sizes("btn-group")[$size]); |
||
79 | } |
||
80 | return $this->addToPropertyCtrl("class", $size, CssRef::sizes("btn-group")); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * add an HtmlDropdownItem |
||
85 | * @param string $caption |
||
86 | * @param string $href |
||
87 | * @return HtmlDropdownItem |
||
88 | */ |
||
89 | public function addItem($caption, $href="#") { |
||
90 | if($caption instanceof HtmlDropdownItem){ |
||
91 | $item=$caption; |
||
92 | }else{ |
||
93 | $iid=$this->getItemsCount()+1; |
||
94 | $item=new HtmlDropdownItem($this->identifier."-dropdown-item-".$iid); |
||
95 | $item->setCaption($caption)->setHref($href); |
||
96 | } |
||
97 | $this->items []=$item; |
||
98 | return $item; |
||
99 | } |
||
100 | |||
101 | public function addDivider() { |
||
102 | return $this->addItem("-"); |
||
103 | } |
||
104 | |||
105 | public function addHeader($caption) { |
||
106 | return $this->addItem("-".$caption); |
||
107 | } |
||
108 | |||
109 | public function addItems($items) { |
||
110 | $iid=$this->getItemsCount()+1; |
||
111 | if (is_array($items)) { |
||
112 | foreach ( $items as $item ) { |
||
113 | if (is_string($item)) { |
||
114 | $this->addItem($item); |
||
115 | } else if (is_array($item)) { |
||
116 | $dropDownItem=new HtmlDropdownItem($this->identifier."-dropdown-item-".$iid); |
||
117 | $dropDownItem->fromArray($item); |
||
118 | $this->items []=$dropDownItem; |
||
119 | } else if ($item instanceof HtmlDropdownItem) { |
||
120 | $this->items []=$item; |
||
121 | } |
||
122 | } |
||
123 | } |
||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /* |
||
128 | * (non-PHPdoc) |
||
129 | * @see BaseHtml::fromArray() |
||
130 | */ |
||
131 | public function fromArray($array) { |
||
132 | if (array_keys($array)!==range(0, count($array)-1)) |
||
133 | return parent::fromArray($array); |
||
134 | else |
||
135 | return $this->addItems($array); |
||
136 | } |
||
137 | |||
138 | public function setItems($items) { |
||
139 | $this->items=array (); |
||
140 | $this->addItems($items); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Return the item at $index |
||
145 | * @param int $index |
||
146 | * @return HtmlDropdownItem |
||
147 | */ |
||
148 | public function getItem($index) { |
||
149 | return $this->items [$index]; |
||
150 | } |
||
151 | |||
152 | public function setBtnClass($value) { |
||
153 | $this->class=$value; |
||
154 | } |
||
155 | |||
156 | public function setMClass($value) { |
||
157 | $this->mClass=$value; |
||
158 | } |
||
159 | |||
160 | public function addBtnClass($value) { |
||
161 | $this->addToMember($this->class, $value); |
||
162 | } |
||
163 | |||
164 | public function addmClass($value) { |
||
165 | $this->addToMember($this->mClass, $value); |
||
166 | } |
||
167 | |||
168 | /* |
||
169 | * (non-PHPdoc) |
||
170 | * @see BaseHtml::run() |
||
171 | */ |
||
172 | View Code Duplication | public function run(JsUtils $js) { |
|
|
|||
173 | if ($this->getProperty("role")==="nav") { |
||
174 | foreach ( $this->items as $dropdownItem ) { |
||
175 | $dropdownItem->runNav($js); |
||
176 | } |
||
177 | } |
||
178 | $this->_bsComponent=$js->bootstrap()->dropdown("#".$this->identifier); |
||
179 | $this->addEventsOnRun($js); |
||
180 | return $this->_bsComponent; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Sets the tagName's dropdown |
||
185 | * @see \Ajax\bootstrap\html\BaseHtml::setTagName() |
||
186 | */ |
||
187 | public function setTagName($tagName) { |
||
188 | if ($tagName=="button") |
||
189 | $this->class="btn dropdown-toggle"; |
||
190 | return parent::setTagName($tagName); |
||
191 | } |
||
192 | |||
193 | public function __toString() { |
||
194 | return $this->compile(); |
||
195 | } |
||
196 | |||
197 | public function setBtnCaption($btnCaption) { |
||
198 | $this->btnCaption=$btnCaption; |
||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | public function getItemsCount() { |
||
203 | return sizeof($this->items); |
||
204 | } |
||
205 | |||
206 | public function setAlignment($alignment) { |
||
207 | if (is_int($alignment)) |
||
208 | $alignment="dropdown-menu-".CssRef::alignment()[$alignment]; |
||
209 | return $this->addToMemberCtrl($this->class, $alignment, CssRef::alignment()); |
||
210 | } |
||
211 | |||
212 | public function dropup() { |
||
213 | $this->addToMember($this->mClass, "dropup"); |
||
214 | } |
||
215 | |||
216 | public function getItems() { |
||
217 | return $this->items; |
||
218 | } |
||
219 | |||
220 | public function asButton($cssStyle="btn-primary") { |
||
221 | $this->setTagName("button"); |
||
222 | $this->setBtnClass("btn dropdown-toggle"); |
||
223 | $this->setStyle($cssStyle); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * This event fires immediately when the show instance method is called. |
||
228 | * @param string $jsCode |
||
229 | * @return $this |
||
230 | */ |
||
231 | public function onShow($jsCode) { |
||
232 | return $this->addEvent("show.bs.dropdown", $jsCode); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * This event is fired when a dropdown element has been made visible to the user (will wait for CSS transitions to complete). |
||
237 | * @param string $jsCode |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function onShown($jsCode) { |
||
241 | return $this->addEvent("shown.bs.dropdown", $jsCode); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * This event is fired immediately when the hide method has been called. |
||
246 | * @param string $jsCode |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function onHide($jsCode) { |
||
250 | return $this->addEvent("hide.bs.dropdown", $jsCode); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * This event is fired when a dropdown element has been hidden from the user (will wait for CSS transitions to complete). |
||
255 | * @param string $jsCode |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function onHidden($jsCode) { |
||
259 | return $this->addEvent("hidden.bs.dropdown", $jsCode); |
||
260 | } |
||
261 | |||
262 | |||
263 | /* (non-PHPdoc) |
||
264 | * @see \Ajax\bootstrap\html\base\BaseHtml::on() |
||
265 | */ |
||
266 | public function on($event, $jsCode, $stopPropagation = false, $preventDefault = false) { |
||
271 | |||
272 | /* (non-PHPdoc) |
||
273 | * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject() |
||
274 | */ |
||
275 | public function fromDatabaseObject($object, $function) { |
||
276 | $this->addItem($function($object)); |
||
277 | } |
||
278 | |||
279 | } |
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.