Completed
Push — master ( 78e634...2972d6 )
by Richard
02:04
created

Math::getModeResult()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 8.8571
cc 5
eloc 10
nc 5
nop 2
1
<?php
2
3
namespace Transphporm\TSSFunction;
4
5
class Math implements \Transphporm\TSSFunction {
6
    private $mode;
7
8
    const ADD = 1;
9
    const SUBTRACT = 2;
10
    const MULTIPLY = 3;
11
    const DIVIDE = 4;
12
13
    public function __construct($mode) {
14
        $this->mode = $mode;
15
    }
16
17
    public function run(array $args, \DomElement $element) {
18
        $result = $args[0];
19
        for ($i = 1; $i < count($args); $i++) $result = $this->getModeResult($args[$i], $result);
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
20
        return $result;
21
    }
22
23
    private function getModeResult($val, $prev) {
24
        switch ($this->mode) {
25
            case Math::ADD:
26
                return $prev+$val;
27
            case Math::SUBTRACT:
28
                return $prev-$val;
29
            case Math::MULTIPLY:
30
                return $prev*$val;
31
            case MATH::DIVIDE:
32
                return $prev/$val;
33
        }
34
    }
35
}
36