PMA_splitSqlFile()   D
last analyzed

Complexity

Conditions 30
Paths 6

Size

Total Lines 104
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 30
eloc 57
c 4
b 0
f 0
nc 6
nop 3
dl 0
loc 104
rs 4.1666

How to fix   Long Method    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
/*
4
*******************************************************
5
***													***
6
*** backpack										***
7
*** Cedric MONTUY pour CHG-WEB                      ***
8
*** Original author : Yoshi Sakai					***
9
***													***
10
*******************************************************
11
*/
12
function PMA_splitSqlFile(&$ret, $sql, $release)
0 ignored issues
show
Unused Code introduced by
The parameter $release is not used and could be removed. ( Ignorable by Annotation )

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

12
function PMA_splitSqlFile(&$ret, $sql, /** @scrutinizer ignore-unused */ $release)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
13
{
14
    $sql          = rtrim($sql, "\n\r");
15
    $sql_len      = strlen($sql);
16
    $char         = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $char is dead and can be removed.
Loading history...
17
    $string_start = '';
18
    $in_string    = false;
19
    $nothing      = true;
20
    $time0        = time();
21
22
    for ($i = 0; $i < $sql_len; ++$i) {
23
        $char = $sql[$i];
24
25
        // We are in a string, check for not escaped end of strings except for
26
        // backquotes that can't be escaped
27
        if ($in_string) {
28
            for (; ;) {
29
                $i = strpos($sql, $string_start, $i);
30
                // No end of string found -> add the current substring to the
31
                // returned array
32
                if (!$i) {
33
                    $ret[] = $sql;
34
                    return true;
35
                }
36
                // Backquotes or no backslashes before quotes: it's indeed the
37
                // end of the string -> exit the loop
38
39
                if ('`' == $string_start || '\\' != $sql[$i - 1]) {
40
                    $string_start = '';
41
                    $in_string    = false;
42
                    break;
43
                }
44
                // one or more Backslashes before the presumed end of string...
45
46
                // ... first checks for escaped backslashes
47
                $j                 = 2;
48
                $escaped_backslash = false;
49
                while ($i - $j > 0 && '\\' == $sql[$i - $j]) {
50
                    $escaped_backslash = !$escaped_backslash;
0 ignored issues
show
introduced by
The condition $escaped_backslash is always false.
Loading history...
51
                    $j++;
52
                }
53
                // ... if escaped backslashes: it's really the end of the
54
                // string -> exit the loop
55
                if ($escaped_backslash) {
56
                    $string_start = '';
57
                    $in_string    = false;
58
                    break;
59
                }
60
                // ... else loop
61
62
                $i++; // end if...elseif...else
63
            } // end for
64
        } // end if (in string)
65
66
        // lets skip comments (/*, -- and #)
67
        elseif (('-' == $char && $sql_len > $i + 2 && '-' == $sql[$i + 1] && $sql[$i + 2] <= ' ') || '#' == $char || ('/' == $char && $sql_len > $i + 1 && '*' == $sql[$i + 1])) {
68
            $i = strpos($sql, '/' == $char ? '*/' : "\n", $i);
69
            // didn't we hit end of string?
70
            if (false === $i) {
71
                break;
72
            }
73
            if ('/' == $char) {
74
                $i++;
75
            }
76
        } // We are not in a string, first check for delimiter...
77
        elseif (';' == $char) {
78
            // if delimiter found, add the parsed part to the returned array
79
            $ret[]   = ['query' => substr($sql, 0, $i), 'empty' => $nothing];
80
            $nothing = true;
81
            $sql     = ltrim(substr($sql, min($i + 1, $sql_len)));
82
            $sql_len = strlen($sql);
83
            if ($sql_len !== 0) {
84
                $i = -1;
85
            } else {
86
                // The submited statement(s) end(s) here
87
                return true;
88
            }
89
        } // end else if (is delimiter)
90
91
        // ... then check for start of a string,...
92
        elseif (('"' == $char) || ('\'' == $char) || ('`' == $char)) {
93
            $in_string    = true;
94
            $nothing      = false;
95
            $string_start = $char;
96
        } // end else if (is start of string)
97
98
        elseif ($nothing) {
99
            $nothing = false;
100
        }
101
102
        // loic1: send a fake header each 30 sec. to bypass browser timeout
103
        $time1 = time();
104
        if ($time1 >= $time0 + 30) {
105
            $time0 = $time1;
106
            header('X-pmaPing: Pong');
107
        } // end if
108
    } // end for
109
110
    // add any rest to the returned array
111
    if (!empty($sql) && preg_match('@[^[:space:]]+@', $sql)) {
112
        $ret[] = ['query' => $sql, 'empty' => $nothing];
113
    }
114
115
    return true;
116
} // end of the 'PMA_splitSqlFile()' function
117
118
/**
119
 * Reads (and decompresses) a (compressed) file into a string
120
 *
121
 * @param string   the path to the file
122
 * @param string   the MIME type of the file, if empty MIME type is autodetected
123
 *
124
 * @return  string   the content of the file or
125
 *          boolean  FALSE in case of an error.
126
 * @global  array    the phpMyAdmin configuration
127
 *
128
 */
129
function PMA_readFile($path, $mime = '')
130
{
131
    global $cfg;
132
133
    if (!is_file($path)) {
134
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
135
    }
136
    switch ($mime) {
137
        case '':
138
            if (!$file = fopen($path, 'rb')) {
139
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
140
            }
141
            $test = fread($file, 3);
142
            fclose($file);
143
            if ($test[0] === chr(31) && $test[1] === chr(139)) {
144
                return PMA_readFile($path, 'application/x-gzip');
145
            }
146
            if ('BZh' == $test) {
147
                return PMA_readFile($path, 'application/x-bzip');
148
            }
149
            return PMA_readFile($path, 'text/plain');
150
        case 'text/plain':
151
            if (!$file = fopen($path, 'rb')) {
152
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
153
            }
154
            $content = fread($file, filesize($path));
155
            fclose($file);
156
            break;
157
        case 'application/x-gzip':
158
            if ($cfg['GZipDump'] && function_exists('gzopen')) {
159
                if (!$file = gzopen($path, 'rb')) {
160
                    return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
161
                }
162
                $content = '';
163
                while (!gzeof($file)) {
164
                    $content .= gzgetc($file);
165
                }
166
                gzclose($file);
167
            } else {
168
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
169
            }
170
            break;
171
        case 'application/x-bzip':
172
            if ($cfg['BZipDump'] && function_exists('bzdecompress')) {
173
                if (!$file = fopen($path, 'rb')) {
174
                    return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
175
                }
176
                $content = fread($file, filesize($path));
177
                fclose($file);
178
                $content = bzdecompress($content);
179
            } else {
180
                return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
181
            }
182
            break;
183
        default:
184
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
185
    }
186
    return $content;
187
}
188