Completed
Push — master ( 5f2082...0a6610 )
by Davey
02:04
created

MySQL::escapeString()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 11.2824
Metric Value
dl 0
loc 29
ccs 16
cts 23
cp 0.6957
rs 4.9091
cc 9
eloc 23
nc 9
nop 1
crap 11.2824
1
<?php
2
namespace {
3
4
    if (!function_exists('\mysql_connect')) {
5
        define('MYSQL_CLIENT_COMPRESS', MYSQLI_CLIENT_COMPRESS);
6
        define('MYSQL_CLIENT_IGNORE_SPACE', MYSQLI_CLIENT_IGNORE_SPACE);
7
        define('MYSQL_CLIENT_INTERACTIVE', MYSQLI_CLIENT_INTERACTIVE);
8
        define('MYSQL_CLIENT_SSL', MYSQLI_CLIENT_SSL);
9
10
        function mysql_connect(
11
            $hostname = null,
12
            $username = null,
13
            $password = null,
14
            $new = false,
15
            $flags = 0
16
        ) {
17 46
            if ($new !== false) {
18 1
                trigger_error('Argument $new is no longer supported in PHP > 7', E_USER_WARNING);
19
            }
20
21 45
            $hash = sha1($hostname . $username . $flags);
22 45
            if ($hostname{1} != ':' && isset(\Dshafik\MySQL::$connections[$hash])) {
23 10
                \Dshafik\MySQL::$last_connection = \Dshafik\MySQL::$connections[$hash]['conn'];
24 10
                \Dshafik\MySQL::$connections[$hash]['refcount'] += 1;
25 10
                return \Dshafik\MySQL::$connections[$hash]['conn'];
26
            }
27
28 36
            if ($flags === 0) {
29 34
                \Dshafik\MySQL::$last_connection = $conn = mysqli_connect($hostname, $username, $password);
30 33
                $conn->hash = $hash;
31 33
                \Dshafik\MySQL::$connections[$hash] = ['refcount' => 1, 'conn' => $conn];
32
33 33
                return $conn;
34
            }
35
36
            try {
37 2
                \Dshafik\MySQL::$last_connection = $conn = mysqli_init();
38
39 2
                mysqli_real_connect(
40
                    $conn,
41
                    $hostname,
42
                    $username,
43
                    $password,
44 2
                    '',
45 2
                    null,
46 2
                    '',
47
                    $flags
48
                );
49
50
                // @codeCoverageIgnoreStart
51
                // PHPUnit turns the warning from mysqli_real_connect into an exception, so this never runs
52
                if ($conn === false) {
53
                    return false;
54
                }
55
                // @codeCoverageIgnoreEnd
56
57 1
                $conn->hash = $hash;
58 1
                \Dshafik\MySQL::$connections[$hash] = ['refcount' => 1, 'conn' => $conn];
59
60 1
                return $conn;
61 1
            } catch (\Throwable $e) {
62 1
                trigger_error($e->getMessage(), E_USER_WARNING);
63
                // @codeCoverageIgnoreStart
64
                // PHPUnit turns the warning into an exception, so this never runs
65
                return false;
66
                // @codeCoverageIgnoreEnd
67
            }
68
        }
69
70
        function mysql_pconnect(
71
            $hostname = null,
72
            $username = null,
73
            $password = null,
74
            $flags = 0
75
        ) {
76 1
            $hostname = 'p:' . $hostname;
77 1
            return mysql_connect($hostname, $username, $password, false, $flags);
78
        }
79
80
        function mysql_close(\mysqli $link = null)
81
        {
82 68
            $isDefault = ($link === null);
83
84 68
            $link = \Dshafik\MySQL::getConnection($link, __FUNCTION__);
85 68
            if ($link === null) {
86
                // @codeCoverageIgnoreStart
87
                // PHPUnit Warning -> Exception
88
                return false;
89
                // @codeCoverageIgnoreEnd
90
            }
91
92 68
            if (isset(\Dshafik\MySQL::$connections[$link->hash])) {
93 43
                \Dshafik\MySQL::$connections[$link->hash]['refcount'] -= 1;
94
            }
95
96 68
            $return = true;
97 68
            if (\Dshafik\MySQL::$connections[$link->hash]['refcount'] == 0) {
98 59
                $return = mysqli_close($link);
99 59
                unset(\Dshafik\MySQL::$connections[$link->hash]);
100
            }
101
102 68
            if ($isDefault) {
103 68
                Dshafik\MySQL::$last_connection = null;
104
            }
105
106 68
            return $return;
107
        }
108
109
        function mysql_select_db($databaseName, \mysqli $link = null)
110
        {
111 37
            $link = \Dshafik\MySQL::getConnection($link);
112
113 37
            return mysqli_query(
114
                $link,
115 37
                "USE " . mysqli_real_escape_string($link, $databaseName)
116 37
            ) !== false;
117
        }
118
119
        function mysql_query($query, \mysqli $link = null)
120
        {
121 40
            return mysqli_query(\Dshafik\MySQL::getConnection($link), $query);
122
        }
123
124
        function mysql_unbuffered_query($query, \mysqli $link = null)
125
        {
126 4
            $link = \Dshafik\MySQL::getConnection($link);
127 4
            if (mysqli_real_query($link, $query)) {
128 3
                return mysqli_use_result($link);
129
            }
130
131 1
            return false;
132
        }
133
134
        function mysql_db_query($databaseName, $query, \mysqli $link = null)
135
        {
136 2
            if (mysql_select_db($databaseName, $link)) {
137 1
                return mysql_query($query, $link);
138
            }
139 1
            return false;
140
        }
141
142
        function mysql_list_dbs(\mysqli $link = null)
143
        {
144 1
            return mysql_query("SHOW DATABASES", $link);
145
        }
146
147
        function mysql_list_tables($databaseName, \mysqli $link = null)
148
        {
149 2
            $link = \Dshafik\MySQL::getConnection($link);
150 2
            return mysql_query("SHOW TABLES FROM " . mysql_real_escape_string($databaseName, $link), $link);
151
        }
152
153
        function mysql_list_fields($databaseName, $tableName, \mysqli $link = null)
154
        {
155 2
            $link = \Dshafik\MySQL::getConnection($link);
156 2
            $result = mysql_query(
157
                "SHOW COLUMNS FROM " .
158 2
                mysqli_real_escape_string($link, $databaseName) . "." .
159 2
                mysqli_real_escape_string($link, $tableName),
160
                $link
161
            );
162
163 2
            if ($result instanceof \mysqli_result) {
164 1
                $result->table = $tableName;
165 1
                return $result;
166
            }
167
168 1
            trigger_error("mysql_list_fields(): Unable to save MySQL query result", E_USER_WARNING);
169
            // @codeCoverageIgnoreStart
170
            return false;
171
            // @codeCoverageIgnoreEnd
172
        }
173
174
        function mysql_list_processes(\mysqli $link = null)
175
        {
176
            return mysql_query("SHOW PROCESSLIST", $link);
177
        }
178
179
        function mysql_error(\mysqli $link = null)
180
        {
181 23
            return mysqli_error(\Dshafik\MySQL::getConnection($link));
182
        }
183
184
        function mysql_errno(\mysqli $link = null)
185
        {
186 1
            return mysqli_errno(\Dshafik\MySQL::getConnection($link));
187
        }
188
189
        function mysql_affected_rows(\mysqli $link = null)
190
        {
191 1
            return mysqli_affected_rows(\Dshafik\MySQL::getConnection($link));
192
        }
193
194
        function mysql_insert_id($link = null) /*|*/
195
        {
196 1
            return mysqli_insert_id(\Dshafik\MySQL::getConnection($link));
197
        }
198
199
        function mysql_result($result, $row, $field = 0)
200
        {
201 6
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
202
                // @codeCoverageIgnoreStart
203
                return false;
204
                // @codeCoverageIgnoreEnd
205
            }
206
207 5
            if (!mysqli_data_seek($result, $row)) {
208 1
                trigger_error(
209
                    sprintf(
210 1
                        "mysql_result(): Unable to jump to row %d on MySQL result index %s",
211
                        $row,
212
                        spl_object_hash($result)
213
                    ),
214 1
                    E_USER_WARNING
215
                );
216
                // @codeCoverageIgnoreStart
217
                return false;
218
                // @codeCoverageIgnoreEnd
219
            }
220
221 4
            $found = true;
222 4
            if (strpos($field, ".") !== false) {
223 2
                list($table, $name) =  explode(".", $field);
224 2
                $i = 0;
225 2
                $found = false;
226 2
                while ($column = mysqli_fetch_field($result)) {
227 2
                    if ($column->table == $table && $column->name == $name) {
228 1
                        $field = $i;
229 1
                        $found = true;
230 1
                        break;
231
                    }
232 1
                    $i++;
233
                }
234
            }
235
236 4
            $row = mysql_fetch_array($result);
237 4
            if ($found && isset($row[$field])) {
238 2
                return $row[$field];
239
            }
240
241 2
            trigger_error(
242
                sprintf(
243 2
                    "%s(): %s not found in MySQL result index %s",
244 2
                    __FUNCTION__,
245
                    $field,
246
                    spl_object_hash($result)
247
                ),
248 2
                E_USER_WARNING
249
            );
250
            // @codeCoverageIgnoreStart
251
            return false;
252
            // @codeCoverageIgnoreEnd
253
        }
254
255
        function mysql_num_rows($result)
256
        {
257 8
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
258
                // @codeCoverageIgnoreStart
259
                return false;
260
                // @codeCoverageIgnoreEnd
261
            }
262
263 7
            $previous = error_reporting(0);
264 7
            $rows = mysqli_num_rows($result);
265 7
            error_reporting($previous);
266
267 7
            return $rows;
268
        }
