Skip to content

Commit

Permalink
Merge pull request #10345 from robchett/scanner_progress
Browse files Browse the repository at this point in the history
Add progress for scanning stage
  • Loading branch information
orklah committed Nov 3, 2023
2 parents 4ce7f9f + f2343ed commit 65f7d7f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/Psalm/Internal/Codebase/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -427,6 +430,7 @@ function () {
$i = 0;

foreach ($files_to_scan as $file_path => $_) {
$this->progress->taskDone(0);
$this->scanAPath($i, $file_path);
++$i;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Progress/DefaultProgress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 28 additions & 2 deletions src/Psalm/Progress/LongProgress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}

Expand All @@ -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) {
Expand All @@ -67,7 +94,6 @@ public function taskDone(int $level): void
$this->write('E');
}

++$this->progress;

if (($this->progress % self::NUMBER_OF_COLUMNS) !== 0) {
return;
Expand Down
4 changes: 4 additions & 0 deletions src/Psalm/Progress/Progress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
Expand Down

0 comments on commit 65f7d7f

Please sign in to comment.