Completed
Push — master ( 2a128c...18787f )
by Alexey
02:20
created

Lexer::parse()   D

Complexity

Conditions 27
Paths 20

Size

Total Lines 109
Code Lines 71

Duplication

Lines 31
Ratio 28.44 %

Code Coverage

Tests 76
CRAP Score 27

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 31
loc 109
ccs 76
cts 76
cp 1
rs 4.509
cc 27
eloc 71
nc 20
nop 1
crap 27

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * ExpressionParser.php
5
 *
6
 * @date 28.03.2015 1:03:37
7
 * @copyright Sklyarov Alexey <[email protected]>
8
 */
9
10
namespace Sufir\Calc;
11
12
use InvalidArgumentException;
13
use Sufir\Calc\Token;
14
use Sufir\Calc\Token\TokenFactory;
15
16
/**
17
 * ExpressionParser
18
 *
19
 * Description of ExpressionParser
20
 *
21
 * @todo Multibyte encoding support.
22
 * @author Sklyarov Alexey <[email protected]>
23
 * @package Sufir\Calc
24
 */
25
class Lexer
26
{
27
    /**
28
     * Разбивает переданную строку на токены
29
     *
30
     * @param string $expr
31
     * @return Token[]
32
     */
33 15
    public function parse($expr)
34
    {
35 15
        if (!is_string($expr) || strlen($expr) < 1) {
36 2
            throw new InvalidArgumentException('Неверное выражение!');
37
        }
38
39 13
        $stack = array();
40 13
        $tokenFactory = new TokenFactory();
41 13
        $lastToken = '';
42 13
        $lastTokenType = null;
43
44 13
        for ($i = 0; $i < mb_strlen($expr); $i++) {
45 13
            $char = $expr[$i];
46
47
            // Скобки
48 13
            if ($char === '(' || $char === ')') {
49 8
                if ($lastTokenType) {
50 8
                    $stack[] = $tokenFactory->create($lastTokenType, $lastToken);
51 8
                }
52
53 8
                $lastTokenType = Token::TYPE_BRACKET;
54 8
                $lastToken = $char;
55
56
            // Разделители параметров функции
57 13
            } elseif ($char === ',') {
58 3
                if ($lastTokenType) {
59 3
                    $stack[] = $tokenFactory->create($lastTokenType, $lastToken);
60 3
                }
61
62 3
                $lastTokenType = Token::TYPE_DELIMITER;
63 3
                $lastToken = $char;
64
65 13
            } elseif ($char === '-') {
66
                // Если предыдущий токен не число и не закрывающая скобка,
67
                // то минус означает отрицательное число
68 5
                if (!$lastTokenType) {
69 2
                    $lastTokenType = Token::TYPE_NUMBER;
70 2
                } elseif (
71
                    $lastTokenType === Token::TYPE_NUMBER
72 5
                    || $lastToken === ')'
73 5
                ) {
74 5
                    $stack[] = $tokenFactory->create($lastTokenType, $lastToken);
75 5
                    $lastTokenType = Token::TYPE_OPERATOR;
76 5
                } else {
77 2
                    $stack[] = $tokenFactory->create($lastTokenType, $lastToken);
78 2
                    $lastTokenType = Token::TYPE_NUMBER;
79
                }
80
81 5
                $lastToken = $char;
82
83 13
            } elseif (preg_match("/[\+\*\/\^]+/i", $char)) {
84 10
                if ($lastTokenType) {
85 10
                    $stack[] = $tokenFactory->create($lastTokenType, $lastToken);
86 10
                }
87
88 10
                $lastTokenType = Token::TYPE_OPERATOR;
89 10
                $lastToken = $char;
90
91
            // Начало переменной
92 13
            } elseif ($char === '$') {
93 3
                if (!$lastTokenType) {
94 2
                    $lastTokenType = Token::TYPE_VARIABLE;
95 2
                    $lastToken = $char;
96 2
                } else {
97 3
                    $stack[] = $tokenFactory->create($lastTokenType, $lastToken);
98 3
                    $lastTokenType = Token::TYPE_VARIABLE;
99 3
                    $lastToken = $char;
100
                }
101
102
            // Буквы a-z, A-Z или символ подчеркивания
103
            // (могут использоваться в именах переменных и функций)
104 13 View Code Duplication
            } elseif (preg_match("/[a-zA-Z\_]+/i", $char)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105 10
                if (!$lastTokenType) {
106 5
                    $lastTokenType = Token::TYPE_FUNCTION;
107 5
                    $lastToken = $char;
108 5
                } elseif (
109
                    $lastTokenType === Token::TYPE_FUNCTION
110 10
                    || $lastTokenType === Token::TYPE_VARIABLE
111 10
                ) {
112 10
                    $lastToken .= $char;
113 10
                } else {
114 5
                    $stack[] = $tokenFactory->create($lastTokenType, $lastToken);
115 5
                    $lastTokenType = Token::TYPE_FUNCTION;
116 5
                    $lastToken = $char;
117
                }
118
119
            // Числа
120 13
            } elseif (is_numeric($char) || $char === '.') {
121 12 View Code Duplication
                if (!$lastTokenType) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122 3
                    $lastTokenType = Token::TYPE_NUMBER;
123 3
                    $lastToken = $char;
124 3
                } elseif (
125
                    $lastTokenType === Token::TYPE_NUMBER
126 12
                    || $lastTokenType === Token::TYPE_VARIABLE
127 12
                    || $lastTokenType === Token::TYPE_FUNCTION
128 12
                ) {
129 8
                    $lastToken .= $char;
130 8
                } else {
131 12
                    $stack[] = $tokenFactory->create($lastTokenType, $lastToken);
132 12
                    $lastTokenType = Token::TYPE_NUMBER;
133 12
                    $lastToken = $char;
134
                }
135 12
            }
136 13
        }
137
138 13
        $stack[] = $tokenFactory->create($lastTokenType, $lastToken);
139
140 12
        return $stack;
141
    }
142
}
143