Test Failed
Push — master ( ca234f...45a6b8 )
by Joe
14:12
created

Db::connect()   C

Complexity

Conditions 12
Paths 288

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 12.9147

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 12
eloc 27
nc 288
nop 5
dl 0
loc 41
ccs 22
cts 27
cp 0.8148
crap 12.9147
rs 5.0333
c 4
b 0
f 0

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
 * 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 $mysql 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 = '', $port = '')
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 26
        if ($port == '') {
76 26
            $port = $this->port;
77
        }
78
        /* establish connection, select database */
79 26
        if (!is_object($this->linkId)) {
80 26
            $this->connectionAttempt++;
81 26
            if ($this->connectionAttempt > 1) {
82 6
                error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}");
83
            }
84 26
            if ($this->connectionAttempt >= $this->maxConnectErrors) {
85
                $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...
86
                return 0;
87
            }
88 26
            $this->linkId = mysqli_init();
89 26
            $this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}");
90 26
            if (!$this->linkId->real_connect($host, $user, $password, $database, $port != '' ? $port : NULL)) {
91
                $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error);
92
                return 0;
93 26
            }
94
            $this->linkId->set_charset($this->characterSet);
95 26
            if ($this->linkId->connect_errno) {
96 26
                $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error);
97
                return 0;
98
            }
99
        }
100
        return $this->linkId;
101 26
    }
102
103
    /**
104
     * Db::disconnect()
105
     * @return bool
106
     */
107
    public function disconnect()
108 1
    {
109
        $return = !is_int($this->linkId) && method_exists($this->linkId, 'close') ? $this->linkId->close() : false;
110 1
        $this->linkId = 0;
111 1
        return $return;
112 1
    }
113
114
    /**
115
     * @param $string
116
     * @return string
117
     */
118
    public function real_escape($string = '')
119 2
    {
120
        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...
121 2
            return $this->escape($string);
122
        }
123
        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 resource; however, parameter $mysql 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

123
        return mysqli_real_escape_string(/** @scrutinizer ignore-type */ $this->linkId, $string);
Loading history...
124 2
    }
125
126
    /**
127
     * discard the query result
128
     * @return void
129
     */
130
    public function free()
