GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CI_DB_pdo_mysql_driver::db_connect()   F
last analyzed

Complexity

Conditions 22
Paths 8190

Size

Total Lines 79
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 22
eloc 33
nc 8190
nop 1
dl 0
loc 79
rs 2.2578
c 2
b 0
f 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 53 and the first side effect is on line 38.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
9
 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
32
 * @copyright	Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
33
 * @license	http://opensource.org/licenses/MIT	MIT License
34
 * @link	http://codeigniter.com
35
 * @since	Version 3.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
40
/**
41
 * PDO MySQL Database Adapter Class
42
 *
43
 * Note: _DB is an extender class that the app controller
44
 * creates dynamically based on whether the query builder
45
 * class is being used or not.
46
 *
47
 * @package		CodeIgniter
48
 * @subpackage	Drivers
49
 * @category	Database
50
 * @author		EllisLab Dev Team
51
 * @link		http://codeigniter.com/user_guide/database/
52
 */
53
class CI_DB_pdo_mysql_driver extends CI_DB_pdo_driver {
54
55
	/**
56
	 * Sub-driver
57
	 *
58
	 * @var	string
59
	 */
60
	public $subdriver = 'mysql';
61
62
	/**
63
	 * Compression flag
64
	 *
65
	 * @var	bool
66
	 */
67
	public $compress = FALSE;
68
69
	/**
70
	 * Strict ON flag
71
	 *
72
	 * Whether we're running in strict SQL mode.
73
	 *
74
	 * @var	bool
75
	 */
76
	public $stricton;
77
78
	// --------------------------------------------------------------------
79
80
	/**
81
	 * Identifier escape character
82
	 *
83
	 * @var	string
84
	 */
85
	protected $_escape_char = '`';
86
87
	// --------------------------------------------------------------------
88
89
	/**
90
	 * Class constructor
91
	 *
92
	 * Builds the DSN if not already set.
93
	 *
94
	 * @param	array	$params
95
	 * @return	void
96
	 */
97 View Code Duplication
	public function __construct($params)
98
	{
99
		parent::__construct($params);
100
101
		if (empty($this->dsn))
102
		{
103
			$this->dsn = 'mysql:host='.(empty($this->hostname) ? '127.0.0.1' : $this->hostname);
104
105
			empty($this->port) OR $this->dsn .= ';port='.$this->port;
106
			empty($this->database) OR $this->dsn .= ';dbname='.$this->database;
107
			empty($this->char_set) OR $this->dsn .= ';charset='.$this->char_set;
108
		}
109
		elseif ( ! empty($this->char_set) && strpos($this->dsn, 'charset=', 6) === FALSE && is_php('5.3.6'))
110
		{
111
			$this->dsn .= ';charset='.$this->char_set;
112
		}
113
	}
114
115
	// --------------------------------------------------------------------
116
117
	/**
118
	 * Database connection
119
	 *
120
	 * @param	bool	$persistent
121
	 * @return	object
122
	 */
123
	public function db_connect($persistent = FALSE)
124
	{
125
		/* Prior to PHP 5.3.6, even if the charset was supplied in the DSN
126
		 * on connect - it was ignored. This is a work-around for the issue.
127
		 *
128
		 * Reference: http://www.php.net/manual/en/ref.pdo-mysql.connection.php
129
		 */
130
		if ( ! is_php('5.3.6') && ! empty($this->char_set))
131
		{
132
			$this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES '.$this->char_set
133
				.(empty($this->dbcollat) ? '' : ' COLLATE '.$this->dbcollat);
134
		}
135
136
		if (isset($this->stricton))
137
		{
138
			if ($this->stricton)
139
			{
140
				$sql = 'CONCAT(@@sql_mode, ",", "STRICT_ALL_TABLES")';
141
			}
142
			else
143
			{
144
				$sql = 'REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
145
                                        @@sql_mode,
146
                                        "STRICT_ALL_TABLES,", ""),
147
                                        ",STRICT_ALL_TABLES", ""),
148
                                        "STRICT_ALL_TABLES", ""),
149
                                        "STRICT_TRANS_TABLES,", ""),
150
                                        ",STRICT_TRANS_TABLES", ""),
151
                                        "STRICT_TRANS_TABLES", "")';
152
			}
153
154
			if ( ! empty($sql))
155
			{
156
				if (empty($this->options[PDO::MYSQL_ATTR_INIT_COMMAND]))
157
				{
158
					$this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET SESSION sql_mode = '.$sql;
159
				}
160
				else
161
				{
162
					$this->options[PDO::MYSQL_ATTR_INIT_COMMAND] .= ', @@session.sql_mode = '.$sql;
163
				}
164
			}
165
		}
166
167
		if ($this->compress === TRUE)
168
		{
169
			$this->options[PDO::MYSQL_ATTR_COMPRESS] = TRUE;
170
		}
171
172
		// SSL support was added to PDO_MYSQL in PHP 5.3.7
173
		if (is_array($this->encrypt) && is_php('5.3.7'))