269
270
        function mysql_num_fields($result)
271
        {
272 3
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
273
                // @codeCoverageIgnoreStart
274
                return false;
275
                // @codeCoverageIgnoreEnd
276
            }
277 1
            return mysqli_num_fields($result);
278
        }
279
280
        function mysql_fetch_row($result)
281
        {
282 5
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
283
                // @codeCoverageIgnoreStart
284
                return false;
285
                // @codeCoverageIgnoreEnd
286
            }
287 4
            return mysqli_fetch_row($result);
288
        }
289
290
        function mysql_fetch_array($result)
291
        {
292 6
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
293
                // @codeCoverageIgnoreStart
294
                return false;
295
                // @codeCoverageIgnoreEnd
296
            }
297 5
            return mysqli_fetch_array($result);
298
        }
299
300
        function mysql_fetch_assoc($result) /* : array|null */
301
        {
302 6
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
303
                // @codeCoverageIgnoreStart
304
                return false;
305
                // @codeCoverageIgnoreEnd
306
            }
307 5
            return mysqli_fetch_assoc($result);
308
        }
309
310
        function mysql_fetch_object($result, $class = null, array $params = []) /* : object|null */
311
        {
312 2
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
313
                // @codeCoverageIgnoreStart
314
                return false;
315
                // @codeCoverageIgnoreEnd
316
            }
