Failed Conditions
Pull Request — 0.3 (#20)
by jean
03:05
created

StatsCalculator::calculForOneStep()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 15
ccs 12
cts 12
cp 1
crap 2
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Darkilliant\ProcessBundle\StatDumper;
4
5
class StatsCalculator
6
{
7 1
    public function calcul(array $dataCollected): array
8
    {
9 1
        $calculatedStats = [];
10
11 1
        foreach ($dataCollected as $class => $stat) {
12 1
            $calculatedStats[$class] = $this->calculForOneStep($stat);
13
        }
14
15
        return [
16 1
            'stats' => $calculatedStats,
17 1
            'total' => $this->calculTotals($calculatedStats),
18
        ];
19
    }
20
21 1
    private function isEventfull($times)
22
    {
23 1
        $moved = false;
24 1
        $last = null;
25 1
        foreach ($times as $time) {
26 1
            $time = (int) $time;
27 1
            if (null !== $last && $last != $time) {
28 1
                $moved = true;
29 1
                break;
30
            }
31 1
            $last = $time;
32
        }
33
34 1
        return $moved;
35
    }
36
37 1
    private function calculForOneStep($stat)
38
    {
39 1
        $calculatedStat['global'] = array_sum($stat['time']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$calculatedStat was never initialized. Although not strictly required by PHP, it is generally a good practice to add $calculatedStat = array(); before regardless.
Loading history...
40
41 1
        $calculatedStat['tendance'] = (!$this->isEventfull($stat['time'])) ? '~~~' : '/\/';
42 1
        $calculatedStat['global_wait'] = array_sum($stat['wait']);
43 1
        asort($stat['time'], SORT_NUMERIC);
44 1
        $calculatedStat['best_times'] = array_slice($stat['time'], 0, 3, true);
45 1
        arsort($stat['time'], SORT_NUMERIC);
46 1
        $calculatedStat['bad_times'] = array_slice($stat['time'], 0, 3, true);
47 1
        $calculatedStat['potential_rate'] = '??';
48 1
        $calculatedStat['position'] = $stat['position'];
49 1
        $calculatedStat['count_iteration'] = count($stat['time']);
50
51 1
        return $calculatedStat;
52
    }
53
54 1
    private function calculTotals($calculatedStats)
55
    {
56
        return [
57 1
            'global' => array_sum(array_column($calculatedStats, 'global')),
58
            'best_times' => [0, 0, 0],
59
            'bad_times' => [0, 0, 0],
60 1
            'global_wait' => array_sum(array_column($calculatedStats, 'global_wait')),
61 1
            'position' => ' ',
62 1
            'potential_rate' => '??',
63
        ];
64
    }
65
}
66