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 |
||
13 | class Bootstrap extends CApplicationComponent |
||
14 | { |
||
15 | // Bootstrap plugins. |
||
16 | const PLUGIN_AFFIX = 'affix'; |
||
17 | const PLUGIN_ALERT = 'alert'; |
||
18 | const PLUGIN_BUTTON = 'button'; |
||
19 | const PLUGIN_CAROUSEL = 'carousel'; |
||
20 | const PLUGIN_COLLAPSE = 'collapse'; |
||
21 | const PLUGIN_DROPDOWN = 'dropdown'; |
||
22 | const PLUGIN_MODAL = 'modal'; |
||
23 | const PLUGIN_POPOVER = 'popover'; |
||
24 | const PLUGIN_SCROLLSPY = 'scrollspy'; |
||
25 | const PLUGIN_TAB = 'tab'; |
||
26 | const PLUGIN_TOOLTIP = 'tooltip'; |
||
27 | const PLUGIN_TRANSITION = 'transition'; |
||
28 | const PLUGIN_TYPEAHEAD = 'typeahead'; |
||
29 | |||
30 | /** |
||
31 | * @var array plugin initial options (name=>options). |
||
32 | * Each array key-value pair represents the initial options for a single plugin class, |
||
33 | * with the array key being the plugin name, and array value being the initial options array. |
||
34 | * @since 0.9.8 |
||
35 | */ |
||
36 | public $plugins = array(); |
||
37 | /** |
||
38 | * @var boolean indicates whether assets should be republished on every request. |
||
39 | */ |
||
40 | public $forceCopyAssets = false; |
||
41 | |||
42 | protected $_assetsUrl; |
||
43 | |||
44 | /** |
||
45 | * Registers the Bootstrap CSS. |
||
46 | */ |
||
47 | public function registerCoreCss() |
||
52 | |||
53 | /** |
||
54 | * Registers the Bootstrap responsive CSS. |
||
55 | * @since 0.9.8 |
||
56 | */ |
||
57 | public function registerResponsiveCss() |
||
65 | |||
66 | /** |
||
67 | * Registers the Yii-specific CSS missing from Bootstrap. |
||
68 | * @since 0.9.11 |
||
69 | */ |
||
70 | public function registerYiiCss() |
||
74 | |||
75 | /** |
||
76 | * Registers all Bootstrap CSS. |
||
77 | * @since 2.0.0 |
||
78 | */ |
||
79 | public function registerAllCss() |
||
85 | |||
86 | /** |
||
87 | * Registers the core JavaScript. |
||
88 | * @since 0.9.8 |
||
89 | */ |
||
90 | public function registerCoreScripts() |
||
95 | |||
96 | /** |
||
97 | * Registers the Bootstrap JavaScript. |
||
98 | * @param int $position the position of the JavaScript code. |
||
99 | */ |
||
100 | protected function registerJS($position = CClientScript::POS_HEAD) |
||
108 | |||
109 | /** |
||
110 | * Registers all Bootstrap CSS and JavaScript. |
||
111 | * @since 2.1.0 |
||
112 | */ |
||
113 | public function register() |
||
118 | |||
119 | /** |
||
120 | * Registers the Bootstrap affix plugin. |
||
121 | * @param string $selector the CSS selector |
||
122 | * @param array $options the plugin options |
||
123 | * @see http://twitter.github.com/bootstrap/javascript.html#affix |
||
124 | * @since 2.0.0 |
||
125 | */ |
||
126 | public function registerAffix($selector = null, $options = array()) |
||
130 | |||
131 | /** |
||
132 | * Registers the Bootstrap alert plugin. |
||
133 | * @param string $selector the CSS selector |
||
134 | * @param array $options the plugin options |
||
135 | * @see http://twitter.github.com/bootstrap/javascript.html#alerts |
||
136 | * @since 0.9.8 |
||
137 | */ |
||
138 | public function registerAlert($selector = null, $options = array()) |
||
142 | |||
143 | /** |
||
144 | * Registers the Bootstrap buttons plugin. |
||
145 | * @param string $selector the CSS selector |
||
146 | * @param array $options the plugin options |
||
147 | * @see http://twitter.github.com/bootstrap/javascript.html#buttons |
||
148 | * @since 0.9.8 |
||
149 | */ |
||
150 | public function registerButton($selector = null, $options = array()) |
||
154 | |||
155 | /** |
||
156 | * Registers the Bootstrap carousel plugin. |
||
157 | * @param string $selector the CSS selector |
||
158 | * @param array $options the plugin options |
||
159 | * @see http://twitter.github.com/bootstrap/javascript.html#carousel |
||
160 | * @since 0.9.8 |
||
161 | */ |
||
162 | public function registerCarousel($selector = null, $options = array()) |
||
166 | |||
167 | /** |
||
168 | * Registers the Bootstrap collapse plugin. |
||
169 | * @param string $selector the CSS selector |
||
170 | * @param array $options the plugin options |
||
171 | * @see http://twitter.github.com/bootstrap/javascript.html#collapse |
||
172 | * @since 0.9.8 |
||
173 | */ |
||
174 | public function registerCollapse($selector = null, $options = array()) |
||
178 | |||
179 | /** |
||
180 | * Registers the Bootstrap dropdowns plugin. |
||
181 | * @param string $selector the CSS selector |
||
182 | * @param array $options the plugin options |
||
183 | * @see http://twitter.github.com/bootstrap/javascript.html#dropdowns |
||
184 | * @since 0.9.8 |
||
185 | */ |
||
186 | public function registerDropdown($selector = null, $options = array()) |
||
190 | |||
191 | /** |
||
192 | * Registers the Bootstrap modal plugin. |
||
193 | * @param string $selector the CSS selector |
||
194 | * @param array $options the plugin options |
||
195 | * @see http://twitter.github.com/bootstrap/javascript.html#modal |
||
196 | * @since 0.9.8 |
||
197 | */ |
||
198 | public function registerModal($selector = null, $options = array()) |
||
202 | |||
203 | /** |
||
204 | * Registers the Bootstrap scrollspy plugin. |
||
205 | * @param string $selector the CSS selector |
||
206 | * @param array $options the plugin options |
||
207 | * @see http://twitter.github.com/bootstrap/javascript.html#scrollspy |
||
208 | * @since 0.9.8 |
||
209 | */ |
||
210 | public function registerScrollSpy($selector = null, $options = array()) |
||
214 | |||
215 | /** |
||
216 | * Registers the Bootstrap popover plugin. |
||
217 | * @param string $selector the CSS selector |
||
218 | * @param array $options the plugin options |
||
219 | * @see http://twitter.github.com/bootstrap/javascript.html#popover |
||
220 | * @since 0.9.8 |
||
221 | */ |
||
222 | public function registerPopover($selector = null, $options = array()) |
||
229 | |||
230 | /** |
||
231 | * Registers the Bootstrap tabs plugin. |
||
232 | * @param string $selector the CSS selector |
||
233 | * @param array $options the plugin options |
||
234 | * @see http://twitter.github.com/bootstrap/javascript.html#tabs |
||
235 | * @since 0.9.8 |
||
236 | */ |
||
237 | public function registerTabs($selector = null, $options = array()) |
||
241 | |||
242 | /** |
||
243 | * Registers the Bootstrap tooltip plugin. |
||
244 | * @param string $selector the CSS selector |
||
245 | * @param array $options the plugin options |
||
246 | * @see http://twitter.github.com/bootstrap/javascript.html#tooltip |
||
247 | * @since 0.9.8 |
||
248 | */ |
||
249 | public function registerTooltip($selector = null, $options = array()) |
||
255 | |||
256 | /** |
||
257 | * Registers the Bootstrap typeahead plugin. |
||
258 | * @param string $selector the CSS selector |
||
259 | * @param array $options the plugin options |
||
260 | * @see http://twitter.github.com/bootstrap/javascript.html#typeahead |
||
261 | * @since 0.9.8 |
||
262 | */ |
||
263 | public function registerTypeahead($selector = null, $options = array()) |
||
267 | |||
268 | /** |
||
269 | * Registers a Bootstrap JavaScript plugin. |
||
270 | * @param string $name the name of the plugin |
||
271 | * @param string $selector the CSS selector |
||
272 | * @param array $options the plugin options |
||
273 | * @param string $defaultSelector the default CSS selector |
||
274 | * @since 0.9.8 |
||
275 | */ |
||
276 | protected function registerPlugin($name, $selector = null, $options = array()) |
||
294 | |||
295 | /** |
||
296 | * Returns the URL to the published assets folder. |
||
297 | * @return string the URL |
||
298 | */ |
||
299 | protected function getAssetsUrl() |
||
310 | |||
311 | /** |
||
312 | * Returns the extension version number. |
||
313 | * @return string the version |
||
314 | */ |
||
315 | public function getVersion() |
||
319 | } |
||
320 |
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.