TokenFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 99
ccs 19
cts 19
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 6 1
A delimiter() 0 4 1
A operator() 0 4 1
A bracket() 0 4 1
A func() 0 4 1
A number() 0 4 1
A variable() 0 4 1
A getClassName() 0 8 2
1
<?php
2
3
/**
4
 * TokenFactory.php
5
 *
6
 * @date 28.03.2015 3:37:00
7
 * @copyright Sklyarov Alexey <[email protected]>
8
 */
9
10
namespace Sufir\Calc\Token;
11
12
use Sufir\Calc\Token;
13
use InvalidArgumentException;
14
15
/**
16
 * TokenFactory
17
 *
18
 * Tokens Factory
19
 *
20
 * @author Sklyarov Alexey <[email protected]>
21
 * @package Sufir\Calc\Token
22
 */
23
final class TokenFactory
24
{
25
    private static $classes = array(
26
        Token::TYPE_FUNCTION => '\Sufir\Calc\Token\FunctionToken',
27
        Token::TYPE_OPERATOR => '\Sufir\Calc\Token\OperatorToken',
28
        Token::TYPE_BRACKET => '\Sufir\Calc\Token\BracketToken',
29
        Token::TYPE_DELIMITER => '\Sufir\Calc\Token\DelimiterToken',
30
        Token::TYPE_NUMBER => '\Sufir\Calc\Token\NumberToken',
31
        Token::TYPE_VARIABLE => '\Sufir\Calc\Token\VariableToken',
32
    );
33
34
    /**
35
     *
36
     * @param string $type
37
     * @param string $value
38
     * @return \Sufir\Calc\Token\Token
39
     * @throws InvalidArgumentException
40
     */
41 35
    public function create($type, $value)
42
    {
43 35
        $className = $this->getClassName($type);
44
45 33
        return new $className($value);
46
    }
47
48
    /**
49
     * @param string $value
50
     * @return \Sufir\Calc\Token\DelimiterToken
51
     */
52 1
    public function delimiter($value)
53
    {
54 1
        return $this->create(Token::TYPE_DELIMITER, $value);
55
    }
56
57
    /**
58
     *
59
     * @param string $value
60
     * @return \Sufir\Calc\Token\OperatorToken
61
     */
62 1
    public function operator($value)
63
    {
64 1
        return $this->create(Token::TYPE_OPERATOR, $value);
65
    }
66
67
    /**
68
     *
69
     * @param string $value
70
     * @return \Sufir\Calc\Token\BracketToken
71
     */
72 1
    public function bracket($value)
73
    {
74 1
        return $this->create(Token::TYPE_BRACKET, $value);
75
    }
76
77
    /**
78
     *
79
     * @param string $value
80
     * @return \Sufir\Calc\Token\FunctionToken
81
     */
82 1
    public function func($value)
83
    {
84 1
        return $this->create(Token::TYPE_FUNCTION, $value);
85
    }
86
87
    /**
88
     *
89
     * @param string $value
90
     * @return \Sufir\Calc\Token\NumberToken
91
     */
92 1
    public function number($value)
93
    {
94 1
        return $this->create(Token::TYPE_NUMBER, $value);
95
    }
96
97
    /**
98
     *
99
     * @param string $value
100
     * @return \Sufir\Calc\Token\VariableToken
101
     */
102 1
    public function variable($value)
103
    {
104 1
        return $this->create(Token::TYPE_VARIABLE, $value);
105
    }
106
107
    /**
108
     *
109
     * @param string $type
110
     * @return string
111
     * @throws InvalidArgumentException
112
     */
113 35
    private function getClassName($type)
114
    {
115 35
        if (!isset(self::$classes[$type])) {
116 2
            throw new InvalidArgumentException('Неизвестный тип токена!');
117
        }
118
119 33
        return self::$classes[$type];
120
    }
121
}
122