317
318 1
            if ($class == null) {
319 1
                return mysqli_fetch_object($result);
320
            }
321
322
            return mysqli_fetch_object($result, $class, $params);
323
        }
324
325
        function mysql_data_seek($result, $offset)
326
        {
327 1
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
328
                // @codeCoverageIgnoreStart
329
                return false;
330
                // @codeCoverageIgnoreEnd
331
            }
332
            return mysqli_data_seek($result, $offset);
333
        }
334
335
        function mysql_fetch_lengths($result) /* : array|*/
336
        {
337 1
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
338
                // @codeCoverageIgnoreStart
339
                return false;
340
                // @codeCoverageIgnoreEnd
341
            }
342
            return mysqli_fetch_lengths($result);
343
        }
344
345
        function mysql_fetch_field($result) /* : object|*/
346
        {
347 1
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
348
                // @codeCoverageIgnoreStart
349
                return false;
350
                // @codeCoverageIgnoreEnd
351
            }
352
            return mysqli_fetch_field($result);
353
        }
354
355
        function mysql_field_seek($result, $field)
356
        {
357 1
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
358
                // @codeCoverageIgnoreStart
359
                return false;
360
                // @codeCoverageIgnoreEnd
361
            }
362
            return mysqli_field_seek($result, $field);
363
        }
