Passed
Push — master ( 3bf766...1a0132 )
by Joe
06:03
created

Db   F

Complexity

Total Complexity 78

Size/Duplication

Total Lines 448
Duplicated Lines 0 %

Test Coverage

Coverage 84.48%

Importance

Changes 15
Bugs 3 Features 0
Metric Value
eloc 157
dl 0
loc 448
ccs 147
cts 174
cp 0.8448
rs 2.16
c 15
b 3
f 0
wmc 78

23 Methods

Rating   Name   Duplication   Size   Complexity  
A free() 0 6 2
A queryReturn() 0 14 4
A useDb() 0 3 1
A real_escape() 0 6 4
A selectDb() 0 4 1
A qr() 0 3 1
A prepare() 0 8 2
B connect() 0 35 9
A disconnect() 0 5 2
A transactionCommit() 0 6 3
A getLastInsertId() 0 7 5
A seek() 0 14 2
A fetchObject() 0 4 1
A unlock() 0 10 3
A lock() 0 22 6
A next_record() 0 17 5
C query() 0 52 16
A transactionAbort() 0 6 3
A num_rows() 0 3 1
A affectedRows() 0 3 1
A transactionBegin() 0 9 3
A tableNames() 0 12 2
A num_fields() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Db often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Db, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * MySQL Related Functionality
4
 * @author Joe Huss <[email protected]>
5
 * @copyright 2019
6
 * @package MyAdmin
7
 * @category SQL
8
 */
9
10
namespace MyDb\Mysqli;
11
12
use \MyDb\Generic;
13
use \MyDb\Db_Interface;
14
15
/**
16
 * Db
17
 *
18
 * @access public
19
 */
20
class Db extends Generic implements Db_Interface
21
{
22
	/**
23
	 * @var string
24
	 */
25
	public $type = 'mysqli';
26
27
	/**
28
	 * alias function of select_db, changes the database we are working with.
29
	 *
30
	 * @param string $database the name of the database to use
31
	 * @return void
32
	 */
33 1
	public function useDb($database)
34
	{
35 1
		$this->selectDb($database);
36
	}
37
38
	/**
39
	 * changes the database we are working with.
40
	 *
41
	 * @param string $database the name of the database to use
42
	 * @return void
43
	 */
44 1
	public function selectDb($database)
45
	{
46 1
		$this->connect();
47 1
		mysqli_select_db($this->linkId, $database);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_select_db() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
		mysqli_select_db(/** @scrutinizer ignore-type */ $this->linkId, $database);
Loading history...
48
	}
49
50
	/* public: connection management */
51
52
	/**
53
	 * Db::connect()
54
	 * @param string $database
55
	 * @param string $host
56
	 * @param string $user
57
	 * @param string $password
58
	 * @return int|\mysqli
59
	 */
60 26
	public function connect($database = '', $host = '', $user = '', $password = '')
61
	{
62
		/* Handle defaults */
63 26
		if ($database == '') {
64 26
			$database = $this->database;
65
		}
66 26
		if ($host == '') {
67 26
			$host = $this->host;
68
		}
69 26
		if ($user == '') {
70 26
			$user = $this->user;
71
		}
72 26
		if ($password == '') {
73 26
			$password = $this->password;
74
		}
75
		/* establish connection, select database */
76 26
		if (!is_object($this->linkId)) {
77 26
			$this->connectionAttempt++;
78 26
			if ($this->connectionAttempt > 1) {
79 6
				error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}");
80
			}
81 26
			if ($this->connectionAttempt >= $this->maxConnectErrors) {
82
				$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mysqli seems to be never defined.
Loading history...
83
				return 0;
84
			}
85 26
			$this->linkId = mysqli_init();
86 26
			$this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}");
87 26
			$this->linkId->real_connect($host, $user, $password, $database);
88 26
			$this->linkId->set_charset($this->characterSet);
89 26
			if ($this->linkId->connect_errno) {
90
				$this->halt("connect($host, $user, \$password) failed. ".$mysqli->connect_error);
91
				return 0;
92
			}
93
		}
