Page   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 288
Duplicated Lines 4.86 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 20%

Importance

Changes 0
Metric Value
dl 14
loc 288
rs 10
c 0
b 0
f 0
ccs 18
cts 90
cp 0.2
wmc 23
lcom 1
cbo 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A _headers() 0 18 1
A set_meta() 0 5 1
A set_head_js_group() 7 7 2
A set_css_group() 0 11 1
A set_foot_js_group() 7 7 2
A set_title() 0 9 2
A set_body_id() 0 5 1
B build_header() 0 37 4
A build() 0 6 1
A build_footer() 0 9 2
A script_tag() 0 14 2
A num_queries() 0 4 2
A set_message() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

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');
2
/**
3
 * Class for building pages
4
 * @author Timothy J. Warren <[email protected]>
5
 *
6
 * All methods are chainable, with the exception of the constructor,
7
 * build_header(), build_footer(), and _headers() methods.
8
 */
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()
25
	{
26 2
		$this->meta = "";
27 2
		$this->head_js = "";
28 2
		$this->foot_js = "";
29 2
		$this->css = "";
30 2
		$this->title = "";
31 2
		$this->head_tags = "";
32 2
		$this->body_id = "";
33 2
		$this->CI =& get_instance();
34 2
	}
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()
48
	{
49
		$this->CI->output->set_header("Cache-Control: must-revalidate, public");
50
		$this->CI->output->set_header("Vary: Accept");
51
52
		//Predefine charset and mime
53
		$charset = "UTF-8";
54
		$mime = "text/html";
55
56
		$doctype_string = doctype('html5') . "\n<html lang='en'>";
57
58
		// finally, output the mime type and prolog type
59
		$this->CI->output->set_header("Content-Type: $mime;charset=$charset");
60
		$this->CI->output->set_header("X-UA-Compatible: chrome=1");
61
		$this->CI->output->set_output($doctype_string);
62
63
		return $this;
64
	}
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)
77
	{
78 1
		$this->meta .= T1.meta($meta).NL;
79 1
		return $this;
80
	}
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)
91
	{
92
		$file = $this->CI->config->item('group_style_path') . $group;
93
		$file .= ($debug == TRUE) ? "/debug/1" : "";
94
		$this->head_js .= $this->script_tag($file, FALSE);
95
		return $this;
96
	}
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)
106
	{
107
		$link = array(
108
		  'href' => $this->CI->config->item('group_style_path') . $group,
109
		  'rel' => 'stylesheet',
110
		  'type' => 'text/css',
111
		);
112
		$this->css .= T1.link_tag($link).NL;
113
114
		return $this;
115
	}
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)
125
	{
126
		$file = $this->CI->config->item('group_style_path') . $group;
127
		$file .= ($debug == TRUE) ? "/debug/1" : "";
128
		$this->foot_js .= $this->script_tag($file, FALSE);
129
		return $this;
130
	}
131
132
	// --------------------------------------------------------------------------
133
134
	/**
135
	 * Sets html title string
136
	 * @param string $title
137
	 * @return Page
138
	 */
139
	public function set_title($title="")
140
	{
141
		$title = ($title == "") ?
142
			$this->CI->config->item('default_title') : $title;
143
144
		$this->title = $title;
145
146
		return $this;
147
	}
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="")
157
	{
158
		$this->body_id = $page_id;
159
		return $this;
160
	}
161
162
	// --------------------------------------------------------------------------
163
164
	/**
165
	 * Sets custom page header
166
	 * @return $this
167
	 */
168
	public function build_header()
169
	{
170
		$data = array();
171
172
		//Set Meta Tags
173
		$this->meta = T1.'<meta charset="utf-8" />'.NL. $this->meta;
174
		$data['meta'] = $this->meta;
175
176
		//Set CSS
177
		if ($this->css != "")
178
		{
179
			$data['css'] = $this->css;
180
		}
181
		else
182
		{
183
			//Set default CSS group
184
			$this->set_css_group($this->CI->config->item('default_css_group'));
185
			$data['css'] = $this->css;
186
		}
187
188
		//Set head javascript
189
		$data['head_js'] = ( ! empty($this->head_js)) ?  $this->head_js : "";
190
191
		//Set Page Title
192
		$data['title'] = ( ! empty($this->title)) ? $this->title : $this->CI->config->item('default_title');
193
194
		//Set Body Id
195
		$data['body_id'] = $this->body_id;
196
197
		//Set Server Headers and Doctype
198
		$this->_headers();
199
200
		//Output Header
201
		$this->CI->load->view('header', $data);
202
203
		return $this;
204
	}
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())
216
	{
217
		$this->build_header();
218
		$this->CI->load->view($view, $data);
219
		$this->build_footer();
220
	}
221
222
	// --------------------------------------------------------------------------
223
224
	/**
225
	 * Builds common footer with any additional js
226
	 */
227
	public function build_footer()
228
	{
229
		$data = array();
230
231
		$data['foot_js'] = ($this->foot_js != "") ?
232
			$this->foot_js : '';
233
234
		$this->CI->load->view('footer', $data);
235
	}
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)
249
	{
250
		$path = $this->CI->config->item('content_domain');
251
		$js_file = $path . "/js/" . $javascript . ".js";
252
253
		if ($domain == FALSE)
254
			$js_file = $javascript;
255
256
		$tag = '<script src="' .
257
			$js_file .
258
			'"></script>'.NL;
259
260
		return $tag;
261
	}
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()
273
	{
274
		return	(isset($this->CI->db)) ? count($this->CI->db->queries) : 0;
275
	}
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)
289
	{
290 2
		$data = array();
291 2
		$data['stat_type'] = $type;
292 2
		$data['message'] = $message;
293
294 2
		return $this->CI->load->view('message', $data, $return);
295
	}
296
}
297
298
299