Failed Conditions
Pull Request — 0.3 (#20)
by jean
12:27
created

StatCliDumper::dump()   B

Complexity

Conditions 9
Paths 56

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 56
nop 4
dl 0
loc 65
rs 7.208
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: darkilliant
5
 * Date: 6/24/18
6
 * Time: 11:14 AM
7
 */
8
9
namespace Darkilliant\ProcessBundle\StatDumper;
10
11
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
class StatCliDumper
15
{
16
    public function dump(array $stats, array $total, $zoom = null, SymfonyStyle $outputHelper)
17
    {
18
        $headers = ['class', '01 BEST', '02 BEST', '03 BEST', '01 BAD', '02 BAD', '03 BAD', 'TOTAL RUN', 'TOTAL WAIT', 'RATE', 'POT. RATE'];
19
        $data = [];
20
        $globals = array_combine(array_keys($stats), array_column($stats, 'global'));
21
        $maxGlobal = max($globals);
22
23
        foreach ($stats as $class => $values) {
24
            $path = explode('\\', $class);
25
            $class = array_pop($path);
26
            $times = array_merge($values['best_times'], $values['bad_times']);
27
            foreach ($times as &$time) {
28
                $time = (int) $time;
29
                $time = ($time > 100) ? '<error>'.$time.' ms</error>' : $time.' ms';
30
            }
31
            $global = $values['global'];
32
            if ($global === $maxGlobal) {
33
                $global = sprintf('<error>%d ms</error>', (int) $values['global']);
34
            } else {
35
                $global = ((int) $values['global']).' ms';
36
            }
37
            $times[] = $global;
38
            $times[] = ((int) $values['global_wait']).' ms';
39
            $data[] = array_merge([$values['position'].'. '.$class], $times, ['--', '--']);
40
        }
41
42
        $outputHelper->table($headers, $data);
43
44
        $waits = array_combine(array_keys($stats), array_column($stats, 'global_wait'));
45
        $maxWait = max($waits);
46
47
        foreach ($waits as $i => $ms)
48
        {
49
            $waits[$i] = (int) $waits[$i];
50
        }
51
52
        $reference = $maxWait / 100;
53
54
        foreach ($waits as $class => $ms) {
55
            if (0 !== $ms) {
56
                $countPercents = (int) ($ms / $reference);
57
58
                if ($countPercents < 1) {
59
                    $countPercents = 1;
60
                }
61
                echo $this->showBar($countPercents);
62
                echo $this->showBar($countPercents);
63
                echo $this->showBar($countPercents);
64
                echo $this->showBar($countPercents);
65
                echo $this->showBar($countPercents);
66
                echo $this->showBar($countPercents);
67
                echo $this->showBar($countPercents);
68
                echo $this->showBar($countPercents);
69
                echo $this->showBar($countPercents);
70
                echo $this->showBarClass($class, $countPercents);
71
                echo $this->showBar($countPercents);
72
                echo $this->showBar($countPercents);
73
                echo $this->showBar($countPercents);
74
                echo $this->showBar($countPercents);
75
                echo $this->showBar($countPercents);
76
                echo $this->showBar($countPercents);
77
                echo $this->showBar($countPercents);
78
                echo $this->showBar($countPercents);
79
                echo $this->showBar($countPercents);
80
                echo $this->showBar($countPercents);
81
            }
82
        }
83
    }
84
85
    public function showBar($countPercents)
86
    {
87
        $bar = str_repeat(' ',10);
88
        $bar .= str_repeat(' ', (100 - $countPercents)/2);
89
        $bar .= "\e[32m\e[42m".str_repeat('#', $countPercents)."\e[0m";
90
        $bar .= PHP_EOL;
91
92
        return $bar;
93
    }
94
95
    public function showBarClass($class, $countPercents)
96
    {
97
        $size = strlen($class);
98
        $bar = str_repeat(' ',10);
99
        $bar .= str_repeat(' ', (100 - $countPercents)/2);
100
        $bar .= "\e[42m".$class."\e[0m";
101
        if ($countPercents > $size) {
102
            $bar .= "\e[32m\e[42m".str_repeat('#', $countPercents - $size)."\e[0m";
103
        }
104
        $bar .= PHP_EOL;
105
106
        return $bar;
107
    }
108
}