131 1
    {
132
        if (is_resource($this->queryId)) {
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
133 1
            @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

133
            /** @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...
134
        }
135
        $this->queryId = 0;
136 1
    }
137
138
    /**
139
     * Db::queryReturn()
140
     *
141
     * Sends an SQL query to the server like the normal query() command but iterates through
142
     * any rows and returns the row or rows immediately or FALSE on error
143
     *
144
     * @param mixed $query SQL Query to be used
145
     * @param string $line optionally pass __LINE__ calling the query for logging
146
     * @param string $file optionally pass __FILE__ calling the query for logging
147
     * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only
148
     */
149
    public function queryReturn($query, $line = '', $file = '')
150 1
    {
151
        $this->query($query, $line, $file);
152 1
        if ($this->num_rows() == 0) {
153 1
            return false;
154 1
        } elseif ($this->num_rows() == 1) {
155 1
            $this->next_record(MYSQLI_ASSOC);
156 1
            return $this->Record;
157 1
        } else {
158
            $out = [];
159 1
            while ($this->next_record(MYSQLI_ASSOC)) {
160 1
                $out[] = $this->Record;
161 1
            }
162
            return $out;
163 1
        }
164
    }
165
166
    /**
167
     * db:qr()
168
     *
169
     *  alias of queryReturn()
170
     *
171
     * @param mixed $query SQL Query to be used
172
     * @param string $line optionally pass __LINE__ calling the query for logging
173
     * @param string $file optionally pass __FILE__ calling the query for logging
174
     * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only
175
     */
176
    public function qr($query, $line = '', $file = '')
177 1
    {
178
        return $this->queryReturn($query, $line, $file);
179 1
    }
180
181
    /**
182
     * creates a prepaired statement from query
183
     *
184
     * @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 = ?
185
     * @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...
186
     * @param string $line
187
     * @param string $file
188
     */
189
    public function prepare($query, $line = '', $file = '')
190 1
    {
191
        if (!$this->connect()) {
192 1
            return 0;
193
        }
194
        $haltPrev = $this->haltOnError;
0 ignored issues
show
Unused Code introduced by
The assignment to $haltPrev is dead and can be removed.
Loading history...
195 1
        $this->haltOnError = 'no';
196 1
        $start = microtime(true);
197 1
        $prepare = 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 $mysql 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

197
        $prepare = mysqli_prepare(/** @scrutinizer ignore-type */ $this->linkId, $query);
Loading history...
198 1
        if (!isset($GLOBALS['disable_db_queries'])) {
199 1
            $this->addLog($query, microtime(true) - $start, $line, $file);
200 1
        }
201
        return $prepare;
202
    }
203
204
    /**
205
     * Db::query()
206
     *
207
     *  Sends an SQL query to the database
208
     *
209
     * @param mixed $queryString
210
     * @param string $line
211
     * @param string $file
212
     * @return mixed 0 if no query or query id handler, safe to ignore this return
213 10
     */
214
    public function query($queryString, $line = '', $file = '')
215
    {
216
        /* No empty queries, please, since PHP4 chokes on them. */
217
        /* The empty query string is passed on from the constructor,
218
        * when calling the class without a query, e.g. in situations
219
        * like these: '$db = new db_Subclass;'
220 10
        */
221 1
        if ($queryString == '') {
222
            return 0;
223 10
        }
224
        if (!$this->connect()) {
225
            return 0;
226
            /* we already complained in connect() about that. */
227 10
        }
228 10
        $haltPrev = $this->haltOnError;
229
        $this->haltOnError = 'no';
230 10
        // New query, discard previous result.
231
        if (is_resource($this->queryId)) {
0 ignored issues
show
introduced by
The condition is_resource($this->queryId) is always false.
Loading history...
232
            $this->free();
233 10
        }
234 1
        if ($this->Debug) {
235
            printf("Debug: query = %s<br>\n", $queryString);
236 10
        }
237
        if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) {
238
            $this->log($queryString, $line, $file);
239 10
        }
240 10
        $tries = 3;
241 10
        $try = 0;
242 10
        $this->queryId = false;
243 10
        while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) {
244 10
            $try++;
245
            if ($try > 1) {
246
                @mysqli_close($this->linkId);
0 ignored issues
show
Bug introduced by
It seems like $this->linkId can also be of type integer; however, parameter $mysql 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

246
                @mysqli_close(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
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

246
                /** @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...
247
                $this->linkId = 0;
248 10
                $this->connect();
249 10
            }
250 10
            $start = microtime(true);
251 10
            $onlyRollback = true;
252
            $fails = -1;
253
            while ($fails < 100 && (null === $this->queryId || $this->queryId === false)) {
254
                $fails++;
255
                try {
256
                    $this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT);
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $mysql of mysqli_query(). ( Ignorable by Annotation )

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

256
                    $this->queryId = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, $queryString, MYSQLI_STORE_RESULT);
Loading history...
257
                    if (in_array((int)@mysqli_errno($this->linkId), [2006, 3101, 1180])) {
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $mysql of mysqli_errno(). ( Ignorable by Annotation )

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

257
                    if (in_array((int)@mysqli_errno(/** @scrutinizer ignore-type */ $this->linkId), [2006, 3101, 1180])) {
Loading history...
258 10
                        //error_log("got ".@mysqli_errno($this->linkId)." sql error fails {$fails} on query {$queryString} from {$line}:{$file}");
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
259
                        usleep(500000); // 0.5 second
260
                    } else {
261 10
                        $onlyRollback = false;
262 10
                    }
263 10
                } catch (\mysqli_sql_exception $e) {
264 10
                    if (in_array((int)$e->getCode(), [2006, 3101, 1180])) {
265 10
                        //error_log("got ".$e->getCode()." sql error fails {$fails}");
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
266
                        usleep(500000); // 0.5 second
267
                    } else {
268
                        error_log('Got mysqli_sql_exception code '.$e->getCode().' error '.$e->getMessage().' on query '.$queryString.' from '.$line.':'.$file);
269 10
                        $onlyRollback = false;
270 10
                    }
271
                }
272
            }
273
            if ($onlyRollback === true && false === $this->queryId) {
274
                error_log('Got MySQLi 3101 Rollback Error '.$fails.' Times, Giving Up on '.$queryString.' from '.$line.':'.$file.' on '.__LINE__.':'.__FILE__);
275 10
            }
