From f2343ed2e1016810a12b0f71a373bb121c8c6fd7 Mon Sep 17 00:00:00 2001 From: robchett Date: Fri, 3 Nov 2023 10:15:48 +0000 Subject: [PATCH] Add progress for scanning stage --- src/Psalm/Internal/Codebase/Scanner.php | 6 ++++- src/Psalm/Progress/DefaultProgress.php | 2 +- src/Psalm/Progress/LongProgress.php | 30 +++++++++++++++++++++++-- src/Psalm/Progress/Progress.php | 4 ++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Codebase/Scanner.php b/src/Psalm/Internal/Codebase/Scanner.php index aaa5e5c6a..262f0ad4e 100644 --- a/src/Psalm/Internal/Codebase/Scanner.php +++ b/src/Psalm/Internal/Codebase/Scanner.php @@ -317,6 +317,7 @@ final class Scanner $pool_size = 1; } + $this->progress->expand(count($files_to_scan)); if ($pool_size > 1) { $process_file_paths = []; @@ -355,7 +356,6 @@ final class Scanner */ function () { $this->progress->debug('Collecting data from forked scanner process' . PHP_EOL); - $project_analyzer = ProjectAnalyzer::getInstance(); $codebase = $project_analyzer->getCodebase(); $statements_provider = $codebase->statements_provider; @@ -377,6 +377,9 @@ final class Scanner 'taint_data' => $codebase->taint_flow_graph, ]; }, + function (): void { + $this->progress->taskDone(0); + }, ); // Wait for all tasks to complete and collect the results. @@ -427,6 +430,7 @@ final class Scanner $i = 0; foreach ($files_to_scan as $file_path => $_) { + $this->progress->taskDone(0); $this->scanAPath($i, $file_path); ++$i; } diff --git a/src/Psalm/Progress/DefaultProgress.php b/src/Psalm/Progress/DefaultProgress.php index 57024cbf7..64788f224 100644 --- a/src/Psalm/Progress/DefaultProgress.php +++ b/src/Psalm/Progress/DefaultProgress.php @@ -22,7 +22,7 @@ class DefaultProgress extends LongProgress public function taskDone(int $level): void { - if ($this->number_of_tasks > self::TOO_MANY_FILES) { + if ($this->fixed_size && $this->number_of_tasks > self::TOO_MANY_FILES) { ++$this->progress; // Source for rate limiting: diff --git a/src/Psalm/Progress/LongProgress.php b/src/Psalm/Progress/LongProgress.php index 8cf23fa1f..5a58886c4 100644 --- a/src/Psalm/Progress/LongProgress.php +++ b/src/Psalm/Progress/LongProgress.php @@ -25,6 +25,8 @@ class LongProgress extends Progress protected bool $print_infos = false; + protected bool $fixed_size = true; + public function __construct(bool $print_errors = true, bool $print_infos = true) { $this->print_errors = $print_errors; @@ -33,16 +35,19 @@ class LongProgress extends Progress public function startScanningFiles(): void { + $this->fixed_size = false; $this->write('Scanning files...' . "\n"); } public function startAnalyzingFiles(): void { - $this->write('Analyzing files...' . "\n\n"); + $this->fixed_size = true; + $this->write("\n" . 'Analyzing files...' . "\n\n"); } public function startAlteringFiles(): void { + $this->fixed_size = true; $this->write('Altering files...' . "\n"); } @@ -57,8 +62,30 @@ class LongProgress extends Progress $this->progress = 0; } + public function expand(int $number_of_tasks): void + { + $this->number_of_tasks += $number_of_tasks; + } + public function taskDone(int $level): void { + if ($this->number_of_tasks === null) { + throw new LogicException('Progress::start() should be called before Progress::taskDone()'); + } + + ++$this->progress; + + if (!$this->fixed_size) { + if ($this->progress == 1 || $this->progress == $this->number_of_tasks || $this->progress % 10 == 0) { + $this->write(sprintf( + "\r%s / %s?", + $this->progress, + $this->number_of_tasks, + )); + } + return; + } + if ($level === 0 || ($level === 1 && !$this->print_infos) || !$this->print_errors) { $this->write(self::doesTerminalSupportUtf8() ? '░' : '_'); } elseif ($level === 1) { @@ -67,7 +94,6 @@ class LongProgress extends Progress $this->write('E'); } - ++$this->progress; if (($this->progress % self::NUMBER_OF_COLUMNS) !== 0) { return; diff --git a/src/Psalm/Progress/Progress.php b/src/Psalm/Progress/Progress.php index f63132147..248878ff0 100644 --- a/src/Psalm/Progress/Progress.php +++ b/src/Psalm/Progress/Progress.php @@ -46,6 +46,10 @@ abstract class Progress { } + public function expand(int $number_of_tasks): void + { + } + public function taskDone(int $level): void { }