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.

ProgressBar::__destruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Kernel\Cli\Writers;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class ProgressBar
20
 *
21
 * @package O2System\Kernel\Cli\Writers
22
 */
23
class ProgressBar
24
{
25
    /**
26
     * ProgressBar::$columns
27
     *
28
     * @var mixed
29
     */
30
    protected $columns;
31
32
    /**
33
     * ProgressBar::$limiter
34
     *
35
     * @var \O2System\Kernel\Cli\Writers\ProgressBar\Limiter
36
     */
37
    protected $limiter;
38
39
    /**
40
     * ProgressBar::$units
41
     *
42
     * @var mixed
43
     */
44
    protected $units;
45
46
    /**
47
     * ProgressBar::$total
48
     *
49
     * @var mixed
50
     */
51
    protected $total;
52
53
    // ------------------------------------------------------------------------
54
55
    /**
56
     * ProgressBar::__construct
57
     */
58
    public function __construct()
59
    {
60
        // change the fps limit as needed
61
        $this->limiter = new ProgressBar\Limiter(10);
62
63
        output()->write(PHP_EOL);
0 ignored issues
show
Bug introduced by
The method write() does not exist on O2System\Kernel\Http\Output. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        output()->/** @scrutinizer ignore-call */ write(PHP_EOL);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
    }
65
66
    // ------------------------------------------------------------------------
67
68
    /**
69
     * ProgressBar::__destruct
70
     */
71
    public function __destruct()
72
    {
73
        $this->write();
74
    }
75
76
    // ------------------------------------------------------------------------
77
78
    /**
79
     * ProgressBar::write
80
     *
81
     * Write progress bar.
82
     */
83
    protected function write()
84
    {
85
        $this->updateSize();
86
        $this->writeStatus($this->units, $this->total, $this->columns, $this->columns);
0 ignored issues
show
Bug introduced by
$this->columns of type string is incompatible with the type integer expected by parameter $lineWidth of O2System\Kernel\Cli\Writ...gressBar::writeStatus(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $this->writeStatus($this->units, $this->total, $this->columns, /** @scrutinizer ignore-type */ $this->columns);
Loading history...
Bug introduced by
$this->columns of type string is incompatible with the type integer expected by parameter $size of O2System\Kernel\Cli\Writ...gressBar::writeStatus(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $this->writeStatus($this->units, $this->total, /** @scrutinizer ignore-type */ $this->columns, $this->columns);
Loading history...
87
    }
88
89
    // ------------------------------------------------------------------------
90
91
    /**
92
     * ProgressBar::updateSize
93
     *
94
     * Execute columns update.
95
     */
96
    protected function updateSize()
97
    {
98
        // get the number of columns
99
        $this->columns = exec("tput cols");
100
    }
101
102
    // ------------------------------------------------------------------------
103
104
    /**
105
     * ProgressBar::writeStatus
106
     *
107
     * @param     $done
108
     * @param     $total
109
     * @param int $size
110
     * @param int $lineWidth
111
     */
112
    protected function writeStatus($done, $total, $size = 30, $lineWidth = -1)
113
    {
114
        if ($lineWidth <= 0) {
115
            $lineWidth = input()->env('COLUMNS');
116
        }
117
118
        static $start_time;
119
120
        // to take account for [ and ]
121
        $size -= 3;
122
        // if we go over our bound, just ignore it
123
        if ($done > $total) {
124
            return;
125
        }
126
127
        if (empty($start_time)) {
128
            $start_time = time();
129
        }
130
        $now = time();
131
132
        $percent = (double)($done / $total);
133
134
        $bar = floor($percent * $size);
135
136
        // jump to the begining
137
        output()->write("\r");
138
139
        // jump a line up
140
        output()->write("\x1b[A");
141
142
        $statusBar = "[";
143
        $statusBar .= str_repeat("=", $bar);
0 ignored issues
show
Bug introduced by
$bar of type double is incompatible with the type integer expected by parameter $multiplier of str_repeat(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
        $statusBar .= str_repeat("=", /** @scrutinizer ignore-type */ $bar);
Loading history...
144
        if ($bar < $size) {
145
            $statusBar .= ">";
146
            $statusBar .= str_repeat(" ", $size - $bar);
147
        } else {
148
            $statusBar .= "=";
149
        }
150
151
        $percentOutput = number_format($percent * 100, 0);
152
153
        $statusBar .= "]";
154
        $details = "$percentOutput%  $done/$total";
155
156
        $rate = ($now - $start_time) / $done;
157
        $left = $total - $done;
158
        $eta = round($rate * $left, 2);
159
160
        $elapsed = $now - $start_time;
161
162
163
        $details .= ' ' . language()->getLine('CLI_PROGRESS_BAR_ESTIMATION') . ': ' . $this->formatTime($eta) . ' ' . language()->getLine('CLI_PROGRESS_BAR_ELAPSED') . ': ' . $this->formatTime($elapsed) . ' ';
164
165
        $lineWidth--;
166
        if (strlen($details) >= $lineWidth) {
167
            $details = substr($details, 0, $lineWidth - 1);
168
        }
169
170
        output()->write(implode(PHP_EOL, [
171
            $details,
172
            $statusBar,
173
        ]));
174
175
        //echo "$details\n$status_bar";
176
177
        flush();
178
179
        // when done, send a newline
180
        if ($done == $total) {
181
            //echo "\n";
182
            output()->write(PHP_EOL);
183
        }
184
185
    }
186
187
    // ------------------------------------------------------------------------
188
189
    /**
190
     * ProgressBar::formatTime
191
     *
192
     * @param $time
193
     *
194
     * @return string
195
     */
196
    protected function formatTime($time)
197
    {
198
        if ($time > 100) {
199
            $time /= 60;
200
            if ($time > 100) {
201
                $time /= 60;
202
203
                return number_format($time) . " hr";
204
            }
205
206
            return number_format($time) . " min";
207
        }
208
209
        return number_format($time) . " sec";
210
    }
211
212
    // ------------------------------------------------------------------------
213
214
    /**
215
     * ProgressBar::update
216
     *
217
     * @param $units
218
     * @param $total
219
     */
220
    public function update($units, $total)
221
    {
222
        $this->units = $units;
223
        $this->total = $total;
224
        if ( ! $this->limiter->isValid()) {
225
            return;
226
        }
227
        $this->write();
228
    }
229
}