Completed
Push — master ( dabf55...764bc6 )
by Nazar
14:29
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 22
	public function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') {
21 22
		$start = microtime(true);
22
		/**
23
		 * Parsing of $host variable, detecting port and persistent connection
24
		 */
25 22
		list($host, $port) = $this->get_host_and_port($host);
26 22
		$this->instance = new \MySQLi($host, $user, $password, $database, $port);
27 22
		if (!is_object($this->instance) || $this->instance->connect_errno) {
28 1
			return;
29
		}
30 22
		$this->database = $database;
31
		/**
32
		 * Changing DB charset if necessary
33
		 */
34 22
		if ($this->instance->character_set_name() != 'utf8mb4') {
35 22
			$this->instance->set_charset('utf8mb4');
36
		}
37 22
		$this->connected       = true;
38 22
		$this->connecting_time = microtime(true) - $start;
39 22
		$this->db_type         = 'mysql';
40 22
		$this->prefix          = $prefix;
41 22
	}
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 22
	protected function get_host_and_port ($host_string) {
52 22
		$host = explode(':', $host_string);
53 22
		$port = ini_get('mysqli.default_port') ?: 3306;
54 22
		switch (count($host)) {
55 22
			case 1:
56 22
				$host = $host[0];
57 22
				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 22
		return [$host, $port];
70
	}
71
	/**
72
	 * @inheritdoc
73
	 *
74
	 * @return false|mysqli_result
75
	 */
76 22
	protected function q_internal ($query, $parameters = []) {
77 22
		if (!$query) {
78 1
			return false;
79
		}
80 22
		for ($i = 0; $i < 2; ++$i) {
81 22
			if ($parameters) {
82 1
				$stmt = $this->instance->prepare($query);
83 1
				$stmt->bind_param(
84 1
					str_repeat('s', count($parameters)),
85 1
					...$parameters
86
				);
87 1
				$stmt->execute();
88 1
				$result = $stmt->get_result();
89
			} else {
90 22
				$result = $this->instance->query($query);
91
			}
92
			// In case of MySQL Client error - try to fix everything, but only once
93
			if (
94 22
				$result ||
95
				$this->instance->errno < 2000 ||
96 22
				!$this->instance->ping()
97
			) {
98 22
				break;
99
			}
100
		}
101
		/** @noinspection PhpUndefinedVariableInspection */
102 22
		return $result;
103
	}
104
	/**
105
	 * @inheritdoc
106
	 *
107
	 * @param false|mysqli_result $query_result
108
	 */
109 21
	public function f ($query_result, $single_column = false, $array = false, $indexed = false) {
110 21
		if (!($query_result instanceof mysqli_result)) {
111 1
			return false;
112
		}
113 21
		$result_type = $single_column || $indexed ? MYSQLI_NUM : MYSQLI_ASSOC;
114 21
		if ($array) {
115 18
			$result = [];
116 18
			while ($current = $query_result->fetch_array($result_type)) {
117 18
				$result[] = $single_column ? $current[0] : $current;
118
			}
119 18
			$this->free($query_result);
120 18
			return $result;
121
		}
122 20
		$result = $query_result->fetch_array($result_type);
123 20
		if ($result === null) {
124 15
			$result = false;
125
		}
126 20
		return $single_column && $result ? $result[0] : $result;
127
	}
128
	/**
129
	 * @inheritdoc
130
	 */
131 15
	public function id () {
132 15
		return $this->instance->insert_id;
133
	}
134
	/**
135
	 * @inheritdoc
136
	 */
137 1
	public function affected () {
138 1
		return $this->instance->affected_rows;
139
	}
140
	/**
141
	 * @inheritdoc
142
	 *
143
	 * @param false|mysqli_result $query_result
144
	 */
145 18
	public function free ($query_result) {
146 18
		if ($query_result instanceof mysqli_result) {
147 18
			$query_result->free();
148
		}
149 18
		return true;
150
	}
151
	/**
152
	 * @inheritdoc
153
	 */
154 2
	public function columns ($table, $like = false) {
155 2
		if (!$table) {
156 1
			return false;
157
		}
158 2
		if ($like) {
159 1
			$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...
160 1
			$columns = $this->qfas("SHOW COLUMNS FROM `$table` LIKE $like") ?: [];
161
		} else {
162 2
			$columns = $this->qfas("SHOW COLUMNS FROM `$table`") ?: [];
163
		}
164 2
		return $columns;
165
	}
166
	/**
167
	 * @inheritdoc
168
	 */
169 1
	public function tables ($like = false) {
170 1
		if ($like) {
171 1
			$like = $this->s($like);
172 1
			return $this->qfas("SHOW TABLES FROM `$this->database` LIKE $like") ?: [];
173
		} else {
174 1
			return $this->qfas("SHOW TABLES FROM `$this->database`") ?: [];
175
		}
176
	}
177
	/**
178
	 * @inheritdoc
179
	 */
180 22
	protected function s_internal ($string, $single_quotes_around) {
181 22
		$return = $this->instance->real_escape_string($string);
182 22
		return $single_quotes_around ? "'$return'" : $return;
183
	}
184
	/**
185
	 * @inheritdoc
186
	 */
187 1
	public function server () {
188 1
		return $this->instance->server_info;
189
	}
190
	/**
191
	 * @inheritdoc
192
	 */
193 2
	public function __destruct () {
194 2
		if ($this->connected && is_object($this->instance)) {
195 2
			$this->instance->close();
196 2
			$this->connected = false;
197
		}
198 2
	}
199
}
200