CalcOperation::exponent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace DiceCalc;
4
5
/**
6
 * Class CalcOperation
7
 *
8
 * @package DiceCalc
9
 * @author  Owen Winkler <[email protected]>
10
 * @license MIT http://opensource.org/licenses/MIT
11
 */
12
class CalcOperation
13
{
14
15
    static protected $operators = [
16
        '+' => 'add',
17
        '*' => 'multiply',
18
        '=' => 'equalto',
19
        '<' => 'lessthan',
20
        '>' => 'greaterthan',
21
        '^' => 'exponent',
22
        '/' => 'divide',
23
        '-' => 'subtract',
24
    ];
25
26
    /**
27
     * @param  string $operator
28
     * @param $operand2
29
     * @param $operand1
30
     *
31
     * @throws \Exception
32
     * @return bool|number
33
     */
34
    public static function calc($operator, $operand1, $operand2)
35
    {
36
        if(isset(static::$operators[$operator])) {
37
            return call_user_func(array('self', static::$operators[$operator]), self::reduce($operand1), self::reduce($operand2));
38
        }
39
        throw new \Exception('Unknown operator "' . $operator . '".');
40
    }
41
42
    /**
43
     * @param $operand
44
     *
45
     * @return number|string
46
     * @throws \Exception
47
     */
48
    public static function reduce($operand)
49
    {
50
        if(is_numeric($operand)) {
51
            return $operand;
52
        }
53
        elseif ($operand instanceof Calc) {
54
            return $operand();
55
        }
56
        throw new \Exception('This is not a number');
57
    }
58
59
    /**
60
     * @param  number      $operand1
61
     * @param  number      $operand2
62
     *
63
     * @return bool|number
64
     */
65
    protected static function add($operand1, $operand2)
66
    {
67
        return $operand1 + $operand2;
68
    }
69
70
    /**
71
     * @param  number      $operand1
72
     * @param  number      $operand2
73
     *
74
     * @return bool|number
75
     */
76
    protected static function multiply($operand1, $operand2)
77
    {
78
        return $operand1 * $operand2;
79
    }
80
81
    /**
82
     * @param  number      $operand1
83
     * @param  number      $operand2
84
     *
85
     * @return bool|number
86
     */
87
    protected static function subtract($operand1, $operand2)
88
    {
89
        return $operand1 - $operand2;
90
    }
91
92
    /**
93
     * @param  number    $operand1
94
     * @param  number    $operand2
95
     *
96
     * @return bool|number
97
     */
98
    protected static function divide($operand1, $operand2)
99
    {
100
        return $operand1 / $operand2;
101
    }
102
103
    /**
104
     * @param  number      $operand1
105
     * @param  number      $operand2
106
     *
107
     * @return bool|number
108
     */
109
    protected static function exponent($operand1, $operand2)
110
    {
111
        return pow($operand1, $operand2);
112
    }
113
114
    /**
115
     * @param  number $operand1
116
     * @param  number $operand2
117
     *
118
     * @return bool
119
     */
120
    protected static function greaterthan($operand1, $operand2)
121
    {
122
        return $operand1 > $operand2;
123
    }
124
125
    /**
126
     * @param  number $operand1
127
     * @param  number $operand2
128
     *
129
     * @return bool
130
     */
131
    protected static function lessthan($operand1, $operand2)
132
    {
133
        return ($operand1 < $operand2);
134
    }
135
136
    /**
137
     * @param  number $operand1
138
     * @param  number $operand2
139
     *
140
     * @return bool
141
     */
142
    protected static function equalto($operand1, $operand2)
143
    {
144
        return ($operand1 == $operand2);
145
    }
146
}
147