Completed
Push — master ( 25342a...843744 )
by Nazar
04:25
created

MySQLi::tables()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 6
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 8
rs 9.2
1
<?php
2
/**
3
 * @package   CleverStyle CMS
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
class MySQLi extends _Abstract {
10
	/**
11
	 * @var \MySQLi Instance of DB connection
12
	 */
13
	protected $instance;
14
	/**
15
	 * @inheritdoc
16
	 */
17
	function __construct ($database, $user = '', $password = '', $host = 'localhost', $charset = 'utf8mb4', $prefix = '') {
18
		$start = microtime(true);
19
		/**
20
		 * Parsing of $host variable, detecting port and persistent connection
21
		 */
22
		list($host, $port) = $this->get_host_and_port($host);
23
		$this->instance = @new \MySQLi($host, $user, $password, $database, $port);
24
		if (!is_object($this->instance) || $this->instance->connect_errno) {
25
			return;
26
		}
27
		$this->database = $database;
28
		/**
29
		 * Changing DB charset
30
		 */
31
		if ($charset && $charset != $this->instance->character_set_name()) {
32
			$this->instance->set_charset($charset);
33
		}
34
		$this->connected       = true;
35
		$this->connecting_time = microtime(true) - $start;
36
		$this->db_type         = 'mysql';
37
		$this->prefix          = $prefix;
38
	}
39
	/**
40
	 * Parse host string into host and port separately
41
	 *
42
	 * Understands `p:` prefix for persistent connections
43
	 *
44
	 * @param string $host_string
45
	 *
46
	 * @return array
47
	 */
48
	protected function get_host_and_port ($host_string) {
49
		$host = explode(':', $host_string);
50
		$port = ini_get('mysqli.default_port') ?: 3306;
51
		switch (count($host)) {
52
			case 1:
53
				$host = $host[0];
54
				break;
55
			case 2:
56
				if ($host[0] == 'p') {
57
					$host = "$host[0]:$host[1]";
58
				} else {
59
					list($host, $port) = $host;
60
				}
61
				break;
62
			case 3:
63
				$port = $host[2];
64
				$host = "$host[0]:$host[1]";
65
		}
66
		return [$host, $port];
67
	}
68
	/**
69
	 * @inheritdoc
70
	 *
71
	 * @return false|\mysqli_result
72
	 */
73
	protected function q_internal ($query) {
74
		if (!$query) {
75
			return false;
76
		}
77
		$result_mode = $this->async && defined('MYSQLI_ASYNC') ? MYSQLI_ASYNC : MYSQLI_STORE_RESULT;
78
		$result      = @$this->instance->query($query, $result_mode);
79
		// In case of MySQL Client error - try to fix everything, but only once
80
		if (
81
			!$result &&
82
			$this->instance->errno >= 2000 &&
83
			$this->instance->ping()
84
		) {
85
			$result = @$this->instance->query($query, $result_mode);
86
		}
87
		return $result;
88
	}
89
	/**
90
	 * @inheritdoc
91
	 */
92
	protected function q_multi_internal ($query) {
93
		$query  = implode(';', $query);
94
		$return = @$this->instance->multi_query($query);
95
		/** @noinspection LoopWhichDoesNotLoopInspection */
96
		while ($this->instance->more_results() && $this->instance->next_result()) {
97
			$result = $this->instance->use_result();
98
			if (is_object($result)) {
99
				$result->free();
100
			}
101
		}
102
		return $return;
103
	}
104
	/**
105
	 * @deprecated
106
	 * @todo remove after 4.x release
107
	 *
108
	 * @inheritdoc
109
	 */
110
	function n ($query_result) {
111
		if (is_object($query_result)) {
112
			return $query_result->num_rows;
113
		} else {
114
			return false;
115
		}
116
	}
117
	/**
118
	 * @inheritdoc
119
	 *
120
	 * @param false|\mysqli_result $query_result
121
	 */
122
	function f ($query_result, $single_column = false, $array = false, $indexed = false) {
123
		if (!is_object($query_result)) {
124
			return false;
125
		}
126
		$result_type = $single_column || $indexed ? MYSQLI_NUM : MYSQLI_ASSOC;
127
		if ($array) {
128
			$result = [];
129
			while ($current = $query_result->fetch_array($result_type)) {
130
				$result[] = $single_column ? $current[0] : $current;
131
			}
132
			$this->free($query_result);
133
			return $result;
134
		}
135
		$result = $query_result->fetch_array($result_type);
136
		if ($result === null) {
137
			$result = false;
138
		}
139
		return $single_column && $result ? $result[0] : $result;
140
	}
141
	/**
142
	 * @inheritdoc
143
	 */
144
	function id () {
145
		return $this->instance->insert_id;
146
	}
147
	/**
148
	 * @inheritdoc
149
	 */
150
	function affected () {
151
		return $this->instance->affected_rows;
152
	}
153
	/**
154
	 * @inheritdoc
155
	 *
156
	 * @param false|\mysqli_result $query_result
157
	 */
158
	function free ($query_result) {
159
		if (is_object($query_result)) {
160
			$query_result->free();
161
		}
162
		return true;
163
	}
164
	/**
165
	 * @inheritdoc
166
	 */
167
	function columns ($table, $like = false) {
168
		if (!$table) {
169
			return false;
170
		}
171
		if ($like) {
172
			$like    = $this->s($like);
1 ignored issue
show
Documentation introduced by
$like is of type boolean, but the function expects a string|array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
173
			$columns = $this->qfas("SHOW COLUMNS FROM `$table` LIKE $like") ?: [];
174
		} else {
175
			$columns = $this->qfas("SHOW COLUMNS FROM `$table`") ?: [];
176
		}
177
		return $columns;
178
	}
179
	/**
180
	 * @inheritdoc
181
	 */
182
	function tables ($like = false) {
183
		if ($like) {
184
			$like = $this->s($like);
1 ignored issue
show
Documentation introduced by
$like is of type boolean, but the function expects a string|array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
185
			return $this->qfas("SHOW TABLES FROM `$this->database` LIKE $like") ?: [];
186
		} else {
187
			return $this->qfas("SHOW TABLES FROM `$this->database`") ?: [];
188
		}
189
	}
190
	/**
191
	 * @inheritdoc
192
	 */
193
	protected function s_internal ($string, $single_quotes_around) {
194
		$return = $this->instance->real_escape_string($string);
195
		return $single_quotes_around ? "'$return'" : $return;
196
	}
197
	/**
198
	 * @inheritdoc
199
	 */
200
	function server () {
201
		return $this->instance->server_info;
202
	}
203
	/**
204
	 * @inheritdoc
205
	 */
206
	function __destruct () {
207
		if ($this->connected && is_object($this->instance)) {
208
			$this->instance->close();
209
			$this->connected = false;
210
		}
211
	}
212
}
213