1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Small test improvements

This commit is contained in:
Brown 2020-04-01 12:56:32 -04:00
parent fa28d767fc
commit 04a49102ec
4 changed files with 94 additions and 37 deletions

View File

@ -171,7 +171,7 @@ class ProjectAnalyzer
/** /**
* @var array<string,string> * @var array<string,string>
*/ */
private $project_files; private $project_files = [];
/** /**
* @var array<string, string> * @var array<string, string>
@ -246,8 +246,6 @@ class ProjectAnalyzer
$this->stdout_report_options = $stdout_report_options; $this->stdout_report_options = $stdout_report_options;
$this->generated_report_options = $generated_report_options; $this->generated_report_options = $generated_report_options;
$project_files = [];
foreach ($this->config->getProjectDirectories() as $dir_name) { foreach ($this->config->getProjectDirectories() as $dir_name) {
$file_extensions = $this->config->getFileExtensions(); $file_extensions = $this->config->getFileExtensions();
@ -255,17 +253,15 @@ class ProjectAnalyzer
foreach ($file_paths as $file_path) { foreach ($file_paths as $file_path) {
if ($this->config->isInProjectDirs($file_path)) { if ($this->config->isInProjectDirs($file_path)) {
$project_files[$file_path] = $file_path; $this->addProjectFile($file_path);
} }
} }
} }
foreach ($this->config->getProjectFiles() as $file_path) { foreach ($this->config->getProjectFiles() as $file_path) {
$project_files[$file_path] = $file_path; $this->addProjectFile($file_path);
} }
$this->project_files = $project_files;
self::$instance = $this; self::$instance = $this;
} }
@ -548,7 +544,12 @@ class ProjectAnalyzer
$this->progress->startAnalyzingFiles(); $this->progress->startAnalyzingFiles();
$this->codebase->analyzer->analyzeFiles($this, $this->threads, $this->codebase->alter_code); $this->codebase->analyzer->analyzeFiles(
$this,
$this->threads,
$this->codebase->alter_code,
true
);
if ($this->parser_cache_provider) { if ($this->parser_cache_provider) {
$removed_parser_files = $this->parser_cache_provider->deleteOldParserCaches( $removed_parser_files = $this->parser_cache_provider->deleteOldParserCaches(
@ -933,7 +934,12 @@ class ProjectAnalyzer
$this->progress->startAnalyzingFiles(); $this->progress->startAnalyzingFiles();
$this->codebase->analyzer->analyzeFiles($this, $this->threads, $this->codebase->alter_code); $this->codebase->analyzer->analyzeFiles(
$this,
$this->threads,
$this->codebase->alter_code,
$this->codebase->find_unused_code === 'always'
);
} }
/** /**
@ -980,6 +986,17 @@ class ProjectAnalyzer
return $file_paths; return $file_paths;
} }
/**
* @param string $dir_name
*
* @return void
*/
public function addProjectFile(string $file_path)
{
$this->project_files[$file_path] = $file_path;
}
/** /**
* @param string $dir_name * @param string $dir_name
* @param Config $config * @param Config $config
@ -1066,7 +1083,12 @@ class ProjectAnalyzer
$this->progress->startAnalyzingFiles(); $this->progress->startAnalyzingFiles();
$this->codebase->analyzer->analyzeFiles($this, $this->threads, $this->codebase->alter_code); $this->codebase->analyzer->analyzeFiles(
$this,
$this->threads,
$this->codebase->alter_code,
$this->codebase->find_unused_code === 'always'
);
} }
/** /**
@ -1100,7 +1122,12 @@ class ProjectAnalyzer
$this->progress->startAnalyzingFiles(); $this->progress->startAnalyzingFiles();
$this->codebase->analyzer->analyzeFiles($this, $this->threads, $this->codebase->alter_code); $this->codebase->analyzer->analyzeFiles(
$this,
$this->threads,
$this->codebase->alter_code,
$this->codebase->find_unused_code === 'always'
);
if ($this->stdout_report_options if ($this->stdout_report_options
&& $this->stdout_report_options->format === Report::TYPE_CONSOLE && $this->stdout_report_options->format === Report::TYPE_CONSOLE

View File

@ -243,8 +243,12 @@ class Analyzer
* *
* @return void * @return void
*/ */
public function analyzeFiles(ProjectAnalyzer $project_analyzer, $pool_size, $alter_code) public function analyzeFiles(
{ ProjectAnalyzer $project_analyzer,
int $pool_size,
bool $alter_code,
bool $consolidate_analyzed_data = false
) {
$this->loadCachedResults($project_analyzer); $this->loadCachedResults($project_analyzer);
$codebase = $project_analyzer->getCodebase(); $codebase = $project_analyzer->getCodebase();
@ -285,7 +289,7 @@ class Analyzer
} else { } else {
$this->progress->finish(); $this->progress->finish();
if ($project_analyzer->full_run || $codebase->find_unused_code === 'always') { if ($consolidate_analyzed_data) {
$project_analyzer->consolidateAnalyzedData(); $project_analyzer->consolidateAnalyzedData();
} }

View File

@ -79,9 +79,7 @@ class ErrorAfterUpdateTest extends \Psalm\Tests\TestCase
$codebase->reloadFiles($this->project_analyzer, array_keys($files)); $codebase->reloadFiles($this->project_analyzer, array_keys($files));
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false); $codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false, true);
$this->project_analyzer->consolidateAnalyzedData();
} }
foreach ($end_files as $file_path => $contents) { foreach ($end_files as $file_path => $contents) {
@ -97,9 +95,7 @@ class ErrorAfterUpdateTest extends \Psalm\Tests\TestCase
$codebase->addFilesToAnalyze([$file_path => $file_path]); $codebase->addFilesToAnalyze([$file_path => $file_path]);
} }
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false); $codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false, true);
$this->project_analyzer->consolidateAnalyzedData();
} }
/** /**

View File

@ -61,9 +61,8 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
bool $test_save = true, bool $test_save = true,
bool $check_unused_code = false bool $check_unused_code = false
) { ) {
$this->project_analyzer->getCodebase()->diff_methods = true;
$codebase = $this->project_analyzer->getCodebase(); $codebase = $this->project_analyzer->getCodebase();
$codebase->diff_methods = true;
if ($check_unused_code) { if ($check_unused_code) {
$codebase->reportUnusedCode(); $codebase->reportUnusedCode();
@ -89,9 +88,7 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
$codebase->scanFiles(); $codebase->scanFiles();
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false); $codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false, true);
$this->project_analyzer->consolidateAnalyzedData();
$data = \Psalm\IssueBuffer::clear(); $data = \Psalm\IssueBuffer::clear();
@ -117,9 +114,7 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
$codebase->reloadFiles($this->project_analyzer, array_keys($file_stage)); $codebase->reloadFiles($this->project_analyzer, array_keys($file_stage));
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false); $codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false, true);
$this->project_analyzer->consolidateAnalyzedData();
$data = \Psalm\IssueBuffer::clear(); $data = \Psalm\IssueBuffer::clear();
@ -147,9 +142,7 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
$codebase->reloadFiles($this->project_analyzer, array_keys($last_file_stage)); $codebase->reloadFiles($this->project_analyzer, array_keys($last_file_stage));
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false); $codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false, true);
$this->project_analyzer->consolidateAnalyzedData();
$data = \Psalm\IssueBuffer::clear(); $data = \Psalm\IssueBuffer::clear();
@ -1452,7 +1445,7 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
'dontForgetErrorInTraitMethod' => [ 'dontForgetErrorInTraitMethod' => [
[ [
[ [
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo; namespace Foo;
class A { class A {
@ -1460,7 +1453,7 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
} }
(new A)->foo();', (new A)->foo();',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'T.php' => '<?php getcwd() . DIRECTORY_SEPARATOR . 'T.php' => '<?php
namespace Foo; namespace Foo;
trait T { trait T {
@ -1470,7 +1463,7 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
}', }',
], ],
[ [
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo; namespace Foo;
class A { class A {
@ -1478,7 +1471,7 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
} }
(new A)->foo();', (new A)->foo();',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'T.php' => '<?php getcwd() . DIRECTORY_SEPARATOR . 'T.php' => '<?php
namespace Foo; namespace Foo;
trait T { trait T {
@ -1490,7 +1483,7 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
], ],
'error_positions' => [[192, 192], [192, 192]], 'error_positions' => [[192, 192], [192, 192]],
], ],
'stillUnused' => [ 'stillUnusedClass' => [
[ [
[ [
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
@ -1523,7 +1516,44 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
], ],
'error_positions' => [[84], [84]], 'error_positions' => [[84], [84]],
[], [],
true, false,
true
],
'stillUnusedMethod' => [
[
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo() : void {}
public function bar() : void {}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'user.php' => '<?php
namespace Foo;
(new A())->foo();',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo() : void {
}
public function bar() : void {}
}',
getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'user.php' => '<?php
namespace Foo;
(new A())->foo();',
],
],
'error_positions' => [[201], [234]],
[],
false,
true true
], ],
]; ];