| Conditions | 11 |
| Paths | 7 |
| Total Lines | 58 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 namespace XoopsModules\Protector; |
||
| 60 | public function separateStringsInSQL($sql) |
||
| 61 | { |
||
| 62 | $sql = trim($sql); |
||
| 63 | $sql_len = strlen($sql); |
||
| 64 | $char = ''; |
||
| 65 | $string_start = ''; |
||
| 66 | $in_string = false; |
||
| 67 | $sql_wo_string = ''; |
||
| 68 | $strings = array(); |
||
| 69 | $current_string = ''; |
||
| 70 | |||
| 71 | for ($i = 0; $i < $sql_len; ++$i) { |
||
| 72 | $char = $sql[$i]; |
||
| 73 | if ($in_string) { |
||
| 74 | while (1) { |
||
| 75 | $new_i = strpos($sql, $string_start, $i); |
||
| 76 | $current_string .= substr($sql, $i, $new_i - $i + 1); |
||
| 77 | $i = $new_i; |
||
| 78 | if ($i === false) { |
||
| 79 | break 2; |
||
| 80 | } elseif (/* $string_start == '`' || */ |
||
| 81 | $sql[$i - 1] !== '\\' |
||
| 82 | ) { |
||
| 83 | $string_start = ''; |
||
| 84 | $in_string = false; |
||
| 85 | $strings[] = $current_string; |
||
| 86 | break; |
||
| 87 | } else { |
||
| 88 | $j = 2; |
||
| 89 | $escaped_backslash = false; |
||
| 90 | while ($i - $j > 0 && $sql[$i - $j] === '\\') { |
||
| 91 | $escaped_backslash = !$escaped_backslash; |
||
| 92 | ++$j; |
||
| 93 | } |
||
| 94 | if ($escaped_backslash) { |
||
| 95 | $string_start = ''; |
||
| 96 | $in_string = false; |
||
| 97 | $strings[] = $current_string; |
||
| 98 | break; |
||
| 99 | } else { |
||
| 100 | ++$i; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } elseif ($char === '"' || $char === "'") { // dare to ignore `` |
||
| 105 | $in_string = true; |
||
| 106 | $string_start = $char; |
||
| 107 | $current_string = $char; |
||
| 108 | } else { |
||
| 109 | $sql_wo_string .= $char; |
||
| 110 | } |
||
| 111 | // dare to ignore comment |
||
| 112 | // because unescaped ' or " have been already checked in stage1 |
||
| 113 | } |
||
| 114 | |||
| 115 | return array( |
||
| 116 | $sql_wo_string, |
||
| 117 | $strings, |
||
| 118 | ); |
||
| 196 |