Total Complexity | 90 |
Total Lines | 469 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Tokenizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Tokenizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
59 | class Tokenizer |
||
60 | { |
||
61 | /** |
||
62 | * List of token |
||
63 | * @var Token[] |
||
64 | */ |
||
65 | protected array $tokens = []; |
||
66 | |||
67 | /** |
||
68 | * The expression to evaluate |
||
69 | * @var string |
||
70 | */ |
||
71 | protected string $input = ''; |
||
72 | |||
73 | /** |
||
74 | * The number buffering |
||
75 | * @var string |
||
76 | */ |
||
77 | protected string $numberBuffer = ''; |
||
78 | |||
79 | /** |
||
80 | * The string buffering |
||
81 | * @var string |
||
82 | */ |
||
83 | protected string $stringBuffer = ''; |
||
84 | |||
85 | /** |
||
86 | * Whether to allow negative or not |
||
87 | * @var bool |
||
88 | */ |
||
89 | protected bool $allowNegative = true; |
||
90 | |||
91 | /** |
||
92 | * The list of operator |
||
93 | * @var array<string, Operator> |
||
94 | */ |
||
95 | protected array $operators = []; |
||
96 | |||
97 | /** |
||
98 | * Whether the current pointer is inside single quoted string |
||
99 | * @var bool |
||
100 | */ |
||
101 | protected bool $inSingleQuotedString = false; |
||
102 | |||
103 | /** |
||
104 | * Whether the current pointer is inside double quoted string |
||
105 | * @var bool |
||
106 | */ |
||
107 | protected bool $inDoubleQuotedString = false; |
||
108 | |||
109 | /** |
||
110 | * Create new instance |
||
111 | * @param string $input |
||
112 | * @param array<string, Operator> $operators |
||
113 | */ |
||
114 | public function __construct(string $input, array $operators) |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Produce the tokens |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function tokenize(): self |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Build the reverse polish notation |
||
324 | * @return Token[] |
||
325 | */ |
||
326 | public function buildReversePolishNotation(): array |
||
327 | { |
||
328 | $tokens = []; |
||
329 | /** @var SplStack<Token> $stack */ |
||
330 | $stack = new SplStack(); |
||
331 | |||
332 | /** @var SplStack<int> $paramCounter */ |
||
333 | $paramCounter = new SplStack(); |
||
334 | foreach ($this->tokens as $token) { |
||
335 | switch ($token->getType()) { |
||
336 | case Token::LITERAL: |
||
337 | case Token::VARIABLE: |
||
338 | case Token::STRING: |
||
339 | $tokens[] = $token; |
||
340 | |||
341 | if ($paramCounter->count() > 0 && $paramCounter->top() === 0) { |
||
342 | $paramCounter->push($paramCounter->pop() + 1); |
||
343 | } |
||
344 | |||
345 | break; |
||
346 | |||
347 | case Token::FUNCTION: |
||
348 | if ($paramCounter->count() > 0 && $paramCounter->top() === 0) { |
||
349 | $paramCounter->push($paramCounter->pop() + 1); |
||
350 | } |
||
351 | $stack->push($token); |
||
352 | $paramCounter->push(0); |
||
353 | |||
354 | break; |
||
355 | |||
356 | case Token::LEFT_PARENTHESIS: |
||
357 | $stack->push($token); |
||
358 | |||
359 | break; |
||
360 | |||
361 | case Token::PARAM_SEPARATOR: |
||
362 | while (Token::LEFT_PARENTHESIS !== $stack->top()->getType()) { |
||
363 | if ($stack->count() === 0) { |
||
364 | throw new IncorrectBracketsException('Incorrect brackets'); |
||
365 | } |
||
366 | $tokens[] = $stack->pop(); |
||
367 | } |
||
368 | |||
369 | $paramCounter->push($paramCounter->pop() + 1); |
||
370 | break; |
||
371 | |||
372 | case Token::OPERATOR: |
||
373 | if (!array_key_exists($token->getValue(), $this->operators)) { |
||
374 | throw new UnknownOperatorException(sprintf( |
||
375 | 'Unknown operator [%s]', |
||
376 | $token->getValue() |
||
377 | )); |
||
378 | } |
||
379 | $op1 = $this->operators[$token->getValue()]; |
||
380 | while ($stack->count() > 0 && Token::OPERATOR === $stack->top()->getType()) { |
||
381 | if (!array_key_exists($stack->top()->getValue(), $this->operators)) { |
||
382 | throw new UnknownOperatorException(sprintf( |
||
383 | 'Unknown operator [%s]', |
||
384 | $stack->top()->getValue() |
||
385 | )); |
||
386 | } |
||
387 | $op2 = $this->operators[$stack->top()->getValue()]; |
||
388 | if ($op2->getPriority() >= $op1->getPriority()) { |
||
389 | $tokens[] = $stack->pop(); |
||
390 | |||
391 | continue; |
||
392 | } |
||
393 | |||
394 | break; |
||
395 | } |
||
396 | $stack->push($token); |
||
397 | break; |
||
398 | |||
399 | case Token::RIGHT_PARENTHESIS: |
||
400 | while (true) { |
||
401 | try { |
||
402 | $ctoken = $stack->pop(); |
||
403 | if (Token::LEFT_PARENTHESIS === $ctoken->getType()) { |
||
404 | break; |
||
405 | } |
||
406 | $tokens[] = $ctoken; |
||
407 | } catch (RuntimeException $ex) { |
||
408 | throw new IncorrectBracketsException('Incorrect brackets'); |
||
409 | } |
||
410 | } |
||
411 | if ($stack->count() > 0 && Token::FUNCTION === $stack->top()->getType()) { |
||
412 | /** @var Token $funcToken */ |
||
413 | $funcToken = $stack->pop(); |
||
414 | $funcToken->setParamCount($paramCounter->pop()); |
||
415 | $tokens[] = $funcToken; |
||
416 | } |
||
417 | break; |
||
418 | case Token::SPACE: |
||
419 | // do nothing |
||
420 | } |
||
421 | } |
||
422 | |||
423 | while ($stack->count() !== 0) { |
||
424 | if ( |
||
425 | in_array( |
||
426 | $stack->top()->getType(), |
||
427 | [Token::LEFT_PARENTHESIS, Token::RIGHT_PARENTHESIS] |
||
428 | ) |
||
429 | ) { |
||
430 | throw new IncorrectBracketsException('Incorrect brackets'); |
||
431 | } |
||
432 | |||
433 | if (Token::SPACE === $stack->top()->getType()) { |
||
434 | $stack->pop(); |
||
435 | |||
436 | continue; |
||
437 | } |
||
438 | $tokens[] = $stack->pop(); |
||
439 | } |
||
440 | |||
441 | return $tokens; |
||
442 | } |
||
443 | |||
444 | /** |
||
445 | * Put the current number buffer content to token |
||
446 | * as literal |
||
447 | * @return void |
||
448 | */ |
||
449 | protected function emptyNumberBufferAsLiteral(): void |
||
450 | { |
||
451 | if (strlen($this->numberBuffer) > 0) { |
||
452 | $this->tokens[] = new Token(Token::LITERAL, $this->numberBuffer); |
||
453 | $this->numberBuffer = ''; |
||
454 | } |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Put the current string buffer content to token |
||
459 | * as variable |
||
460 | * @return void |
||
461 | */ |
||
462 | protected function emptyStringBufferAsVariable(): void |
||
463 | { |
||
464 | if (strlen($this->stringBuffer) > 0) { |
||
465 | $this->tokens[] = new Token(Token::VARIABLE, $this->stringBuffer); |
||
466 | $this->stringBuffer = ''; |
||
467 | } |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Whether the give argument is number |
||
472 | * @param string $chr |
||
473 | * @return bool |
||
474 | */ |
||
475 | protected function isNumber(string $chr): bool |
||
476 | { |
||
477 | return $chr >= '0' && $chr <= '9'; |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * Whether the give argument is alphabetic |
||
482 | * @param string $chr |
||
483 | * @return bool |
||
484 | */ |
||
485 | protected function isAlpha(string $chr): bool |
||
486 | { |
||
487 | return $chr >= 'a' && $chr <= 'z' || $chr >= 'A' && $chr <= 'Z' || $chr === '_'; |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Whether the give argument is dot |
||
492 | * @param string $chr |
||
493 | * @return bool |
||
494 | */ |
||
495 | protected function isDot(string $chr): bool |
||
496 | { |
||
497 | return $chr === '.'; |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Whether the give argument is left parenthesis |
||
502 | * @param string $chr |
||
503 | * @return bool |
||
504 | */ |
||
505 | protected function isLeftParenthesis(string $chr): bool |
||
506 | { |
||
507 | return $chr === '('; |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * Whether the give argument is right parenthesis |
||
512 | * @param string $chr |
||
513 | * @return bool |
||
514 | */ |
||
515 | protected function isRightParenthesis(string $chr): bool |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Whether the give argument is comma |
||
522 | * @param string $chr |
||
523 | * @return bool |
||
524 | */ |
||
525 | protected function isComma(string $chr): bool |
||
528 | } |
||
529 | } |
||
530 |