Completed
Push — master ( b8d669...053f5f )
by ARCANEDEV
09:08
created

Calculatable::calculate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 3
crap 2
1
<?php namespace Arcanedev\Units\Traits;
2
3
/**
4
 * Trait     Calculatable
5
 *
6
 * @package  Arcanedev\Units\Traits
7
 * @author   ARCANEDEV <[email protected]>
8
 */
9
trait Calculatable
10
{
11
    /**
12
     * Calculate the value.
13
     *
14
     * @param  float|int  $a
15
     * @param  string     $operator
16
     * @param  float|int  $b
17
     *
18
     * @return float|int
19
     */
20
    protected static function calculate($a, $operator, $b)
21
    {
22
        $operations = $operations = [
23
            '+' => function ($a, $b) { return $a + $b; },
24
            '-' => function ($a, $b) { return $a - $b; },
25
            'x' => function ($a, $b) { return $a * $b; },
26
            '*' => function ($a, $b) { return $a * $b; },
27
            '/' => function ($a, $b) { return $a / $b; },
28
            '^' => function ($a, $b) { return pow($a, $b); },
29 102
        ];
30
31 136
        return array_key_exists($operator, $operations)
32 134
            ? call_user_func_array($operations[$operator], [$a, $b])
33 128
            : $a;
34
    }
35
}
36