364
365
        function mysql_free_result($result)
366
        {
367 2
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
368
                // @codeCoverageIgnoreStart
369
                return false;
370
                // @codeCoverageIgnoreEnd
371
            }
372 1
            return mysqli_free_result($result);
373
        }
374
375
        function mysql_field_name($result, $field)
376
        {
377 3
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
378
                // @codeCoverageIgnoreStart
379
                return false;
380
                // @codeCoverageIgnoreEnd
381
            }
382 2
            return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'name');
383
        }
384
385
        function mysql_field_table($result, $field)
386
        {
387 3
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
388
                // @codeCoverageIgnoreStart
389
                return false;
390
                // @codeCoverageIgnoreEnd
391
            }
392 2
            return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'table');
393
        }
394
395
        function mysql_field_len($result, $field)
396
        {
397 3
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
398
                // @codeCoverageIgnoreStart
399
                return false;
400
                // @codeCoverageIgnoreEnd
401
            }
402 2
            return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'len');
403
        }
404
405
        function mysql_field_type($result, $field)
406
        {
407 3
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
408
                // @codeCoverageIgnoreStart
409
                return false;
410
                // @codeCoverageIgnoreEnd
411
            }
412 2
            return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'type');
413
        }
414
415
        function mysql_field_flags($result, $field)
416
        {
417 3
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
418
                // @codeCoverageIgnoreStart
419
                return false;
420
                // @codeCoverageIgnoreEnd
421
            }
422 2
            return \Dshafik\MySQL::mysqlFieldInfo($result, $field, 'flags');
423
        }
424
425
        function mysql_escape_string($unescapedString)
426
        {
427 2
            if (\Dshafik\MySQL::$last_connection == null) {
428 2
                trigger_error(
429
                    sprintf(
430 2
                        "%s() is insecure; use mysql_real_escape_string() instead!",
431 2
                        __FUNCTION__
432
                    ),
433 2
                    E_USER_NOTICE
434
                );
435
436 1
                return \Dshafik\MySQL::escapeString($unescapedString);
437
            }
438
            return mysql_real_escape_string($unescapedString, null);
439
        }
440
441
        function mysql_real_escape_string($unescapedString, \mysqli $link = null)
442
        {
443 2
            return mysqli_escape_string(\Dshafik\MySQL::getConnection($link), $unescapedString);
444
        }
445
446
        function mysql_stat(\mysqli $link = null)
447
        {
448
            return mysqli_stat(\Dshafik\MySQL::getConnection($link));
449
        }
450
451
        function mysql_thread_id(\mysqli $link = null)
452
        {
453
            return mysqli_thread_id(\Dshafik\MySQL::getConnection($link));
454
        }
455
456
        function mysql_client_encoding(\mysqli $link = null)
457
        {
458
            return mysqli_character_set_name(\Dshafik\MySQL::getConnection($link));
459
        }
460
461
        function mysql_ping(\mysqli $link = null)
462
        {
463
            return mysqli_ping($link);
464
        }
