Conditions | 44 |
Paths | 58 |
Total Lines | 196 |
Code Lines | 133 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
124 | public function tokenize(): self |
||
125 | { |
||
126 | $isLastCharEscape = false; |
||
127 | foreach (str_split($this->input) as $ch) { |
||
128 | switch (true) { |
||
129 | case $this->inSingleQuotedString: |
||
130 | if ($ch === '\\') { |
||
131 | if ($isLastCharEscape) { |
||
132 | $this->stringBuffer .= '\\'; |
||
133 | $isLastCharEscape = false; |
||
134 | } else { |
||
135 | $isLastCharEscape = true; |
||
136 | } |
||
137 | |||
138 | continue 2; |
||
139 | } elseif ($ch === "'") { |
||
140 | if ($isLastCharEscape) { |
||
141 | $this->stringBuffer .= "'"; |
||
142 | $isLastCharEscape = false; |
||
143 | } else { |
||
144 | $this->tokens[] = new Token(Token::STRING, $this->stringBuffer); |
||
145 | $this->inSingleQuotedString = false; |
||
146 | $this->stringBuffer = ''; |
||
147 | } |
||
148 | |||
149 | continue 2; |
||
150 | } |
||
151 | |||
152 | if ($isLastCharEscape) { |
||
153 | $this->stringBuffer .= '\\'; |
||
154 | $isLastCharEscape = false; |
||
155 | } |
||
156 | |||
157 | $this->stringBuffer .= $ch; |
||
158 | |||
159 | continue 2; |
||
160 | |||
161 | case $this->inDoubleQuotedString: |
||
162 | if ($ch === '\\') { |
||
163 | if ($isLastCharEscape) { |
||
164 | $this->stringBuffer .= '\\'; |
||
165 | $isLastCharEscape = false; |
||
166 | } else { |
||
167 | $isLastCharEscape = true; |
||
168 | } |
||
169 | |||
170 | continue 2; |
||
171 | } elseif ($ch === '"') { |
||
172 | if ($isLastCharEscape) { |
||
173 | $this->stringBuffer .= '"'; |
||
174 | $isLastCharEscape = false; |
||
175 | } else { |
||
176 | $this->tokens[] = new Token(Token::STRING, $this->stringBuffer); |
||
177 | $this->inDoubleQuotedString = false; |
||
178 | $this->stringBuffer = ''; |
||
179 | } |
||
180 | |||
181 | continue 2; |
||
182 | } |
||
183 | |||
184 | if ($isLastCharEscape) { |
||
185 | $this->stringBuffer .= '\\'; |
||
186 | $isLastCharEscape = false; |
||
187 | } |
||
188 | |||
189 | $this->stringBuffer .= $ch; |
||
190 | |||
191 | continue 2; |
||
192 | |||
193 | case $ch === '[': |
||
194 | $this->tokens[] = new Token(Token::FUNCTION, 'array'); |
||
195 | $this->allowNegative = true; |
||
196 | $this->tokens[] = new Token(Token::LEFT_PARENTHESIS, ''); |
||
197 | |||
198 | continue 2; |
||
199 | |||
200 | case $ch == ' ' || $ch == "\n" || $ch == "\r" || $ch == "\t": |
||
201 | $this->emptyNumberBufferAsLiteral(); |
||
202 | $this->emptyStringBufferAsVariable(); |
||
203 | $this->tokens[] = new Token(Token::SPACE, ''); |
||
204 | continue 2; |
||
205 | |||
206 | case $this->isNumber($ch): |
||
207 | if ($this->stringBuffer != '') { |
||
208 | $this->stringBuffer .= $ch; |
||
209 | |||
210 | continue 2; |
||
211 | } |
||
212 | $this->numberBuffer .= $ch; |
||
213 | $this->allowNegative = false; |
||
214 | break; |
||
215 | |||
216 | case strtolower($ch) === 'e': |
||
217 | if (strlen($this->numberBuffer) > 0 && strpos($this->numberBuffer, '.') !== false) { |
||
218 | $this->numberBuffer .= 'e'; |
||
219 | $this->allowNegative = false; |
||
220 | |||
221 | break; |
||
222 | } |
||
223 | // no break |
||
224 | // Intentionally fall through |
||
225 | case $this->isAlpha($ch): |
||
226 | if (strlen($this->numberBuffer) > 0) { |
||
227 | $this->emptyNumberBufferAsLiteral(); |
||
228 | $this->tokens[] = new Token(Token::OPERATOR, '*'); |
||
229 | } |
||
230 | $this->allowNegative = false; |
||
231 | $this->stringBuffer .= $ch; |
||
232 | |||
233 | break; |
||
234 | |||
235 | case $ch === '"': |
||
236 | $this->inDoubleQuotedString = true; |
||
237 | |||
238 | continue 2; |
||
239 | |||
240 | case $ch === "'": |
||
241 | $this->inSingleQuotedString = true; |
||
242 | |||
243 | continue 2; |
||
244 | |||
245 | case $this->isDot($ch): |
||
246 | $this->numberBuffer .= $ch; |
||
247 | $this->allowNegative = false; |
||
248 | |||
249 | break; |
||
250 | |||
251 | case $this->isLeftParenthesis($ch): |
||
252 | if ($this->stringBuffer != '') { |
||
253 | $this->tokens[] = new Token(Token::FUNCTION, $this->stringBuffer); |
||
254 | $this->stringBuffer = ''; |
||
255 | } elseif (strlen($this->numberBuffer) > 0) { |
||
256 | $this->emptyNumberBufferAsLiteral(); |
||
257 | $this->tokens[] = new Token(Token::OPERATOR, '*'); |
||
258 | } |
||
259 | $this->allowNegative = true; |
||
260 | $this->tokens[] = new Token(Token::LEFT_PARENTHESIS, ''); |
||
261 | |||
262 | break; |
||
263 | |||
264 | case $this->isRightParenthesis($ch) || $ch === ']': |
||
265 | $this->emptyNumberBufferAsLiteral(); |
||
266 | $this->emptyStringBufferAsVariable(); |
||
267 | $this->allowNegative = false; |
||
268 | $this->tokens[] = new Token(Token::RIGHT_PARENTHESIS, ''); |
||
269 | |||
270 | break; |
||
271 | |||
272 | case $this->isComma($ch): |
||
273 | $this->emptyNumberBufferAsLiteral(); |
||
274 | $this->emptyStringBufferAsVariable(); |
||
275 | $this->allowNegative = true; |
||
276 | $this->tokens[] = new Token(Token::PARAM_SEPARATOR, ''); |
||
277 | |||
278 | break; |
||
279 | |||
280 | default: |
||
281 | // special case for unary operations |
||
282 | if ($ch == '-' || $ch == '+') { |
||
283 | if ($this->allowNegative) { |
||
284 | $this->allowNegative = false; |
||
285 | $this->tokens[] = new Token(Token::OPERATOR, $ch == '-' ? 'uNeg' : 'uPos'); |
||
286 | |||
287 | continue 2; |
||
288 | } |
||
289 | // could be in exponent, in which case negative |
||
290 | // should be added to the number buffer |
||
291 | if ($this->numberBuffer && 'e' == $this->numberBuffer[strlen($this->numberBuffer) - 1]) { |
||
292 | $this->numberBuffer .= $ch; |
||
293 | |||
294 | continue 2; |
||
295 | } |
||
296 | } |
||
297 | $this->emptyNumberBufferAsLiteral(); |
||
298 | $this->emptyStringBufferAsVariable(); |
||
299 | |||
300 | if ($ch !== '$') { |
||
301 | if (count($this->tokens) > 0) { |
||
302 | if (Token::OPERATOR === $this->tokens[count($this->tokens) - 1]->getType()) { |
||
303 | $token = $this->tokens[count($this->tokens) - 1]; |
||
304 | $this->tokens[count($this->tokens) - 1] |
||
305 | ->setValue($token->getValue() . $ch); |
||
306 | } else { |
||
307 | $this->tokens[] = new Token(Token::OPERATOR, $ch); |
||
308 | } |
||
309 | } else { |
||
310 | $this->tokens[] = new Token(Token::OPERATOR, $ch); |
||
311 | } |
||
312 | } |
||
313 | $this->allowNegative = true; |
||
314 | } |
||
315 | } |
||
316 | $this->emptyNumberBufferAsLiteral(); |
||
317 | $this->emptyStringBufferAsVariable(); |
||
318 | |||
319 | return $this; |
||
320 | } |
||
530 |