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

OperatorToken::getPriority()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 32
Code Lines 27

Duplication

Lines 26
Ratio 81.25 %

Code Coverage

Tests 22
CRAP Score 13.4172

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 26
loc 32
ccs 22
cts 28
cp 0.7857
rs 5.1612
cc 12
eloc 27
nc 12
nop 1
crap 13.4172

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 type $assoc
31
     * @return integer
32
     */
33 7
    public function getPriority($assoc = 'left')
34
    {
35 7
        if ($assoc === 'right') {
36 5 View Code Duplication
            switch ($this->value) {
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...
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
                default:
48
                    return 0;
49
            }
50
        } else {
51 7 View Code Duplication
            switch ($this->value) {
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...
52 7
                case '^':
53 3
                    return 3;
54 6
                case '*':
55 6
                case '/':
56 4
                    return 2;
57 4
                case '+':
58 4
                case '-':
59 4
                    return 1;
60
                default:
61
                    return 0;
62
            }
63
        }
64
    }
65
66
    /**
67
     *
68
     * @return boolean
69
     */
70 7
    public function isLeftAssoc()
71
    {
72 7
        return !$this->isRightAssoc();
73
    }
74
75
    /**
76
     *
77
     * @return boolean
78
     */
79 7
    public function isRightAssoc()
80
    {
81 7
        return ($this->value === '^');
82
    }
83
84
    /**
85
     *
86
     * @return array
87
     */
88 28
    public static function getAllowedOperators()
89
    {
90 28
        return self::$operators;
91
    }
92
93
    /**
94
     *
95
     * @param string $value
96
     * @return string
97
     */
98 21
    protected function sanitize($value)
99
    {
100 21
        return trim($value);
101
    }
102
103
    /**
104
     *
105
     * @param string $value
106
     * @return boolean
107
     */
108 28
    protected function validate($value)
109
    {
110 28
        return in_array($value, self::getAllowedOperators());
111
    }
112
}
113