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

Math   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 31
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A run() 0 5 2
B getModeResult() 0 12 5
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