2019-05-30 16:30:41 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Progress;
|
|
|
|
|
|
|
|
class DefaultProgress extends Progress
|
|
|
|
{
|
|
|
|
public const NUMBER_OF_COLUMNS = 60;
|
|
|
|
|
|
|
|
/** @var int|null */
|
|
|
|
private $number_of_tasks;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $progress = 0;
|
|
|
|
|
2019-05-30 16:42:16 +02:00
|
|
|
/** @var bool */
|
|
|
|
private $print_failures = false;
|
|
|
|
|
|
|
|
public function __construct(bool $print_failures = true)
|
|
|
|
{
|
|
|
|
$this->print_failures = $print_failures;
|
|
|
|
}
|
|
|
|
|
2019-05-30 16:30:41 +02:00
|
|
|
public function startScanningFiles(): void
|
|
|
|
{
|
|
|
|
$this->write('Scanning files...' . "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function startAnalyzingFiles(): void
|
|
|
|
{
|
|
|
|
$this->write('Analyzing files...' . "\n\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
|
|
|
|
{
|
2019-05-30 16:42:16 +02:00
|
|
|
if ($successful || !$this->print_failures) {
|
2019-05-30 16:30:41 +02:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|