94 26
		return $this->linkId;
95
	}
96
97
	/**
98
	 * Db::disconnect()
99
	 * @return bool
100
	 */
101 1
	public function disconnect()
102
	{
103 1
		$return = method_exists($this->linkId, 'close') ? $this->linkId->close() : false;
104 1
		$this->linkId = 0;
105 1
		return $return;
106
	}
107
108
	/**
109
	 * @param $string
110
	 * @return string
111
	 */
112 2
	public function real_escape($string = '')
113
	{
114 2
		if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) {
0 ignored issues
show
introduced by
The condition is_resource($this->linkId) is always false.
Loading history...
115
			return $this->escape($string);
116
		}
117 2
		return mysqli_real_escape_string($this->linkId, $string);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer and resource; however, parameter $link of mysqli_real_escape_string() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
		return mysqli_real_escape_string(/** @scrutinizer ignore-type */ $this->linkId, $string);
Loading history...
118
	}
119
120
	/**
121
	 * discard the query result
122
	 * @return void
123
	 */
124 1
	public function free()
125
	{
126 1
		if (is_resource($this->queryId)) {
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
127
			@mysqli_free_result($this->queryId);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mysqli_free_result(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

127
			/** @scrutinizer ignore-unhandled */ @mysqli_free_result($this->queryId);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
128
		}
129 1
		$this->queryId = 0;
130
	}
131
132
	/**
133
	 * Db::queryReturn()
134
	 *
135
	 * Sends an SQL query to the server like the normal query() command but iterates through
136
	 * any rows and returns the row or rows immediately or FALSE on error
137
	 *
138
	 * @param mixed $query SQL Query to be used
139
	 * @param string $line optionally pass __LINE__ calling the query for logging
140
	 * @param string $file optionally pass __FILE__ calling the query for logging
141
	 * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only
142
	 */
143 1
	public function queryReturn($query, $line = '', $file = '')
144
	{
145 1
		$this->query($query, $line, $file);
146 1
		if ($this->num_rows() == 0) {
147 1
			return false;
148 1
		} elseif ($this->num_rows() == 1) {
149 1
			$this->next_record(MYSQLI_ASSOC);
150 1
			return $this->Record;
151
		} else {
152 1
			$out = [];
153 1
			while ($this->next_record(MYSQLI_ASSOC)) {
154 1
				$out[] = $this->Record;
155
			}
156 1
			return $out;
157
		}
158
	}
159
160
	/**
161
	 * db:qr()
162
	 *
163
	 *  alias of queryReturn()
164
	 *
165
	 * @param mixed $query SQL Query to be used
166
	 * @param string $line optionally pass __LINE__ calling the query for logging
167
	 * @param string $file optionally pass __FILE__ calling the query for logging
168
	 * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only
169
	 */
170 1
	public function qr($query, $line = '', $file = '')
171
	{
172 1
		return $this->queryReturn($query, $line, $file);
173
	}
174
175
	/**
176
	 * creates a prepaired statement from query
177
	 *
178
	 * @param string $query sql query like INSERT INTO table (col) VALUES (?)  or  SELECT * from table WHERE col1 = ? and col2 = ?  or  UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ?
179
	 * @return int|\MyDb\Mysqli\mysqli_stmt
0 ignored issues
show
Bug introduced by
The type MyDb\Mysqli\mysqli_stmt was not found. Did you mean mysqli_stmt? If so, make sure to prefix the type with \.
Loading history...
180
	 */
181 1
	public function prepare($query)
182
	{
183 1
		if (!$this->connect()) {
184
			return 0;
185
		}
186 1
		$haltPrev = $this->haltOnError;
0 ignored issues
show
Unused Code introduced by
The assignment to $haltPrev is dead and can be removed.
Loading history...
187 1
		$this->haltOnError = 'no';
188 1
		return mysqli_prepare($this->linkId, $query);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_prepare() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

188
		return mysqli_prepare(/** @scrutinizer ignore-type */ $this->linkId, $query);
Loading history...
189
	}
190
191
	/**
192
	 * Db::query()
193
	 *
194
	 *  Sends an SQL query to the database
195
	 *
196
	 * @param mixed $queryString
197
	 * @param string $line
198
	 * @param string $file
199
	 * @return mixed 0 if no query or query id handler, safe to ignore this return
200
	 */
201 10
	public function query($queryString, $line = '', $file = '')
202
	{
203
		/* No empty queries, please, since PHP4 chokes on them. */
204
		/* The empty query string is passed on from the constructor,
205
		* when calling the class without a query, e.g. in situations
206
		* like these: '$db = new db_Subclass;'
207
		*/
208 10
		if ($queryString == '') {
209 1
			return 0;
210
		}
211 10
		if (!$this->connect()) {
212
			return 0;
213
			/* we already complained in connect() about that. */
214
		}
215 10
		$haltPrev = $this->haltOnError;
216 10
		$this->haltOnError = 'no';
217
		// New query, discard previous result.
218 10
		if (is_resource($this->queryId)) {
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
219
			$this->free();
220
		}
221 10
		if ($this->Debug) {
222 1
			printf("Debug: query = %s<br>\n", $queryString);
223
		}
224 10
		if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) {
225
			$this->log($queryString, $line, $file);
226
		}
227 10
		$tries = 3;
228 10
		$try = 0;
229 10
		$this->queryId = false;
230 10
		while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) {
231 10
			$try++;
232 10
			if ($try > 1) {
233
				@mysqli_close($this->linkId);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mysqli_close(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

233
				/** @scrutinizer ignore-unhandled */ @mysqli_close($this->linkId);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_close() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

233
				@mysqli_close(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
234
				$this->connect();
235
			}
236 10
			$start = microtime(true);
237 10
			$this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_query() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

237
			$this->queryId = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, $queryString, MYSQLI_STORE_RESULT);
Loading history...
238 10
			$this->addLog($queryString, microtime(true) - $start, $line, $file);
239 10
			$this->Row = 0;
240 10
			$this->Errno = @mysqli_errno($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_errno() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

240
			$this->Errno = @mysqli_errno(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
241 10
			$this->Error = @mysqli_error($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_error() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

241
			$this->Error = @mysqli_error(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
242 10
			if ($try == 1 && (null === $this->queryId || $this->queryId === false)) {
243
				$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file);
244
			}
245
		}
246 10
		$this->haltOnError = $haltPrev;
247 10
		if (null === $this->queryId || $this->queryId === false) {
248
			$this->halt('', $line, $file);
249
		}
250
251
		// Will return nada if it fails. That's fine.
252 10
		return $this->queryId;
253
	}
254
255
	/**
256
	 * @return array|null|object
257
	 */
258 1
	public function fetchObject()
259
	{
260 1
		$this->Record = @mysqli_fetch_object($this->queryId);
0 ignored issues
show
Bug introduced by
$this->queryId of type integer is incompatible with the type mysqli_result expected by parameter $result of mysqli_fetch_object(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

260
		$this->Record = @mysqli_fetch_object(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
261 1
		return $this->Record;
262
	}
263
264
	/* public: walk result set */
265
266
	/**
267
	 * Db::next_record()
268
	 *
269
	 * @param mixed $resultType
270
	 * @return bool
271
	 */
272 6
	public function next_record($resultType = MYSQLI_BOTH)
273
	{
274 6
		if ($this->queryId === false) {
0 ignored issues
show
introduced by
The condition $this->queryId === false is always false.
Loading history...
275
			$this->haltmsg('next_record called with no query pending.');
276
			return 0;
277
		}
278
279 6
		$this->Record = @mysqli_fetch_array($this->queryId, $resultType);
0 ignored issues
show
Bug introduced by
$this->queryId of type integer is incompatible with the type mysqli_result expected by parameter $result of mysqli_fetch_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

279
		$this->Record = @mysqli_fetch_array(/** @scrutinizer ignore-type */ $this->queryId, $resultType);
Loading history...
280 6
		++$this->Row;
281 6
		$this->Errno = mysqli_errno($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_errno() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

281
		$this->Errno = mysqli_errno(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
282 6
		$this->Error = mysqli_error($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_error() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

282
		$this->Error = mysqli_error(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
283
284 6
		$stat = is_array($this->Record);
285 6
		if (!$stat && $this->autoFree && is_resource($this->queryId)) {
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
286
			$this->free();
287
		}
288 6
		return $stat;
289
	}
290
291
	/**
292
	 * switch to position in result set
293
	 *
294
	 * @param integer $pos the row numbe starting at 0 to switch to
295
	 * @return bool whetherit was successfu or not.
296
	 */
297 1
	public function seek($pos = 0)
298
	{
299 1
		$status = @mysqli_data_seek($this->queryId, $pos);
0 ignored issues
show
Bug introduced by
$this->queryId of type integer is incompatible with the type mysqli_result expected by parameter $result of mysqli_data_seek(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

299
		$status = @mysqli_data_seek(/** @scrutinizer ignore-type */ $this->queryId, $pos);
Loading history...
300 1
		if ($status) {
301 1
			$this->Row = $pos;
302
		} else {
303 1
			$this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__);
304
			/* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */
305 1
			$rows = $this->num_rows();
306 1
			@mysqli_data_seek($this->queryId, $rows);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mysqli_data_seek(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

306
			/** @scrutinizer ignore-unhandled */ @mysqli_data_seek($this->queryId, $rows);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
307 1
			$this->Row = $rows;
308 1
			return false;
309
		}
310 1
		return true;
311
	}
312
313
	/**
314
	 * Initiates a transaction
315
	 *
316
	 * @return bool
317
	 */
318 26
	public function transactionBegin()
319
	{
320 26
		if (version_compare(PHP_VERSION, '5.5.0') < 0) {
321
			return true;
322
		}
323 26
		if (!$this->connect()) {
324
			return 0;
325
		}
326 26
		return mysqli_begin_transaction($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_begin_transaction() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

326
		return mysqli_begin_transaction(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
327
	}
328
329
	/**
330
	 * Commits a transaction
331
	 *
332
	 * @return bool
333
	 */
334 1
	public function transactionCommit()
335
	{
336 1
		if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) {
337
			return true;
338
		}
339 1
		return mysqli_commit($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_commit() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

339
		return mysqli_commit(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
340
	}
341
342
	/**
343
	 * Rolls back a transaction
344
	 *
345
	 * @return bool
346
	 */
347 26
	public function transactionAbort()
348
	{
349 26
		if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) {
350
			return true;
351
		}
352 26
		return mysqli_rollback($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_rollback() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

352
		return mysqli_rollback(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
353
	}
354
355
	/**
356
	 * This will get the last insert ID created on the current connection.  Should only be called after an insert query is
357
	 * run on a table that has an auto incrementing field.  $table and $field are required, but unused here since it's
358
	 * unnecessary for mysql.  For compatibility with pgsql, the params must be supplied.
359
	 *
360
	 * @param string $table
361
	 * @param string $field
362
	 * @return int|string
363
	 */
364 2
	public function getLastInsertId($table, $field)
365
	{
366 2
		if (!isset($table) || $table == '' || !isset($field) || $field == '') {
367
			return -1;
368
		}
369
370 2
		return @mysqli_insert_id($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_insert_id() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

370
		return @mysqli_insert_id(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
371
	}
372
373
	/* public: table locking */
374
375
	/**
376
	 * Db::lock()
377
	 * @param mixed  $table
378
	 * @param string $mode
379
	 * @return bool|int|\mysqli_result
380
	 */
381 1
	public function lock($table, $mode = 'write')
382
	{
383 1
		$this->connect();
384 1
		$query = 'lock tables ';
385 1
		if (is_array($table)) {
386 1
			foreach ($table as $key => $value) {
387 1
				if ($key == 'read' && $key != 0) {
388
					$query .= "$value read, ";
389
				} else {
390 1
					$query .= "$value $mode, ";
391
				}
392
			}
393 1
			$query = mb_substr($query, 0, -2);
394
		} else {
395 1
			$query .= "$table $mode";
396
		}
397 1
		$res = @mysqli_query($this->linkId, $query);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_query() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

397
		$res = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, $query);
Loading history...
398 1
		if (!$res) {
399
			$this->halt("lock($table, $mode) failed.");
400
			return 0;
401
		}
402 1
		return $res;
403
	}
404
405
	/**
406
	 * Db::unlock()
407
	 * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error
408
	 * @return bool|int|\mysqli_result
409
	 */
410 2
	public function unlock($haltOnError = true)
411
	{
412 2
		$this->connect();
413
414 2
		$res = @mysqli_query($this->linkId, 'unlock tables');
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_query() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

414
		$res = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, 'unlock tables');
Loading history...
415 2
		if ($haltOnError === true && !$res) {
416
			$this->halt('unlock() failed.');
417
			return 0;
418
		}
419 2
		return $res;
420
	}
421
422
	/* public: evaluate the result (size, width) */
423
424
	/**
425
	 * Db::affectedRows()
426
	 * @return int
427
	 */
428 2
	public function affectedRows()
429
	{
430 2
		return @mysqli_affected_rows($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $link of mysqli_affected_rows() does only seem to accept mysqli, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

430
		return @mysqli_affected_rows(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
431
	}
432
433
	/**
434
	 * Db::num_rows()
435
	 * @return int
436
	 */
437 6
	public function num_rows()
438
	{
439 6
		return @mysqli_num_rows($this->queryId);
0 ignored issues
show
Bug introduced by
$this->queryId of type integer is incompatible with the type mysqli_result expected by parameter $result of mysqli_num_rows(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

439
		return @mysqli_num_rows(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
440
	}
441
442
	/**
443
	 * Db::num_fields()
444
	 * @return int
445
	 */
446 1
	public function num_fields()
447
	{
448 1
		return @mysqli_num_fields($this->queryId);
0 ignored issues
show
Bug introduced by
$this->queryId of type integer is incompatible with the type mysqli_result expected by parameter $result of mysqli_num_fields(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

448
		return @mysqli_num_fields(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
449
	}
450
451
	/**
452
	 * gets an array of the table names in teh current datase
453
	 *
454
	 * @return array
455
	 */
456 1
	public function tableNames()
457
	{
458 1
		$return = [];
459 1
		$this->query('SHOW TABLES');
460 1
		$i = 0;
461 1
		while ($info = $this->queryId->fetch_row()) {
0 ignored issues
show
Bug introduced by
The method fetch_row() does not exist on integer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

461
		while ($info = $this->queryId->/** @scrutinizer ignore-call */ fetch_row()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
462 1
			$return[$i]['table_name'] = $info[0];
463 1
			$return[$i]['tablespace_name'] = $this->database;
464 1
			$return[$i]['database'] = $this->database;
465 1
			++$i;
466
		}
467 1
		return $return;
468
	}
469
}
470
471
/**
472
 * @param $result
473
 * @param $row
474
 * @param int|string $field
475
 * @return bool
476
 */
477
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
478
function mysqli_result($result, $row, $field = 0) {
479
	if ($result === false) return false;
480
	if ($row >= mysqli_num_rows($result)) return false;
481
	if (is_string($field) && !(mb_strpos($field, '.') === false)) {
482
		$tField = explode('.', $field);
483
		$field = -1;
484
		$tFields = mysqli_fetch_fields($result);
485
		for ($id = 0, $idMax = mysqli_num_fields($result); $id < $idMax; $id++) {
486
			if ($tFields[$id]->table == $tField[0] && $tFields[$id]->name == $tField[1]) {
487
				$field = $id;
488
				break;
489
			}
490
		}
491
		if ($field == -1) return false;
492
	}
493
	mysqli_data_seek($result, $row);
494
	$line = mysqli_fetch_array($result);
495
	return isset($line[$field]) ? $line[$field] : false;
496
}
497
*/
498