465
466
        function mysql_get_client_info(\mysqli $link = null)
467
        {
468
            return mysqli_get_client_info(\Dshafik\MySQL::getConnection($link));
469
        }
470
471
        function mysql_get_host_info(\mysqli $link = null)
472
        {
473
            return mysqli_get_host_info(\Dshafik\MySQL::getConnection($link));
474
        }
475
476
        function mysql_get_proto_info(\mysqli $link = null)
477
        {
478
            return mysqli_get_proto_info(\Dshafik\MySQL::getConnection($link));
479
        }
480
481
        function mysql_get_server_info(\mysqli $link = null)
482
        {
483
            return mysqli_get_server_info(\Dshafik\MySQL::getConnection($link));
484
        }
485
486
        function mysql_info(\mysqli $link = null)
487
        {
488
            return mysqli_info(\Dshafik\MySQL::getConnection($link));
489
        }
490
491
        function mysql_set_charset($charset, \mysqli $link = null)
492
        {
493
            return mysqli_set_charset(\Dshafik\MySQL::getConnection($link), $charset);
494
        }
495
496
        function mysql_db_name($result, $row)
497
        {
498 1
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
499
                // @codeCoverageIgnoreStart
500
                return false;
501
                // @codeCoverageIgnoreEnd
502
            }
503
504
            // Alias as per http://lxr.php.net/xref/PHP_5_6/ext/mysql/php_mysql.c#319
505
            return mysql_result($result, $row, 'Database');
506
        }
507
508
        function mysql_table_name($result, $row)
509
        {
510 1
            if (\Dshafik\MySQL::checkValidResult($result, __FUNCTION__)) {
511
                // @codeCoverageIgnoreStart
512
                return false;
513
                // @codeCoverageIgnoreEnd
514
            }
515
516
            // Alias as per http://lxr.php.net/xref/PHP_5_6/ext/mysql/php_mysql.c#321
517
            return mysql_result($result, $row, 'Table');
518
        }
519
520
        /* Aliases */
521
522
        function mysql_fieldname(... $args)
523
        {
524
            return mysql_field_name(... $args);
525
        }
526
527
        function mysql_fieldtable(... $args)
528
        {
529
            return mysql_field_table(... $args);
530
        }
531
532
        function mysql_fieldlen(... $args)
533
        {
534
            return mysql_field_len(... $args);
535
        }
536
537
        function mysql_fieldtype(... $args)
538
        {
539
            return mysql_field_type(... $args);
540
        }
541
542
        function mysql_fieldflags(... $args)
543
        {
544
            return mysql_field_flags(... $args);
545
        }
546
547
        function mysql_selectdb(... $args)
548
        {
549
            return mysql_select_db(... $args);
550
        }
551
552
        function mysql_freeresult(... $args)
553
        {
554
            return mysql_free_result(... $args);
555
        }
556
557
        function mysql_numfields(... $args)
558
        {
559
            return mysql_num_fields(... $args);
560
        }
561
562
        function mysql_numrows(... $args)
563
        {
564
            return mysql_num_rows(... $args);
565
        }
566
567
        function mysql_listdbs(... $args)
568
        {
569
            return mysql_list_dbs(... $args);
570
        }
571
572
        function mysql_listtables(... $args)
573
        {
574
            return mysql_list_tables(... $args);
575
        }
576
577
        function mysql_listfields(... $args)
578
        {
579
            return mysql_list_fields(... $args);
580
        }
581
582
        function mysql_dbname(... $args)
583
        {
584
            return mysql_db_name(... $args);
1 ignored issue
show
Bug introduced by
The call to mysql_db_name() misses a required argument $row.

This check looks for function calls that miss required arguments.

Loading history...
585
        }
586
587
        function mysql_tablename(... $args)
588
        {
589
            return mysql_table_name(... $args);
0 ignored issues
show
Bug introduced by
The call to mysql_table_name() misses a required argument $row.

This check looks for function calls that miss required arguments.

Loading history...
590
        }
