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 aaa5e5c6aea..262f0ad4ed8 100644 --- a/src/Psalm/Internal/Codebase/Scanner.php +++ b/src/Psalm/Internal/Codebase/Scanner.php @@ -317,6 +317,7 @@ private function scanFilePaths(int $pool_size): bool $pool_size = 1; } + $this->progress->expand(count($files_to_scan)); if ($pool_size > 1) { $process_file_paths = []; @@ -355,7 +356,6 @@ function (): void { */ 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 @@ function () { '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 @@ function () { $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 57024cbf72f..64788f224cd 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 8cf23fa1fc6..5a58886c4b0 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 @@ public function __construct(bool $print_errors = true, bool $print_infos = true) 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 @@ public function start(int $number_of_tasks): void $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 @@ public function taskDone(int $level): void $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 f6313214775..248878ff0a1 100644 --- a/src/Psalm/Progress/Progress.php +++ b/src/Psalm/Progress/Progress.php @@ -46,6 +46,10 @@ public function start(int $number_of_tasks): void { } + public function expand(int $number_of_tasks): void + { + } + public function taskDone(int $level): void { }