GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

GraphCommand   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 31
lcom 1
cbo 4
dl 0
loc 180
rs 9.8
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 7 1
C execute() 0 46 7
C drawGraph() 0 54 14
B overwrite() 0 24 5
A strlen() 0 12 3
1
<?php
2
3
namespace Fieg\StatisticoBundle\Command;
4
5
use Fieg\Statistico\Reader;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class GraphCommand extends Command
11
{
12
    /**
13
     * @var Reader
14
     */
15
    private $reader;
16
17
    /**
18
     * @param Reader $reader
19
     */
20
    public function __construct(Reader $reader)
21
    {
22
        $this->reader = $reader;
23
        parent::__construct();
24
    }
25
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('statistico:graph')
30
            ->addArgument('bucket')
31
       ;
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        while (true) {
37
            $from = new \DateTime('-1 minute');
38
            $to = new \DateTime();
39
40
            $buckets = $this->reader->getBuckets();
41
            $bucketArgument = $input->getArgument('bucket');
42
43
            $buckets = array_filter(
44
                $buckets,
45
                function ($bucket) use ($bucketArgument) {
46
                    return preg_match('/'.preg_quote($bucketArgument, '/') . '/', $bucket);
47
                }
48
            );
49
50
            $maxLen = 0;
51
            foreach ($buckets as $bucket) {
52
                if (strlen($bucket) > $maxLen) {
53
                    $maxLen = strlen($bucket);
54
                }
55
            }
56
57
            $out = [];
58
59
            foreach ($buckets as $bucket) {
60
                $counts = $this->reader->queryCounts($bucket, 'seconds', $from, $to);
61
                $rpm = array_sum($counts);
62
63
                $minX = $from->getTimestamp();
64
                $maxX = $to->getTimestamp();
65
66
                $data = [];
67
68
                for ($i = $minX; $i <= $maxX; $i++) {
69
                    $data[$i] = (isset($counts[$i]) ? $counts[$i] : 0);
70
                }
71
72
                $out[] = sprintf("<info>%-".$maxLen."s</info> %s %3d rpm", $bucket, $this->drawGraph($data), $rpm);
73
            }
74
75
            $this->overwrite($output, implode($out, "\n"));
76
77
            usleep(500 * 1000);
78
        }
79
    }
80
81
    protected function drawGraph($data)
82
    {
83
        $chars = 60;
84
85
        if (count($data) > $chars) {
86
            $scaleX = floor(count($data) / $chars);
87
88
            $scaledData = [];
89
90
            for ($i = 0; $i < $chars; $i++) {
91
                $segment = array_slice($data, 1 + ($scaleX * $i) * -1, $scaleX);
92
93
                $total = 0;
94
                foreach ($segment as $value) {
95
                    $total += $value;
96
                }
97
98
                $value = $total;
99
100
                $scaledData[] = round($value);
101
            }
102
103
            $data = array_reverse($scaledData);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $data. This often makes code more readable.
Loading history...
104
        }
105
106
        $minY = min(array_values($data));
0 ignored issues
show
Unused Code introduced by
$minY is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
107
        $maxY = max(array_values($data));
108
109
        $graph = '';
110
111
        foreach ($data as $time => $value) {
112
            if ($maxY > 0) {
113
                $percent = round($value * 100 / $maxY);
114
            } else {
115
                $percent = 0;
116
            }
117
118
            $char = '▁';
119
120
            if ($percent > 75 && $percent <= 100) {
121
                $char = '▇';
122
            } else if ($percent > 50 && $percent <= 75) {
123
                $char = '▅';
124
            } else if ($percent > 25 && $percent <= 50) {
125
                $char = '▃';
126
            } else if ($percent > 0 && $percent <= 25) {
127
                $char = '▂';
128
            }
129
130
            $graph .= $char;
131
        }
132
133
        return $graph;
134
    }
135
136
    protected $lastMessageLength;
137
    protected $lastMessage;
138
139
    /**
140
     * Overwrites a previous message to the output.
141
     *
142
     * @param OutputInterface $output   An Output instance
143
     * @param string          $message  The message
144
     */
145
    protected function overwrite(OutputInterface $output, $message)
146
    {
147
        $length = $this->strlen($message);
148
149
        // append whitespace to match the last line's length
150
        if (null !== $this->lastMessageLength && $this->lastMessageLength > $length) {
151
            $message = str_pad($message, $this->lastMessageLength, "\x20", STR_PAD_RIGHT);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
152
        }
153
154
        // clear previous lines
155
        if (null !== $this->lastMessage) {
156
            $count = mb_substr_count($this->lastMessage, "\n");
157
            if ($count >= 1) {
158
                $output->write(chr(27) . '['.$count.'A');
159
            }
160
        }
161
162
        // carriage return
163
        $output->write("\x0D");
164
        $output->write($message);
165
166
        $this->lastMessageLength = $this->strlen($message);
167
        $this->lastMessage = $message;
168
    }
169
170
    /**
171
     * Returns the length of a string, using mb_strlen if it is available.
172
     *
173
     * @param string $string The string to check its length
174
     *
175
     * @return integer The length of the string
176
     */
177
    protected function strlen($string)
178
    {
179
        if (!function_exists('mb_strlen')) {
180
            return strlen($string);
181
        }
182
183
        if (false === $encoding = mb_detect_encoding($string)) {
184
            return strlen($string);
185
        }
186
187
        return mb_strlen($string, $encoding);
188
    }
189
}
190