591
    }
592
}
593
594
namespace Dshafik {
595
596
    class MySQL
597
    {
598
        public static $last_connection = null;
599
        public static $connections = [];
600
601 68
        public static function getConnection($link = null, $func = null)
602
        {
603 68
            if ($link !== null) {
604 7
                return $link;
605
            }
606
607 68
            if (static::$last_connection === null) {
608 25
                $err = "A link to the server could not be established";
609 25
                if ($func !== null) {
610 25
                    $err = $func . "(): no MySQL-Link resource supplied";
611
                }
612 25
                trigger_error($err, E_USER_WARNING);
613 25
                return false;
614
            }
615
616 44
            return static::$last_connection;
617
        }
618
619 6
        public static function mysqlFieldInfo(\mysqli_result $result, $field, $what)
620
        {
621
            try {
622 6
                $field = mysqli_fetch_field_direct($result, $field);
623 5
            } catch (\Exception $e) {
624 5
                trigger_error(
625
                    sprintf(
626 5
                        "mysql_field_%s(): Field %d is invalid for MySQL result index %s",
627
                        $what,
628
                        $field,
629
                        spl_object_hash($result)
630
                    ),
631 5
                    E_USER_WARNING
632
                );
633
                // @codeCoverageIgnoreStart
634
                // PHPUnit turns the warning into an exception, so this never runs
635
                return false;
636
                // @codeCoverageIgnoreEnd
637
            }
638
639 1
            if ($what == 'name' || $what == 'table') {
640 1
                return $field->{$what};
641
            }
642
643 1
            if ($what == 'len') {
644 1
                return $field->length;
645
            }
646
647 1
            if ($what == 'type') {
648 1
                return static::getFieldType($field->type);
649
            }
650
651 1
            if ($what == 'flags') {
652 1
                return static::getFieldFlags($field->flags);
653
            }
654
655
            return false;
656
        }
657
658 45
        public static function checkValidResult($result, $function)
659
        {
660 45
            if (!($result instanceof \mysqli_result)) {
661 20
                if ($function != "mysql_fetch_object") {
662 19
                    trigger_error(
663 19
                        $function . "() expects parameter 1 to be resource, " . strtolower(gettype($result)) . " given",
664 19
                        E_USER_WARNING
665
                    );
666
                }
667
668 1
                if ($function == "mysql_fetch_object") {
669 1
                    trigger_error(
670 1
                        $function . "(): supplied argument is not a valid MySQL result resource",
671 1
                        E_USER_WARNING
672
                    );
673
                }
674
                return false;
675
            }
676
677 25
        }
678
679 1
        public static function escapeString($unescapedString)
680
        {
681 1
            $escapedString = "";
682 1
            for ($i = 0; $i < strlen($unescapedString); $i++) {
683 1
                switch ($unescapedString{$i}) {
684
                    case "\0":
685 1
                        $esc = 0;
686 1
                        break;
687
                    case "\n":
688 1
                        $esc = "n";
689 1
                        break;
690
                    case "\r":
691 1
                        $esc = "r";
692 1
                        break;
693
                    case '\\':
694
                    case '\'':
695
                    case '"':
696 1
                        $esc = $unescapedString{$i};
697 1
                        break;
698
                    case "\032":
699 1
                        $esc = 'Z';
700 1
                        break;
701
                }
702 1
                $escapedString .= "\\$esc";
0 ignored issues
show
Bug introduced by
The variable $esc does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
703
704
            }
705
706 1
            return $escapedString;
707
        }
708
709 1
        protected static function getFieldFlags($what)
710
        {
711
            // Order of flags taken from http://lxr.php.net/xref/PHP_5_6/ext/mysql/php_mysql.c#2507
712
713 1
            $flags = [];
714 1
            if ($what & MYSQLI_NOT_NULL_FLAG) {
715 1
                $flags[] = "not_null";
716
            }
717
718 1
            if ($what & MYSQLI_PRI_KEY_FLAG) {
719 1
                $flags[] = "primary_key";
720
            }
721
722 1
            if ($what & MYSQLI_UNIQUE_KEY_FLAG) {
723 1
                $flags[] = "unique_key";
724
            }
725
726 1
            if ($what & MYSQLI_MULTIPLE_KEY_FLAG) {
727 1
                $flags[] = "multiple_key";
728
            }
729
730 1
            if ($what & MYSQLI_BLOB_FLAG) {
731 1
                $flags[] = "blob";
732
            }
733
734 1
            if ($what & MYSQLI_UNSIGNED_FLAG) {
735
                $flags[] = "unsigned";
736
            }
737
738 1
            if ($what & MYSQLI_ZEROFILL_FLAG) {
739
                $flags[] = "zerofill";
740
            }
741
742 1
            if ($what & MYSQLI_BINARY_FLAG) {
743
                $flags[] = "binary";
744
            }
745
746 1
            if ($what & MYSQLI_ENUM_FLAG) {
747 1
                $flags[] = "enum";
748
            }
749
750 1
            if ($what & MYSQLI_SET_FLAG) {
751 1
                $flags[] = "set";
752
            }
753
754
755 1
            if ($what & MYSQLI_AUTO_INCREMENT_FLAG) {
756 1
                $flags[] = "auto_increment";
757
            }
758
759 1
            if ($what & MYSQLI_TIMESTAMP_FLAG) {
760
                $flags[] = "timestamp";
761
            }
762
763 1
            return implode(" ", $flags);
764
        }
765
766 1
        protected static function getFieldType($what)
767
        {
768
            switch ($what) {
769 1
                case MYSQLI_TYPE_STRING:
770 1
                case MYSQLI_TYPE_VAR_STRING:
771 1
                case MYSQLI_TYPE_ENUM:
772 1
                case MYSQLI_TYPE_SET:
773 1
                    return "string";
774 1
                case MYSQLI_TYPE_LONG:
775 1
                case MYSQLI_TYPE_TINY:
776 1
                case MYSQLI_TYPE_SHORT:
777 1
                case MYSQLI_TYPE_INT24:
778 1
                case MYSQLI_TYPE_CHAR:
779 1
                case MYSQLI_TYPE_LONGLONG:
780 1
                    return "int";
781 1
                case MYSQLI_TYPE_DECIMAL:
782 1
                case MYSQLI_TYPE_FLOAT:
783 1
                case MYSQLI_TYPE_DOUBLE:
784 1
                case MYSQLI_TYPE_NEWDECIMAL:
785
                    return "real";
786 1
                case MYSQLI_TYPE_DATETIME:
787
                    return "datetime";
788 1
                case MYSQLI_TYPE_TIMESTAMP:
789
                    return "timestamp";
790 1
                case MYSQLI_TYPE_NEWDATE:
791 1
                case MYSQLI_TYPE_DATE:
792
                    return "date";
793 1
                case MYSQLI_TYPE_TIME:
794
                    return "time";
795 1
                case MYSQLI_TYPE_YEAR:
796
                    return "year";
797 1
                case MYSQLI_TYPE_TINY_BLOB:
798 1
                case MYSQLI_TYPE_MEDIUM_BLOB:
799 1
                case MYSQLI_TYPE_LONG_BLOB:
800 1
                case MYSQLI_TYPE_BLOB:
801 1
                    return "blob";
802
                case MYSQLI_TYPE_NULL:
803
                    return "null";
804
                case MYSQLI_TYPE_GEOMETRY:
805
                    return "geometry";
806
                case MYSQLI_TYPE_INTERVAL:
807
                case MYSQLI_TYPE_BIT:
808
                default:
809
                    return "unknown";
810
            }
811
        }
812
    }
813
}
814