174
		{
175
			$ssl = array();
176
			empty($this->encrypt['ssl_key'])    OR $ssl[PDO::MYSQL_ATTR_SSL_KEY]    = $this->encrypt['ssl_key'];
177
			empty($this->encrypt['ssl_cert'])   OR $ssl[PDO::MYSQL_ATTR_SSL_CERT]   = $this->encrypt['ssl_cert'];
178
			empty($this->encrypt['ssl_ca'])     OR $ssl[PDO::MYSQL_ATTR_SSL_CA]     = $this->encrypt['ssl_ca'];
179
			empty($this->encrypt['ssl_capath']) OR $ssl[PDO::MYSQL_ATTR_SSL_CAPATH] = $this->encrypt['ssl_capath'];
180
			empty($this->encrypt['ssl_cipher']) OR $ssl[PDO::MYSQL_ATTR_SSL_CIPHER] = $this->encrypt['ssl_cipher'];
181
182
			// DO NOT use array_merge() here!
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
183
			// It re-indexes numeric keys and the PDO_MYSQL_ATTR_SSL_* constants are integers.
184
			empty($ssl) OR $this->options += $ssl;
185
		}
186
187
		// Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails
188
		if (
189
			($pdo = parent::db_connect($persistent)) !== FALSE
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression parent::db_connect($persistent); of type PDO|false adds false to the return on line 200 which is incompatible with the return type documented by CI_DB_pdo_mysql_driver::db_connect of type object. It seems like you forgot to handle an error condition.
Loading history...
190
			&& ! empty($ssl)
191
			&& version_compare($pdo->getAttribute(PDO::ATTR_CLIENT_VERSION), '5.7.3', '<=')
192
			&& empty($pdo->query("SHOW STATUS LIKE 'ssl_cipher'")->fetchObject()->Value)
193
		)
194
		{
195
			$message = 'PDO_MYSQL was configured for an SSL connection, but got an unencrypted connection instead!';
196
			log_message('error', $message);
197
			return ($this->db->db_debug) ? $this->db->display_error($message, '', TRUE) : FALSE;
198
		}
199
200
		return $pdo;
201
	}
202
203
	// --------------------------------------------------------------------
204
205
	/**
206
	 * Select the database
207
	 *
208
	 * @param	string	$database
209
	 * @return	bool
210
	 */
211 View Code Duplication
	public function db_select($database = '')
212
	{
213
		if ($database === '')
214
		{
215
			$database = $this->database;
216
		}
217
218
		if (FALSE !== $this->simple_query('USE '.$this->escape_identifiers($database)))
219
		{
220
			$this->database = $database;
221
			return TRUE;
222
		}
223
224
		return FALSE;
225
	}
226
227
	// --------------------------------------------------------------------
228
229
	/**
230
	 * Show table query
231
	 *
232
	 * Generates a platform-specific query string so that the table names can be fetched
233
	 *
234
	 * @param	bool	$prefix_limit
235
	 * @return	string
236
	 */
237
	protected function _list_tables($prefix_limit = FALSE)
238
	{
239
		$sql = 'SHOW TABLES';
240
241
		if ($prefix_limit === TRUE && $this->dbprefix !== '')
242
		{
243
			return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
244
		}
245
246
		return $sql;
247
	}
248
249
	// --------------------------------------------------------------------
250
251
	/**
252
	 * Show column query
253
	 *
254
	 * Generates a platform-specific query string so that the column names can be fetched
255
	 *
256
	 * @param	string	$table
257
	 * @return	string
258
	 */
259
	protected function _list_columns($table = '')
260
	{
261
		return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
262
	}
263
264
	// --------------------------------------------------------------------
265
266
	/**
267
	 * Returns an object with field data
268
	 *
269
	 * @param	string	$table
270
	 * @return	array
271
	 */
272 View Code Duplication
	public function field_data($table)
273
	{
274
		if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
275
		{
276
			return FALSE;
277
		}
278
		$query = $query->result_object();
279
280
		$retval = array();
281
		for ($i = 0, $c = count($query); $i < $c; $i++)
282
		{
283
			$retval[$i]			= new stdClass();
284
			$retval[$i]->name		= $query[$i]->Field;
285
286
			sscanf($query[$i]->Type, '%[a-z](%d)',
287
				$retval[$i]->type,
288
				$retval[$i]->max_length
289
			);
290
291
			$retval[$i]->default		= $query[$i]->Default;
292
			$retval[$i]->primary_key	= (int) ($query[$i]->Key === 'PRI');
293
		}
294
295
		return $retval;
296
	}
297
298
	// --------------------------------------------------------------------
299
300
	/**
301
	 * Truncate statement
302
	 *
303
	 * Generates a platform-specific truncate string from the supplied data
304
	 *
305
	 * If the database does not support the TRUNCATE statement,
306
	 * then this method maps to 'DELETE FROM table'
307
	 *
308
	 * @param	string	$table
309
	 * @return	string
310
	 */
311
	protected function _truncate($table)
312
	{
313
		return 'TRUNCATE '.$table;
314
	}
315
316
	// --------------------------------------------------------------------
317
318
	/**
319
	 * FROM tables
320
	 *
321
	 * Groups tables in FROM clauses if needed, so there is no confusion
322
	 * about operator precedence.
323
	 *
324
	 * @return	string
325
	 */
326 View Code Duplication
	protected function _from_tables()
327
	{
328
		if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
329
		{
330
			return '('.implode(', ', $this->qb_from).')';
331
		}
332
333
		return implode(', ', $this->qb_from);
334
	}
335
336
}
337