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

Calculatable   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 27
ccs 4
cts 4
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A calculate() 0 15 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