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