| Conditions | 30 |
| Paths | 6 |
| Total Lines | 104 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 12 | function PMA_splitSqlFile(&$ret, $sql, $release) |
||
|
|
|||
| 13 | { |
||
| 14 | $sql = rtrim($sql, "\n\r"); |
||
| 15 | $sql_len = strlen($sql); |
||
| 16 | $char = ''; |
||
| 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; |
||
| 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 |
||
| 188 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.