Completed
Push — master ( 542d84...357932 )
by Alexey
02:33
created

OperatorToken   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 89
Duplicated Lines 24.72 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 17
c 2
b 1
f 1
lcom 2
cbo 1
dl 22
loc 89
ccs 32
cts 36
cp 0.8889
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
C getPriority() 22 28 12
A isLeftAssoc() 0 4 1
A isRightAssoc() 0 4 1
A getAllowedOperators() 0 4 1
A sanitize() 0 4 1
A validate() 0 4 1

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