Conditions | 25 |
Paths | 25 |
Total Lines | 107 |
Code Lines | 59 |
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 |
||
45 | public static function split($command) |
||
46 | { |
||
47 | |||
48 | // whitespace characters count as argument separators |
||
49 | static $ws = array( |
||
50 | ' ', |
||
51 | "\r", |
||
52 | "\n", |
||
53 | "\t", |
||
54 | "\v", |
||
55 | ); |
||
56 | |||
57 | $i = 0; |
||
58 | $args = array(); |
||
59 | |||
60 | while (true) { |
||
61 | // skip all whitespace characters |
||
62 | for (; isset($command[$i]) && in_array($command[$i], $ws); ++$i) { |
||
63 | } |
||
64 | |||
65 | // command string ended |
||
66 | if (!isset($command[$i])) { |
||
67 | break; |
||
68 | } |
||
69 | |||
70 | $inQuote = null; |
||
71 | $quotePosition = 0; |
||
72 | $argument = ''; |
||
73 | $part = ''; |
||
74 | |||
75 | // read a single argument |
||
76 | for (; isset($command[$i]); ++$i) { |
||
77 | $c = $command[$i]; |
||
78 | |||
79 | if ($inQuote === "'") { |
||
80 | // we're within a 'single quoted' string |
||
81 | if ($c === '\\' && isset($command[$i + 1]) && ($command[$i + 1] === "'" || $command[$i + 1] === '\\')) { |
||
82 | // escaped single quote or backslash ends up as char in argument |
||
83 | $part .= $command[++$i]; |
||
84 | continue; |
||
85 | } elseif ($c === "'") { |
||
86 | // single quote ends |
||
87 | $inQuote = null; |
||
88 | $argument .= $part; |
||
89 | $part = ''; |
||
90 | continue; |
||
91 | } |
||
92 | } else { |
||
93 | // we're not within any quotes or within a "double quoted" string |
||
94 | if ($c === '\\' && isset($command[$i + 1])) { |
||
95 | if ($command[$i + 1] === 'u') { |
||
96 | // this looks like a unicode escape sequence |
||
97 | // use JSON parser to interpret this |
||
98 | $c = json_decode('"' . substr($command, $i, 6) . '"'); |
||
99 | if ($c !== null) { |
||
100 | // on success => use interpreted and skip sequence |
||
101 | $argument .= stripcslashes($part) . $c; |
||
102 | $part = ''; |
||
103 | $i += 5; |
||
104 | continue; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | // escaped characters will be interpreted when part is complete |
||
109 | $part .= $command[$i] . $command[$i + 1]; |
||
110 | ++$i; |
||
111 | continue; |
||
112 | } elseif ($inQuote === '"' && $c === '"') { |
||
113 | // double quote ends |
||
114 | $inQuote = null; |
||
115 | |||
116 | // previous double quoted part should be interpreted |
||
117 | $argument .= stripcslashes($part); |
||
118 | $part = ''; |
||
119 | continue; |
||
120 | } elseif ($inQuote === null && ($c === '"' || $c === "'")) { |
||
121 | // start of quotes found |
||
122 | $inQuote = $c; |
||
123 | $quotePosition = $i; |
||
124 | |||
125 | // previous unquoted part should be interpreted |
||
126 | $argument .= stripcslashes($part); |
||
127 | $part = ''; |
||
128 | continue; |
||
129 | } elseif ($inQuote === null && in_array($c, $ws)) { |
||
130 | // whitespace character terminates unquoted argument |
||
131 | break; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | $part .= $c; |
||
136 | } |
||
137 | |||
138 | // end of argument reached. Still in quotes is a parse error. |
||
139 | if ($inQuote !== null) { |
||
140 | throw new UnclosedQuotesException($inQuote, $quotePosition); |
||
141 | } |
||
142 | |||
143 | // add remaining part to current argument |
||
144 | if ($part !== '') { |
||
145 | $argument .= stripcslashes($part); |
||
146 | } |
||
147 | |||
148 | $args []= $argument; |
||
149 | } |
||
150 | |||
151 | return $args; |
||
152 | } |
||
154 |