OperatorToken::isLeftAssoc()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * OperatorToken.php
5
 *
6
 * @date 28.03.2015 2:48:12
7
 * @copyright Sklyarov Alexey <[email protected]>
8
 */
9
10
namespace Sufir\Calc\Token;
11
12
/**
13
 * OperatorToken
14
 *
15
 * Математический оператор
16
 *
17
 * @author Sklyarov Alexey <[email protected]>
18
 * @package Sufir\Calc\Token
19
 */
20
final class OperatorToken extends AbstractToken
21
{
22
    /**
23
     *
24
     * @var array
25
     */
26
    protected static $operators = array('*', '/', '+', '-', '^');
27
28
    /**
29
     *
30
     * @param string $assoc
31
     * @return integer
32
     */
33 22
    public function getPriority($assoc = 'left')
34
    {
35 22
        if ($assoc === 'right') {
36 5 View Code Duplication
            switch ($this->value) {
37 5
                case '^':
38 1
                    return 5;
39 4
                case '/':
40 1
                    return 4;
41 3
                case '*':
42 1
                    return 3;
43 2
                case '-':
44 1
                    return 2;
45 1
                case '+':
46 1
                    return 1;
47
            }
48
        } else {
49 22 View Code Duplication
            switch ($this->value) {
50 22
                case '^':
51 10
                    return 3;
52 21
                case '*':
53 21
                case '/':
54 18
                    return 2;
55 15
                case '+':
56 15
                case '-':
57 15
                    return 1;
58
            }
59
        }
60
    }
61
62
    /**
63
     *
64
     * @return boolean
65
     */
66 22
    public function isLeftAssoc()
67
    {
68 22
        return !$this->isRightAssoc();
69
    }
70
71
    /**
72
     *
73
     * @return boolean
74
     */
75 22
    public function isRightAssoc()
76
    {
77 22
        return ($this->value === '^');
78
    }
79
80
    /**
81
     *
82
     * @return array
83
     */
84 45
    public static function getAllowedOperators()
85
    {
86 45
        return self::$operators;
87
    }
88
89
    /**
90
     *
91
     * @param string $value
92
     * @return string
93
     */
94 38
    protected function sanitize($value)
95
    {
96 38
        return trim($value);
97
    }
98
99
    /**
100
     *
101
     * @param string $value
102
     * @return boolean
103
     */
104 45
    public static function validate($value)
105
    {
106 45
        return in_array($value, self::getAllowedOperators());
107
    }
108
}
109