276
            if (!isset($GLOBALS['disable_db_queries'])) {
277
                $this->addLog($queryString, microtime(true) - $start, $line, $file);
278
            }
279
            $this->Row = 0;
280
            $this->Errno = @mysqli_errno($this->linkId);
281 1
            $this->Error = @mysqli_error($this->linkId);
0 ignored issues
show
Bug introduced by
$this->linkId of type integer is incompatible with the type mysqli expected by parameter $mysql of mysqli_error(). ( 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->Error = @mysqli_error(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
282
            if ($try == 1 && (null === $this->queryId || $this->queryId === false)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
283 1
                //$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file);
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
284 1
            }
285
        }
286
        $this->haltOnError = $haltPrev;
287
        if (null === $this->queryId || $this->queryId === false) {
288
            $this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file);
289
            $this->halt('', $line, $file);
290
        }
291
292
        // Will return nada if it fails. That's fine.
293
        return $this->queryId;
294
    }
295 6
296
    /**
297 6
     * @return array|null|object
298
     */
299
    public function fetchObject()
300
    {
301
        $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

301
        $this->Record = @mysqli_fetch_object(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
302 6
        return $this->Record;
303 6
    }
304 6
305 6
    /* public: walk result set */
306
307 6
    /**
308 6
     * Db::next_record()
309
     *
310
     * @param mixed $resultType
311 6
     * @return bool
312
     */
313
    public function next_record($resultType = MYSQLI_BOTH)
314
    {
315
        if ($this->queryId === false) {
0 ignored issues
show
introduced by
The condition $this->queryId === false is always false.
Loading history...
316
            $this->haltmsg('next_record called with no query pending.');
317
            return 0;
318
        }
319
320 1
        $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

320
        $this->Record = @mysqli_fetch_array(/** @scrutinizer ignore-type */ $this->queryId, $resultType);
Loading history...
321
        ++$this->Row;
322 1
        $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 $mysql 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

322
        $this->Errno = mysqli_errno(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
323 1
        $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 $mysql 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

323
        $this->Error = mysqli_error(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
324 1
325
        $stat = is_array($this->Record);
326 1
        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...
327
            $this->free();
328 1
        }
329 1
        return $stat;
330 1
    }
331 1
332
    /**
333 1
     * switch to position in result set
334
     *
335
     * @param integer $pos the row numbe starting at 0 to switch to
336
     * @return bool whetherit was successfu or not.
337
     */
338
    public function seek($pos = 0)
339
    {
340
        $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

340
        $status = @mysqli_data_seek(/** @scrutinizer ignore-type */ $this->queryId, $pos);
Loading history...
341 26
        if ($status) {
342
            $this->Row = $pos;
343 26
        } else {
344
            $this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__);
345
            /* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */
346 26
            $rows = $this->num_rows();
347
            @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

347
            /** @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...
348
            $this->Row = $rows;
349 26
            return false;
350
        }
351
        return true;
352
    }
353
354
    /**
355
     * Initiates a transaction
356
     *
357 1
     * @return bool
358
     */
359 1
    public function transactionBegin()
360
    {
361
        if (version_compare(PHP_VERSION, '5.5.0') < 0) {
362 1
            return true;
363
        }
364
        if (!$this->connect()) {
365
            return 0;
366
        }
367
        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 $mysql 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

367
        return mysqli_begin_transaction(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
368
    }
369
370 26
    /**
371
     * Commits a transaction
372 26
     *
373
     * @return bool
374
     */
375 26
    public function transactionCommit()
376
    {
377
        if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) {
378
            return true;
379
        }
380
        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 $mysql 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

380
        return mysqli_commit(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
381
    }
382
383
    /**
384
     * Rolls back a transaction
385
     *
386
     * @return bool
387 2
     */
388
    public function transactionAbort()
389 2
    {
390
        if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) {
391
            return true;
392
        }
393 2
        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 $mysql 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

393
        return mysqli_rollback(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
394
    }
395
396
    /**
397
     * This will get the last insert ID created on the current connection.  Should only be called after an insert query is
398
     * run on a table that has an auto incrementing field.  $table and $field are required, but unused here since it's
399
     * unnecessary for mysql.  For compatibility with pgsql, the params must be supplied.
400
     *
401
     * @param string $table
402
     * @param string $field
403
     * @return int|string
404 1
     */
405
    public function getLastInsertId($table, $field)
406 1
    {
407 1
        if (!isset($table) || $table == '' || !isset($field) || $field == '') {
408 1
            return -1;
409 1
        }
410 1
411
        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 $mysql 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

411
        return @mysqli_insert_id(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
412
    }
413 1
414
    /* public: table locking */
415
416 1
    /**
417
     * Db::lock()
418 1
     * @param mixed  $table
419
     * @param string $mode
420 1
     * @return bool|int|\mysqli_result
421 1
     */
422
    public function lock($table, $mode = 'write')
423
    {
424
        $this->connect();
425 1
        $query = 'lock tables ';
426
        if (is_array($table)) {
427
            foreach ($table as $key => $value) {
428
                if ($key == 'read' && $key != 0) {
429
                    $query .= "$value read, ";
430
                } else {
431
                    $query .= "$value $mode, ";
432
                }
433 2
            }
434
            $query = mb_substr($query, 0, -2);
435 2
        } else {
436
            $query .= "$table $mode";
437 2
        }
438 2
        $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 $mysql 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

438
        $res = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, $query);
Loading history...
439
        if (!$res) {
440
            $this->halt("lock($table, $mode) failed.");
441
            return 0;
442 2
        }
443
        return $res;
444
    }
445
446
    /**
447
     * Db::unlock()
448
     * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error
449
     * @return bool|int|\mysqli_result
450
     */
451 2
    public function unlock($haltOnError = true)
452
    {
453 2
        $this->connect();
454
455
        $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 $mysql 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

455
        $res = @mysqli_query(/** @scrutinizer ignore-type */ $this->linkId, 'unlock tables');
Loading history...
456
        if ($haltOnError === true && !$res) {
457
            $this->halt('unlock() failed.');
458
            return 0;
459
        }
460 6
        return $res;
461
    }
462 6
463
    /* public: evaluate the result (size, width) */
464
465
    /**
466
     * Db::affectedRows()
467
     * @return int
468
     */
469 1
    public function affectedRows()
470
    {
471 1
        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 $mysql 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

471
        return @mysqli_affected_rows(/** @scrutinizer ignore-type */ $this->linkId);
Loading history...
472
    }
473
474
    /**
475
     * Db::num_rows()
476
     * @return int
477
     */
478
    public function num_rows()
479 1
    {
480
        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

480
        return @mysqli_num_rows(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
481 1
    }
482 1
483 1
    /**
484 1
     * Db::num_fields()
485 1
     * @return int
486 1
     */
487 1
    public function num_fields()
488 1
    {
489
        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

489
        return @mysqli_num_fields(/** @scrutinizer ignore-type */ $this->queryId);
Loading history...
490 1
    }
491
492
    /**
493
     * gets an array of the table names in teh current datase
494
     *
495
     * @return array
496
     */
497
    public function tableNames()
498
    {
499
        $return = [];
500
        $this->query('SHOW TABLES');
501
        $i = 0;
502
        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

502
        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...
503
            $return[$i]['table_name'] = $info[0];
504
            $return[$i]['tablespace_name'] = $this->database;
505
            $return[$i]['database'] = $this->database;
506
            ++$i;
507
        }
508
        return $return;
509
    }
510
}
511
512
/**
513
 * @param $result
514
 * @param $row
515
 * @param int|string $field
516
 * @return bool
517
 */
518
/*
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...
519
function mysqli_result($result, $row, $field = 0) {
520
    if ($result === false) return false;
521
    if ($row >= mysqli_num_rows($result)) return false;
522
    if (is_string($field) && !(mb_strpos($field, '.') === false)) {
523
        $tField = explode('.', $field);
524
        $field = -1;
525
        $tFields = mysqli_fetch_fields($result);
526
        for ($id = 0, $idMax = mysqli_num_fields($result); $id < $idMax; $id++) {
527
            if ($tFields[$id]->table == $tField[0] && $tFields[$id]->name == $tField[1]) {
528
                $field = $id;
529
                break;
530
            }
531
        }
532
        if ($field == -1) return false;
533
    }
534
    mysqli_data_seek($result, $row);
535
    $line = mysqli_fetch_array($result);
536
    return isset($line[$field]) ? $line[$field] : false;
537
}
538
*/
539