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

Lexer   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 118
Duplicated Lines 26.27 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
c 1
b 0
f 0
lcom 0
cbo 1
dl 31
loc 118
ccs 76
cts 76
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D parse() 31 109 27

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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