2019-05-16 23:12:31 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Progress;
|
|
|
|
|
|
|
|
class DefaultProgress extends Progress
|
|
|
|
{
|
2019-05-25 19:24:40 +02:00
|
|
|
public const NUMBER_OF_COLUMNS = 60;
|
|
|
|
|
|
|
|
/** @var int|null */
|
|
|
|
private $number_of_tasks;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $progress = 0;
|
|
|
|
|
2019-05-16 23:12:31 +02:00
|
|
|
public function startScanningFiles(): void
|
|
|
|
{
|
2019-05-25 19:24:40 +02:00
|
|
|
$this->write('Scanning files...' . "\n");
|
2019-05-16 23:12:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function startAnalyzingFiles(): void
|
|
|
|
{
|
2019-05-25 19:24:40 +02:00
|
|
|
$this->write('Analyzing files...' . "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function start(int $number_of_tasks): void
|
|
|
|
{
|
|
|
|
$this->number_of_tasks = $number_of_tasks;
|
|
|
|
$this->progress = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function taskDone(bool $successful): void
|
|
|
|
{
|
|
|
|
if ($successful) {
|
|
|
|
$this->write('.');
|
|
|
|
} else {
|
|
|
|
$this->write('F');
|
|
|
|
}
|
|
|
|
|
|
|
|
++$this->progress;
|
|
|
|
|
|
|
|
if (($this->progress % self::NUMBER_OF_COLUMNS) !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->printOverview();
|
|
|
|
$this->write(PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function finish(): void
|
|
|
|
{
|
|
|
|
$this->write(PHP_EOL);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function printOverview(): void
|
|
|
|
{
|
|
|
|
if ($this->number_of_tasks === null) {
|
|
|
|
throw new \LogicException('Progress::start() should be called before Progress::startDone()');
|
|
|
|
}
|
|
|
|
|
|
|
|
$leadingSpaces = 1 + strlen((string) $this->number_of_tasks) - strlen((string) $this->progress);
|
|
|
|
$percentage = round($this->progress / $this->number_of_tasks * 100);
|
|
|
|
$message = sprintf(
|
|
|
|
'%s%s / %s (%s%%)',
|
|
|
|
str_repeat(' ', $leadingSpaces),
|
|
|
|
$this->progress,
|
|
|
|
$this->number_of_tasks,
|
|
|
|
$percentage
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->write($message);
|
2019-05-16 23:12:31 +02:00
|
|
|
}
|
|
|
|
}
|