Skip to content

Commit

Permalink
Fix #3024 - replay errors in --diff --diff-methods mode
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 26, 2020
1 parent 971ae50 commit 4ced26b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Psalm/Codebase.php
Expand Up @@ -465,7 +465,7 @@ public function reportUnusedVariables()
public function addFilesToAnalyze(array $files_to_analyze)
{
$this->scanner->addFilesToDeepScan($files_to_analyze);
$this->analyzer->addFiles($files_to_analyze);
$this->analyzer->addFilesToAnalyze($files_to_analyze);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Expand Up @@ -487,7 +487,7 @@ public function check($base_dir, $is_diff = false)
|| $deleted_files === null
|| count($diff_files) > 200
) {
$this->codebase->analyzer->addFiles($this->project_files);
$this->codebase->analyzer->addFilesToAnalyze($this->project_files);

$this->config->initializePlugins($this);

Expand All @@ -498,6 +498,8 @@ public function check($base_dir, $is_diff = false)
$this->progress->debug(count($diff_files) . ' changed files: ' . "\n");
$this->progress->debug(' ' . implode("\n ", $diff_files) . "\n");

$this->codebase->analyzer->addFilesToShowResults($this->project_files);

if ($diff_files || $this->codebase->find_unused_code) {
$file_list = $this->getReferencedFilesFromDiff($diff_files);

Expand Down
33 changes: 28 additions & 5 deletions src/Psalm/Internal/Codebase/Analyzer.php
Expand Up @@ -113,6 +113,14 @@ class Analyzer
*/
private $files_to_analyze = [];

/**
* We can show analysis results on more files than we analyze
* because the results can be cached
*
* @var array<string, string>
*/
private $files_with_analysis_results = [];

/**
* We may update fewer files than we analyse (i.e. for dead code detection)
*
Expand Down Expand Up @@ -167,9 +175,20 @@ public function __construct(
*
* @return void
*/
public function addFiles(array $files_to_analyze)
public function addFilesToAnalyze(array $files_to_analyze)
{
$this->files_to_analyze += $files_to_analyze;
$this->files_with_analysis_results += $files_to_analyze;
}

/**
* @param array<string, string> $files_to_analyze
*
* @return void
*/
public function addFilesToShowResults(array $files_to_analyze)
{
$this->files_with_analysis_results += $files_to_analyze;
}

/**
Expand All @@ -189,7 +208,7 @@ public function setFilesToUpdate(array $files_to_update)
*/
public function canReportIssues($file_path)
{
return isset($this->files_to_analyze[$file_path]);
return isset($this->files_with_analysis_results[$file_path]);
}

/**
Expand Down Expand Up @@ -576,9 +595,7 @@ public function loadCachedResults(ProjectAnalyzer $project_analyzer)
{
$codebase = $project_analyzer->getCodebase();

if ($codebase->diff_methods
&& (!$codebase->collect_references || $codebase->server_mode)
) {
if ($codebase->diff_methods) {
$this->analyzed_methods = $codebase->file_reference_provider->getAnalyzedMethods();
$this->existing_issues = $codebase->file_reference_provider->getExistingIssues();
$file_maps = $codebase->file_reference_provider->getFileMaps();
Expand Down Expand Up @@ -771,6 +788,12 @@ public function loadCachedResults(ProjectAnalyzer $project_analyzer)
}
}

foreach (array_diff_key($this->files_with_analysis_results, $this->files_to_analyze) as $file_path) {
if (isset($this->existing_issues[$file_path])) {
IssueBuffer::addIssues([$file_path => $this->existing_issues[$file_path]]);
}
}

$method_references_to_class_members = array_filter(
$method_references_to_class_members
);
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/LanguageServer/LanguageServer.php
Expand Up @@ -308,7 +308,7 @@ public function doAnalysis()
}

$all_file_paths_to_analyze = array_keys($all_files_to_analyze);
$codebase->analyzer->addFiles(array_combine($all_file_paths_to_analyze, $all_file_paths_to_analyze));
$codebase->analyzer->addFilesToAnalyze(array_combine($all_file_paths_to_analyze, $all_file_paths_to_analyze));
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);

$this->emitIssues($all_files_to_analyze);
Expand Down

0 comments on commit 4ced26b

Please sign in to comment.