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 (defined('BASEPATH')) OR exit('No direct script access allowed'); |
||
9 | class Page { |
||
10 | |||
11 | private $meta, $head_js, $foot_js, $css, $title, |
||
12 | $head_tags, $body_id; |
||
13 | |||
14 | /** |
||
15 | * Current Controller Instance |
||
16 | * |
||
17 | * @var CI_Controller |
||
18 | */ |
||
19 | private $CI; |
||
20 | |||
21 | /** |
||
22 | * Constructor |
||
23 | */ |
||
24 | 2 | public function __construct() |
|
35 | |||
36 | // -------------------------------------------------------------------------- |
||
37 | |||
38 | /** |
||
39 | * Sets server headers and doctype |
||
40 | * |
||
41 | * Also sets page mime type, based on if sent as |
||
42 | * html or xhtml, and what the target browser |
||
43 | * supports |
||
44 | * |
||
45 | * @return Page |
||
46 | */ |
||
47 | private function _headers() |
||
65 | |||
66 | // -------------------------------------------------------------------------- |
||
67 | |||
68 | /** |
||
69 | * Set Meta |
||
70 | * |
||
71 | * Sets meta tags, with codeigniter native meta tag helper |
||
72 | * |
||
73 | * @param array $meta |
||
74 | * @return Page |
||
75 | */ |
||
76 | 1 | public function set_meta($meta) |
|
81 | |||
82 | // -------------------------------------------------------------------------- |
||
83 | |||
84 | /** |
||
85 | * Sets minified javascript group in header |
||
86 | * @param string $group |
||
87 | * @param bool $debug |
||
88 | * @return Page |
||
89 | */ |
||
90 | View Code Duplication | public function set_head_js_group($group, $debug=FALSE) |
|
97 | |||
98 | // -------------------------------------------------------------------------- |
||
99 | |||
100 | /** |
||
101 | * Sets a minified css group |
||
102 | * @param string $group |
||
103 | * @return Page |
||
104 | */ |
||
105 | public function set_css_group($group) |
||
116 | |||
117 | // -------------------------------------------------------------------------- |
||
118 | |||
119 | /** |
||
120 | * Sets a minified javascript group for the page footer |
||
121 | * @param string $group |
||
122 | * @return Page |
||
123 | */ |
||
124 | View Code Duplication | public function set_foot_js_group($group, $debug=FALSE) |
|
131 | |||
132 | // -------------------------------------------------------------------------- |
||
133 | |||
134 | /** |
||
135 | * Sets html title string |
||
136 | * @param string $title |
||
137 | * @return Page |
||
138 | */ |
||
139 | public function set_title($title="") |
||
148 | |||
149 | // -------------------------------------------------------------------------- |
||
150 | |||
151 | /** |
||
152 | * Sets custom body id |
||
153 | * @param string $id |
||
154 | * @return Page |
||
155 | */ |
||
156 | public function set_body_id($page_id="") |
||
161 | |||
162 | // -------------------------------------------------------------------------- |
||
163 | |||
164 | /** |
||
165 | * Sets custom page header |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function build_header() |
||
205 | |||
206 | // -------------------------------------------------------------------------- |
||
207 | |||
208 | /** |
||
209 | * Shortcut method to cut down on boilerplate |
||
210 | * |
||
211 | * @param string $view |
||
212 | * @param array|object $data |
||
213 | * @return void |
||
214 | */ |
||
215 | public function build($view, $data = array()) |
||
221 | |||
222 | // -------------------------------------------------------------------------- |
||
223 | |||
224 | /** |
||
225 | * Builds common footer with any additional js |
||
226 | */ |
||
227 | public function build_footer() |
||
236 | |||
237 | // -------------------------------------------------------------------------- |
||
238 | |||
239 | /** |
||
240 | * Script Tag |
||
241 | * |
||
242 | * Helper function for making script tags |
||
243 | * |
||
244 | * @param string $javascript |
||
245 | * @param bool $domain |
||
246 | * @return string |
||
247 | */ |
||
248 | private function script_tag($javascript, $domain=TRUE) |
||
262 | |||
263 | // -------------------------------------------------------------------------- |
||
264 | |||
265 | /** |
||
266 | * Num Queries |
||
267 | * |
||
268 | * Returns number of queries run on a page |
||
269 | * |
||
270 | * @return int |
||
271 | */ |
||
272 | public function num_queries() |
||
276 | |||
277 | // -------------------------------------------------------------------------- |
||
278 | |||
279 | /** |
||
280 | * Set Message |
||
281 | * |
||
282 | * Adds a message to the page |
||
283 | * @param string $type |
||
284 | * @param string $message |
||
285 | * @param bool $return |
||
286 | * @return mixed |
||
287 | */ |
||
288 | 2 | public function set_message($type, $message, $return = FALSE) |
|
296 | } |
||
297 | |||
299 |