OperatorToken::getPriority()   C
last analyzed

Complexity

Conditions 12
Paths 12

Size

Total Lines 28
Code Lines 23

Duplication

Lines 22
Ratio 78.57 %

Code Coverage

Tests 22
CRAP Score 12.5239

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 22
loc 28
ccs 22
cts 26
cp 0.8462
rs 5.1612
cc 12
eloc 23
nc 12
nop 1
crap 12.5239

How to fix   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
 * 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