CliProgressBar::getMemString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Progress bar for console applications
4
 *
5
 * @file      CliProgressBar.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      Сбт Фев 16 20:07:56 2013
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>.
14
 */
15
16
namespace Veles\Tools;
17
18
use Veles\Validators\ByteValidator;
19
20
/**
21
 * Class CliProgressBar
22
 *
23
 * @author  Alexander Yancharuk <alex at itvault dot info>
24
 */
25
class CliProgressBar
26
{
27
	/** @var float Progress bar percent */
28
	protected $pb_percent;
29
	/** @var float Current progress bar percent  */
30
	protected $percent;
31
	/** @var float Progress bar initialization time  */
32
	protected $start_time;
33
	/** @var int Progress bar final value */
34
	protected $final_value;
35
	/** @var float Time when update calculation started */
36
	protected $curr_time;
37
	/** @var float Time when last update calculation finished */
38
	protected $last_update_time = 0.0;
39
	/** @var float Time between finishing last update and starting current update */
40
	protected $clean_process_time = 0.0;
41
	/** @var string Function for memory usage */
42
	protected $mem_usage_func = 'memory_get_usage';
43
	/** @var string Function for max memory usage */
44
	protected $mem_peak_func = 'memory_get_peak_usage';
45
	/** @var int Progress bar width */
46
	protected $width;
47
48
	/**
49
	 * Constructor
50
	 *
51
	 * @param int $final Final result quantity
52
	 * @param int $width ProgressBar width
53
	 */
54 6
	public function __construct($final, $width = 60)
55
	{
56 6
		$this->final_value = max($final, 1);
57 6
		$this->width       = $width;
58 6
		$this->pb_percent  = $width / 100;
59 6
		$this->percent     = $this->final_value / 100;
60 6
		$this->start_time  = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $start_time is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
61 6
		$this->last_update_time = $this->start_time;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->start_time can also be of type string. However, the property $last_update_time is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
62
	}
63
64
	/**
65
	 * Progress bar update
66
	 *
67
	 * @param int $current Current process quantity
68
	 */
69 4
	public function update($current)
70
	{
71 4
		$this->curr_time = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $curr_time is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
72 4
		$this->clean_process_time += $this->curr_time - $this->last_update_time;
73
74 4
		list($end, $bar, $space_len, $status) = $this->calcParams($current);
75
76 4
		echo ($space_len > 0)
77 3
			? "\033[?25l[$bar>\033[{$space_len}C]$status$end"
78 1
			: "[$bar>]$status$end\033[?25h";
79
80 4
		$this->last_update_time = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $last_update_time is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
81
	}
82
83
	/**
84
	 * Get string with statistic
85
	 *
86
	 * This method is public only for testing purposes. Logic with calculation
87
	 * must be moved into builder or class-calculator.
88
	 *
89
	 * @param int $current Current process quantity
90
	 *
91
	 * @return string
92
	 */
93 1
	public function getStatusString($current)
94
	{
95 1
		$current   = max($current, 1);
96 1
		$avg_speed = round($current / $this->clean_process_time);
97 1
		$estimated = number_format(
98 1
			($this->final_value - $current) *
99 1
			($this->curr_time - $this->start_time) /
100 1
			$current, 1
101 1
		);
102
103 1
		return " $current u | $avg_speed u/s | Est: $estimated s";
104
	}
105
106
	/**
107
	 * Get string with memory statistic
108
	 *
109
	 * @return string
110
	 */
111 1
	public function getMemString()
112
	{
113 1
		$mem = ByteValidator::format(call_user_func($this->mem_usage_func));
114 1
		$max_mem = ByteValidator::format(call_user_func($this->mem_peak_func));
115
116 1
		return " | Mem: $mem | Max: $max_mem";
117
	}
118
119
	/**
120
	 * Calculate bar-string params
121
	 *
122
	 * @param int $current Current process quantity
123
	 *
124
	 * @return array
125
	 */
126 4
	protected function calcParams($current)
127
	{
128 4
		$done = number_format($current / $this->percent, 2);
129 4
		list($position, $end) = $this->getPosition($done);
130
131 4
		return [
132 4
			$end,
133 4
			str_repeat('=', $position),
134 4
			$this->width - $position,
135 4
			$this->getStatusString($current) . $this->getMemString()
136 4
		];
137
	}
138
139
	/**
140
	 * Returns cursor position and last string-part for bar
141
	 *
142
	 * @param $done
143
	 *
144
	 * @return array
145
	 */
146 4
	protected function getPosition($done)
147
	{
148 4
		return ($done < 100)
149 3
			? [(int) floor($this->pb_percent * $done), "\033[K\r"]
150 4
			: [$this->width, "\033[K" . PHP_EOL];
151
	}
152
}
153