Conditions | 102 |
Paths | > 20000 |
Total Lines | 467 |
Code Lines | 294 |
Lines | 83 |
Ratio | 17.77 % |
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 |
||
151 | private function parseToTokens() |
||
152 | { |
||
153 | // No attempt is made to verify formulas; assumes formulas are derived from Excel, where |
||
154 | // they can only exist if valid; stack overflows/underflows sunk as nulls without exceptions. |
||
155 | |||
156 | // Check if the formula has a valid starting = |
||
157 | $formulaLength = strlen($this->formula); |
||
158 | if ($formulaLength < 2 || $this->formula{0} != '=') { |
||
159 | return; |
||
160 | } |
||
161 | |||
162 | // Helper variables |
||
163 | $tokens1 = $tokens2 = $stack = array(); |
||
164 | $inString = $inPath = $inRange = $inError = false; |
||
165 | $token = $previousToken = $nextToken = null; |
||
166 | |||
167 | $index = 1; |
||
168 | $value = ''; |
||
169 | |||
170 | $ERRORS = array("#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A"); |
||
171 | $COMPARATORS_MULTI = array(">=", "<=", "<>"); |
||
172 | |||
173 | while ($index < $formulaLength) { |
||
174 | // state-dependent character evaluation (order is important) |
||
175 | |||
176 | // double-quoted strings |
||
177 | // embeds are doubled |
||
178 | // end marks token |
||
179 | if ($inString) { |
||
180 | if ($this->formula{$index} == self::QUOTE_DOUBLE) { |
||
181 | if ((($index + 2) <= $formulaLength) && ($this->formula{$index + 1} == self::QUOTE_DOUBLE)) { |
||
182 | $value .= self::QUOTE_DOUBLE; |
||
183 | ++$index; |
||
184 | } else { |
||
185 | $inString = false; |
||
186 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND, FormulaToken::TOKEN_SUBTYPE_TEXT); |
||
187 | $value = ""; |
||
188 | } |
||
189 | } else { |
||
190 | $value .= $this->formula{$index}; |
||
191 | } |
||
192 | ++$index; |
||
193 | continue; |
||
194 | } |
||
195 | |||
196 | // single-quoted strings (links) |
||
197 | // embeds are double |
||
198 | // end does not mark a token |
||
199 | if ($inPath) { |
||
200 | if ($this->formula{$index} == self::QUOTE_SINGLE) { |
||
201 | if ((($index + 2) <= $formulaLength) && ($this->formula{$index + 1} == self::QUOTE_SINGLE)) { |
||
202 | $value .= self::QUOTE_SINGLE; |
||
203 | ++$index; |
||
204 | } else { |
||
205 | $inPath = false; |
||
206 | } |
||
207 | } else { |
||
208 | $value .= $this->formula{$index}; |
||
209 | } |
||
210 | ++$index; |
||
211 | continue; |
||
212 | } |
||
213 | |||
214 | // bracked strings (R1C1 range index or linked workbook name) |
||
215 | // no embeds (changed to "()" by Excel) |
||
216 | // end does not mark a token |
||
217 | if ($inRange) { |
||
218 | if ($this->formula{$index} == self::BRACKET_CLOSE) { |
||
219 | $inRange = false; |
||
220 | } |
||
221 | $value .= $this->formula{$index}; |
||
222 | ++$index; |
||
223 | continue; |
||
224 | } |
||
225 | |||
226 | // error values |
||
227 | // end marks a token, determined from absolute list of values |
||
228 | if ($inError) { |
||
229 | $value .= $this->formula{$index}; |
||
230 | ++$index; |
||
231 | if (in_array($value, $ERRORS)) { |
||
232 | $inError = false; |
||
233 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND, FormulaToken::TOKEN_SUBTYPE_ERROR); |
||
234 | $value = ""; |
||
235 | } |
||
236 | continue; |
||
237 | } |
||
238 | |||
239 | // scientific notation check |
||
240 | if (strpos(self::OPERATORS_SN, $this->formula{$index}) !== false) { |
||
241 | if (strlen($value) > 1) { |
||
242 | if (preg_match("/^[1-9]{1}(\.[0-9]+)?E{1}$/", $this->formula{$index}) != 0) { |
||
243 | $value .= $this->formula{$index}; |
||
244 | ++$index; |
||
245 | continue; |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | // independent character evaluation (order not important) |
||
251 | |||
252 | // establish state-dependent character evaluations |
||
253 | View Code Duplication | if ($this->formula{$index} == self::QUOTE_DOUBLE) { |
|
254 | if (strlen($value > 0)) { |
||
255 | // unexpected |
||
256 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_UNKNOWN); |
||
257 | $value = ""; |
||
258 | } |
||
259 | $inString = true; |
||
260 | ++$index; |
||
261 | continue; |
||
262 | } |
||
263 | |||
264 | View Code Duplication | if ($this->formula{$index} == self::QUOTE_SINGLE) { |
|
265 | if (strlen($value) > 0) { |
||
266 | // unexpected |
||
267 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_UNKNOWN); |
||
268 | $value = ""; |
||
269 | } |
||
270 | $inPath = true; |
||
271 | ++$index; |
||
272 | continue; |
||
273 | } |
||
274 | |||
275 | if ($this->formula{$index} == self::BRACKET_OPEN) { |
||
276 | $inRange = true; |
||
277 | $value .= self::BRACKET_OPEN; |
||
278 | ++$index; |
||
279 | continue; |
||
280 | } |
||
281 | |||
282 | View Code Duplication | if ($this->formula{$index} == self::ERROR_START) { |
|
283 | if (strlen($value) > 0) { |
||
284 | // unexpected |
||
285 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_UNKNOWN); |
||
286 | $value = ""; |
||
287 | } |
||
288 | $inError = true; |
||
289 | $value .= self::ERROR_START; |
||
290 | ++$index; |
||
291 | continue; |
||
292 | } |
||
293 | |||
294 | // mark start and end of arrays and array rows |
||
295 | if ($this->formula{$index} == self::BRACE_OPEN) { |
||
296 | if (strlen($value) > 0) { |
||
297 | // unexpected |
||
298 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_UNKNOWN); |
||
299 | $value = ""; |
||
300 | } |
||
301 | |||
302 | $tmp = new FormulaToken("ARRAY", FormulaToken::TOKEN_TYPE_FUNCTION, FormulaToken::TOKEN_SUBTYPE_START); |
||
303 | $tokens1[] = $tmp; |
||
304 | $stack[] = clone $tmp; |
||
305 | |||
306 | $tmp = new FormulaToken("ARRAYROW", FormulaToken::TOKEN_TYPE_FUNCTION, FormulaToken::TOKEN_SUBTYPE_START); |
||
307 | $tokens1[] = $tmp; |
||
308 | $stack[] = clone $tmp; |
||
309 | |||
310 | ++$index; |
||
311 | continue; |
||
312 | } |
||
313 | |||
314 | if ($this->formula{$index} == self::SEMICOLON) { |
||
315 | if (strlen($value) > 0) { |
||
316 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND); |
||
317 | $value = ""; |
||
318 | } |
||
319 | |||
320 | $tmp = array_pop($stack); |
||
321 | $tmp->setValue(""); |
||
322 | $tmp->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_STOP); |
||
323 | $tokens1[] = $tmp; |
||
324 | |||
325 | $tmp = new FormulaToken(",", FormulaToken::TOKEN_TYPE_ARGUMENT); |
||
326 | $tokens1[] = $tmp; |
||
327 | |||
328 | $tmp = new FormulaToken("ARRAYROW", FormulaToken::TOKEN_TYPE_FUNCTION, FormulaToken::TOKEN_SUBTYPE_START); |
||
329 | $tokens1[] = $tmp; |
||
330 | $stack[] = clone $tmp; |
||
331 | |||
332 | ++$index; |
||
333 | continue; |
||
334 | } |
||
335 | |||
336 | if ($this->formula{$index} == self::BRACE_CLOSE) { |
||
337 | if (strlen($value) > 0) { |
||
338 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND); |
||
339 | $value = ""; |
||
340 | } |
||
341 | |||
342 | $tmp = array_pop($stack); |
||
343 | $tmp->setValue(""); |
||
344 | $tmp->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_STOP); |
||
345 | $tokens1[] = $tmp; |
||
346 | |||
347 | $tmp = array_pop($stack); |
||
348 | $tmp->setValue(""); |
||
349 | $tmp->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_STOP); |
||
350 | $tokens1[] = $tmp; |
||
351 | |||
352 | ++$index; |
||
353 | continue; |
||
354 | } |
||
355 | |||
356 | // trim white-space |
||
357 | if ($this->formula{$index} == self::WHITESPACE) { |
||
358 | if (strlen($value) > 0) { |
||
359 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND); |
||
360 | $value = ""; |
||
361 | } |
||
362 | $tokens1[] = new FormulaToken("", FormulaToken::TOKEN_TYPE_WHITESPACE); |
||
363 | ++$index; |
||
364 | while (($this->formula{$index} == self::WHITESPACE) && ($index < $formulaLength)) { |
||
365 | ++$index; |
||
366 | } |
||
367 | continue; |
||
368 | } |
||
369 | |||
370 | // multi-character comparators |
||
371 | if (($index + 2) <= $formulaLength) { |
||
372 | if (in_array(substr($this->formula, $index, 2), $COMPARATORS_MULTI)) { |
||
373 | if (strlen($value) > 0) { |
||
374 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND); |
||
375 | $value = ""; |
||
376 | } |
||
377 | $tokens1[] = new FormulaToken(substr($this->formula, $index, 2), FormulaToken::TOKEN_TYPE_OPERATORINFIX, FormulaToken::TOKEN_SUBTYPE_LOGICAL); |
||
378 | $index += 2; |
||
379 | continue; |
||
380 | } |
||
381 | } |
||
382 | |||
383 | // standard infix operators |
||
384 | View Code Duplication | if (strpos(self::OPERATORS_INFIX, $this->formula{$index}) !== false) { |
|
385 | if (strlen($value) > 0) { |
||
386 | $tokens1[] =new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND); |
||
387 | $value = ""; |
||
388 | } |
||
389 | $tokens1[] = new FormulaToken($this->formula{$index}, FormulaToken::TOKEN_TYPE_OPERATORINFIX); |
||
390 | ++$index; |
||
391 | continue; |
||
392 | } |
||
393 | |||
394 | // standard postfix operators (only one) |
||
395 | View Code Duplication | if (strpos(self::OPERATORS_POSTFIX, $this->formula{$index}) !== false) { |
|
396 | if (strlen($value) > 0) { |
||
397 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND); |
||
398 | $value = ""; |
||
399 | } |
||
400 | $tokens1[] = new FormulaToken($this->formula{$index}, FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX); |
||
401 | ++$index; |
||
402 | continue; |
||
403 | } |
||
404 | |||
405 | // start subexpression or function |
||
406 | if ($this->formula{$index} == self::PAREN_OPEN) { |
||
407 | if (strlen($value) > 0) { |
||
408 | $tmp = new FormulaToken($value, FormulaToken::TOKEN_TYPE_FUNCTION, FormulaToken::TOKEN_SUBTYPE_START); |
||
409 | $tokens1[] = $tmp; |
||
410 | $stack[] = clone $tmp; |
||
411 | $value = ""; |
||
412 | } else { |
||
413 | $tmp = new FormulaToken("", FormulaToken::TOKEN_TYPE_SUBEXPRESSION, FormulaToken::TOKEN_SUBTYPE_START); |
||
414 | $tokens1[] = $tmp; |
||
415 | $stack[] = clone $tmp; |
||
416 | } |
||
417 | ++$index; |
||
418 | continue; |
||
419 | } |
||
420 | |||
421 | // function, subexpression, or array parameters, or operand unions |
||
422 | if ($this->formula{$index} == self::COMMA) { |
||
423 | if (strlen($value) > 0) { |
||
424 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND); |
||
425 | $value = ""; |
||
426 | } |
||
427 | |||
428 | $tmp = array_pop($stack); |
||
429 | $tmp->setValue(""); |
||
430 | $tmp->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_STOP); |
||
431 | $stack[] = $tmp; |
||
432 | |||
433 | if ($tmp->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) { |
||
434 | $tokens1[] = new FormulaToken(",", FormulaToken::TOKEN_TYPE_OPERATORINFIX, FormulaToken::TOKEN_SUBTYPE_UNION); |
||
435 | } else { |
||
436 | $tokens1[] = new FormulaToken(",", FormulaToken::TOKEN_TYPE_ARGUMENT); |
||
437 | } |
||
438 | ++$index; |
||
439 | continue; |
||
440 | } |
||
441 | |||
442 | // stop subexpression |
||
443 | if ($this->formula{$index} == self::PAREN_CLOSE) { |
||
444 | if (strlen($value) > 0) { |
||
445 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND); |
||
446 | $value = ""; |
||
447 | } |
||
448 | |||
449 | $tmp = array_pop($stack); |
||
450 | $tmp->setValue(""); |
||
451 | $tmp->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_STOP); |
||
452 | $tokens1[] = $tmp; |
||
453 | |||
454 | ++$index; |
||
455 | continue; |
||
456 | } |
||
457 | |||
458 | // token accumulation |
||
459 | $value .= $this->formula{$index}; |
||
460 | ++$index; |
||
461 | } |
||
462 | |||
463 | // dump remaining accumulation |
||
464 | if (strlen($value) > 0) { |
||
465 | $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERAND); |
||
466 | } |
||
467 | |||
468 | // move tokenList to new set, excluding unnecessary white-space tokens and converting necessary ones to intersections |
||
469 | $tokenCount = count($tokens1); |
||
470 | for ($i = 0; $i < $tokenCount; ++$i) { |
||
471 | $token = $tokens1[$i]; |
||
472 | if (isset($tokens1[$i - 1])) { |
||
473 | $previousToken = $tokens1[$i - 1]; |
||
474 | } else { |
||
475 | $previousToken = null; |
||
476 | } |
||
477 | if (isset($tokens1[$i + 1])) { |
||
478 | $nextToken = $tokens1[$i + 1]; |
||
479 | } else { |
||
480 | $nextToken = null; |
||
481 | } |
||
482 | |||
483 | if (is_null($token)) { |
||
484 | continue; |
||
485 | } |
||
486 | |||
487 | if ($token->getTokenType() != FormulaToken::TOKEN_TYPE_WHITESPACE) { |
||
488 | $tokens2[] = $token; |
||
489 | continue; |
||
490 | } |
||
491 | |||
492 | if (is_null($previousToken)) { |
||
493 | continue; |
||
494 | } |
||
495 | |||
496 | View Code Duplication | if (! ( |
|
497 | (($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) && ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) || |
||
498 | (($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) || |
||
499 | ($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND) |
||
500 | ) ) { |
||
501 | continue; |
||
502 | } |
||
503 | |||
504 | if (is_null($nextToken)) { |
||
505 | continue; |
||
506 | } |
||
507 | |||
508 | View Code Duplication | if (! ( |
|
509 | (($nextToken->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) && ($nextToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_START)) || |
||
510 | (($nextToken->getTokenType() == FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && ($nextToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_START)) || |
||
511 | ($nextToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND) |
||
512 | ) ) { |
||
513 | continue; |
||
514 | } |
||
515 | |||
516 | $tokens2[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_OPERATORINFIX, FormulaToken::TOKEN_SUBTYPE_INTERSECTION); |
||
517 | } |
||
518 | |||
519 | // move tokens to final list, switching infix "-" operators to prefix when appropriate, switching infix "+" operators |
||
520 | // to noop when appropriate, identifying operand and infix-operator subtypes, and pulling "@" from function names |
||
521 | $this->tokens = array(); |
||
522 | |||
523 | $tokenCount = count($tokens2); |
||
524 | for ($i = 0; $i < $tokenCount; ++$i) { |
||
525 | $token = $tokens2[$i]; |
||
526 | if (isset($tokens2[$i - 1])) { |
||
527 | $previousToken = $tokens2[$i - 1]; |
||
528 | } else { |
||
529 | $previousToken = null; |
||
530 | } |
||
531 | if (isset($tokens2[$i + 1])) { |
||
532 | $nextToken = $tokens2[$i + 1]; |
||
533 | } else { |
||
534 | $nextToken = null; |
||
535 | } |
||
536 | |||
537 | if (is_null($token)) { |
||
538 | continue; |
||
539 | } |
||
540 | |||
541 | if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getValue() == "-") { |
||
542 | if ($i == 0) { |
||
543 | $token->setTokenType(FormulaToken::TOKEN_TYPE_OPERATORPREFIX); |
||
544 | View Code Duplication | } elseif ((($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) && |
|
545 | ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) || |
||
546 | (($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && |
||
547 | ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) || |
||
548 | ($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX) || |
||
549 | ($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND)) { |
||
550 | $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_MATH); |
||
551 | } else { |
||
552 | $token->setTokenType(FormulaToken::TOKEN_TYPE_OPERATORPREFIX); |
||
553 | } |
||
554 | |||
555 | $this->tokens[] = $token; |
||
556 | continue; |
||
557 | } |
||
558 | |||
559 | if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORINFIX && $token->getValue() == "+") { |
||
560 | View Code Duplication | if ($i == 0) { |
|
561 | continue; |
||
562 | } elseif ((($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) && |
||
563 | ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) || |
||
564 | (($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_SUBEXPRESSION) && |
||
565 | ($previousToken->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_STOP)) || |
||
566 | ($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORPOSTFIX) || |
||
567 | ($previousToken->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND)) { |
||
568 | $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_MATH); |
||
569 | } else { |
||
570 | continue; |
||
571 | } |
||
572 | |||
573 | $this->tokens[] = $token; |
||
574 | continue; |
||
575 | } |
||
576 | |||
577 | if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_OPERATORINFIX && |
||
578 | $token->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_NOTHING) { |
||
579 | if (strpos("<>=", substr($token->getValue(), 0, 1)) !== false) { |
||
580 | $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_LOGICAL); |
||
581 | } elseif ($token->getValue() == "&") { |
||
582 | $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_CONCATENATION); |
||
583 | } else { |
||
584 | $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_MATH); |
||
585 | } |
||
586 | |||
587 | $this->tokens[] = $token; |
||
588 | continue; |
||
589 | } |
||
590 | |||
591 | if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND && |
||
592 | $token->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_NOTHING) { |
||
593 | if (!is_numeric($token->getValue())) { |
||
594 | if (strtoupper($token->getValue()) == "TRUE" || strtoupper($token->getValue() == "FALSE")) { |
||
595 | $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_LOGICAL); |
||
596 | } else { |
||
597 | $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_RANGE); |
||
598 | } |
||
599 | } else { |
||
600 | $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_NUMBER); |
||
601 | } |
||
602 | |||
603 | $this->tokens[] = $token; |
||
604 | continue; |
||
605 | } |
||
606 | |||
607 | if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) { |
||
608 | if (strlen($token->getValue() > 0)) { |
||
609 | if (substr($token->getValue(), 0, 1) == "@") { |
||
610 | $token->setValue(substr($token->getValue(), 1)); |
||
611 | } |
||
612 | } |
||
613 | } |
||
614 | |||
615 | $this->tokens[] = $token; |
||
616 | } |
||
617 | } |
||
618 | } |
||
619 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.