Completed
Push — master ( 238e1f...47c7ce )
by Nazar
05:15
created

MySQLi::f()   B

Complexity

Conditions 10
Paths 45

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 11.8232

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 14
c 1
b 1
f 0
nc 45
nop 4
dl 0
loc 19
ccs 14
cts 19
cp 0.7368
crap 11.8232
rs 7.2765

How to fix   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
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\DB;
9
use
10
	mysqli_result;
11
12
class MySQLi extends _Abstract {
13
	/**
14
	 * @var \MySQLi Instance of DB connection
15
	 */
16
	protected $instance;
17
	/**
18
	 * @inheritdoc
19
	 */
20 24
	public function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') {
21 24
		$start = microtime(true);
22
		/**
23
		 * Parsing of $host variable, detecting port and persistent connection
24
		 */
25 24
		list($host, $port) = $this->get_host_and_port($host);
26 24
		$this->instance = new \MySQLi($host, $user, $password, $database, $port);
27 24
		if (!is_object($this->instance) || $this->instance->connect_errno) {
28 1
			return;
29
		}
30 24
		$this->database = $database;
31
		/**
32
		 * Changing DB charset if necessary
33
		 */
34 24
		if ($this->instance->character_set_name() != 'utf8mb4') {
35 24
			$this->instance->set_charset('utf8mb4');
36
		}
37 24
		$this->connected       = true;
38 24
		$this->connecting_time = microtime(true) - $start;
39 24
		$this->db_type         = 'mysql';
40 24
		$this->prefix          = $prefix;
41 24
	}
42
	/**
43
	 * Parse host string into host and port separately
44
	 *
45
	 * Understands `p:` prefix for persistent connections
46
	 *
47
	 * @param string $host_string
48
	 *
49
	 * @return array
50
	 */
51 24
	protected function get_host_and_port ($host_string) {
52 24
		$host = explode(':', $host_string);
53 24
		$port = ini_get('mysqli.default_port') ?: 3306;
54 24
		switch (count($host)) {
55 24
			case 1:
56 24
				$host = $host[0];
57 24
				break;
58 1
			case 2:
59 1
				if ($host[0] == 'p') {
60 1
					$host = "$host[0]:$host[1]";
61
				} else {
62 1
					list($host, $port) = $host;
63
				}
64 1
				break;
65 1
			case 3:
66 1
				$port = $host[2];
67 1
				$host = "$host[0]:$host[1]";
68
		}
69 24
		return [$host, $port];
70
	}
71
	/**
72
	 * @inheritdoc
73
	 *
74
	 * @return bool|mysqli_result
75
	 */
76 24
	protected function q_internal ($query, $parameters = []) {
77 24
		if (!$query) {
78 1
			return false;
79
		}
80 24
		$result = $this->q_internal_internal($query, $parameters);
81
		/*
82
		 * In case of MySQL Client error try once again
83
		 */
84
		if (
85 24
			!$result &&
86 24
			$this->instance->errno >= 2000 &&
87 24
			$this->instance->ping()
88
		) {
89
			$result = $this->q_internal_internal($query, $parameters);
90
		}
91 24
		return $result;
92
	}
93
	/**
94
	 * @param string $query
95
	 * @param array  $parameters
96
	 *
97
	 * @return bool|mysqli_result
98
	 */
99 24
	protected function q_internal_internal ($query, $parameters) {
100 24
		if (!$parameters) {
101 24
			return $this->instance->query($query);
102
		}
103 21
		$stmt = $this->instance->prepare($query);
104
		// Allows to provide more parameters for prepared statements than needed
105 21
		$local_parameters = array_slice($parameters, 0, substr_count($query, '?'));
106 21
		$stmt->bind_param(
107 21
			str_repeat('s', count($local_parameters)),
108 21
			...$local_parameters
109
		);
110 21
		$result = $stmt->execute();
111
		/**
112
		 * Return result only for SELECT queries, boolean otherwise
113
		 */
114 21
		return $stmt->get_result() ?: $result;
115
	}
116
	/**
117
	 * @inheritdoc
118
	 *
119
	 * @param false|mysqli_result $query_result
120
	 */
121 23
	public function f ($query_result, $single_column = false, $array = false, $indexed = false) {
122 23
		if (!($query_result instanceof mysqli_result)) {
123 1
			return false;
124
		}
125 23
		$result_type = $single_column || $indexed ? MYSQLI_NUM : MYSQLI_ASSOC;
126 23
		if ($array) {
127 20
			$result = [];
128 20
			while ($current = $query_result->fetch_array($result_type)) {
129 20
				$result[] = $single_column ? $current[0] : $current;
130
			}
131 20
			$this->free($query_result);
132 20
			return $result;
133
		}
134 22
		$result = $query_result->fetch_array($result_type);
135 22
		if ($result === null) {
136 16
			$result = false;
137
		}
138 22
		return $single_column && $result ? $result[0] : $result;
139
	}
140
	/**
141
	 * @inheritdoc
142
	 */
143 17
	public function id () {
144 17
		return $this->instance->insert_id;
145
	}
146
	/**
147
	 * @inheritdoc
148
	 */
149 1
	public function affected () {
150 1
		return $this->instance->affected_rows;
151
	}
152
	/**
153
	 * @inheritdoc
154
	 *
155
	 * @param false|mysqli_result $query_result
156
	 */
157 20
	public function free ($query_result) {
158 20
		if ($query_result instanceof mysqli_result) {
159 20
			$query_result->free();
160
		}
161 20
		return true;
162
	}
163
	/**
164
	 * @inheritdoc
165
	 */
166 2
	public function columns ($table, $like = false) {
167 2
		if (!$table) {
168 1
			return false;
169
		}
170 2
		if ($like) {
171 1
			$like    = $this->s($like);
172 1
			$columns = $this->qfas("SHOW COLUMNS FROM `$table` LIKE $like") ?: [];
173
		} else {
174 2
			$columns = $this->qfas("SHOW COLUMNS FROM `$table`") ?: [];
175
		}
176 2
		return $columns;
177
	}
178
	/**
179
	 * @inheritdoc
180
	 */
181 1
	public function tables ($like = false) {
182 1
		if ($like) {
183 1
			$like = $this->s($like);
184 1
			return $this->qfas("SHOW TABLES FROM `$this->database` LIKE $like") ?: [];
185
		} else {
186 1
			return $this->qfas("SHOW TABLES FROM `$this->database`") ?: [];
187
		}
188
	}
189
	/**
190
	 * @inheritdoc
191
	 */
192 22
	protected function s_internal ($string, $single_quotes_around) {
193 22
		$return = $this->instance->real_escape_string($string);
194 22
		return $single_quotes_around ? "'$return'" : $return;
195
	}
196
	/**
197
	 * @inheritdoc
198
	 */
199 1
	public function server () {
200 1
		return $this->instance->server_info;
201
	}
202
	/**
203
	 * @inheritdoc
204
	 */
205 2
	public function __destruct () {
206 2
		if ($this->connected && is_object($this->instance)) {
207 2
			$this->instance->close();
208 2
			$this->connected = false;
209
		}
210 2
	}
211
}
212