1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Allow psalter to be used in multithreaded mode

This commit is contained in:
Brown 2019-04-17 13:15:06 -04:00
parent 80e28d6a4a
commit fea9faaec5
10 changed files with 67 additions and 42 deletions

View File

@ -59,6 +59,11 @@ class Codebase
*/
public $collect_locations = false;
/**
* @var null|'always'|'auto'
*/
public $find_unused_code = null;
/**
* @var FileProvider
*/
@ -98,11 +103,6 @@ class Codebase
*/
public $register_stub_files = false;
/**
* @var bool
*/
public $find_unused_code = false;
/**
* @var bool
*/
@ -331,15 +331,6 @@ class Codebase
$this->store_node_types = true;
}
/**
* @return void
*/
public function collectReferences()
{
$this->collect_references = true;
$this->classlikes->collect_references = true;
}
/**
* @return void
*/
@ -352,12 +343,15 @@ class Codebase
}
/**
* @param 'always'|'auto' $find_unused_code
*
* @return void
*/
public function reportUnusedCode()
public function reportUnusedCode(string $find_unused_code = 'auto')
{
$this->collectReferences();
$this->find_unused_code = true;
$this->collect_references = true;
$this->classlikes->collect_references = true;
$this->find_unused_code = $find_unused_code;
$this->find_unused_variables = true;
}

View File

@ -107,6 +107,11 @@ class ProjectAnalyzer
*/
public $dry_run = false;
/**
* @var bool
*/
public $full_run = false;
/**
* @var bool
*/
@ -335,6 +340,8 @@ class ProjectAnalyzer
$diff_files = null;
$deleted_files = null;
$this->full_run = !$is_diff;
$reference_cache = $this->file_reference_provider->loadReferenceCache(true);
if ($is_diff
@ -426,7 +433,7 @@ class ProjectAnalyzer
throw new \UnexpectedValueException('Should not be checking references');
}
$this->codebase->classlikes->checkClassReferences($this->codebase->methods);
$this->codebase->classlikes->checkClassReferences($this->codebase->methods, $this->debug_output);
}
/**

View File

@ -39,6 +39,7 @@ use Psalm\Internal\Provider\FileStorageProvider;
* file_references_to_missing_class_members: array<string, array<string,bool>>,
* mixed_counts: array<string, array{0: int, 1: int}>,
* mixed_member_names: array<string, bool>,
* file_manipulations: array<string, FileManipulation[]>,
* method_references_to_class_members: array<string, array<string,bool>>,
* method_references_to_missing_class_members: array<string, array<string,bool>>,
* analyzed_methods: array<string, array<string, int>>,
@ -269,6 +270,7 @@ class Analyzer
'method_references_to_missing_class_members'
=> $file_reference_provider->getAllMethodReferencesToMissingClassMembers(),
'mixed_member_names' => $analyzer->getMixedMemberNames(),
'file_manipulations' => FileManipulationBuffer::getAll(),
'mixed_counts' => $analyzer->getMixedCounts(),
'analyzed_methods' => $analyzer->getAnalyzedMethods(),
'file_maps' => $analyzer->getFileMaps(),
@ -339,6 +341,10 @@ class Analyzer
}
}
foreach ($pool_data['file_manipulations'] as $file_path => $manipulations) {
FileManipulationBuffer::add($file_path, $manipulations);
}
foreach ($pool_data['file_maps'] as $file_path => list($reference_map, $type_map)) {
$this->reference_map[$file_path] = $reference_map;
$this->type_map[$file_path] = $type_map;
@ -362,6 +368,13 @@ class Analyzer
}
$codebase = $project_analyzer->getCodebase();
if ($codebase->collect_references
&& ($project_analyzer->full_run || $codebase->find_unused_code === 'always')
) {
$project_analyzer->checkClassReferences();
}
$scanned_files = $codebase->scanner->getScannedFiles();
$codebase->file_reference_provider->setAnalyzedMethods($this->analyzed_methods);
$codebase->file_reference_provider->setFileMaps($this->getFileMaps());

View File

@ -665,8 +665,12 @@ class ClassLikes
/**
* @return void
*/
public function checkClassReferences(Methods $methods)
public function checkClassReferences(Methods $methods, bool $debug_output = false)
{
if ($debug_output) {
echo 'Checking class references' . PHP_EOL;
}
foreach ($this->existing_classlikes_lc as $fq_class_name_lc => $_) {
try {
$classlike_storage = $this->classlike_storage_provider->get($fq_class_name_lc);

View File

@ -57,6 +57,14 @@ class FileManipulationBuffer
return self::$file_manipulations[$file_path];
}
/**
* @return array<string, FileManipulation[]>
*/
public static function getAll()
{
return self::$file_manipulations;
}
/**
* @return void
*/

View File

@ -239,7 +239,6 @@ if (isset($options['disable-on-change'])) {
$config->visitComposerAutoloadFiles($project_analyzer);
if ($find_dead_code) {
$project_analyzer->getCodebase()->collectReferences();
$project_analyzer->getCodebase()->reportUnusedCode();
}

View File

@ -559,8 +559,7 @@ if ($find_references_to !== null) {
}
if ($find_unused_code) {
$project_analyzer->getCodebase()->collectReferences();
$project_analyzer->getCodebase()->reportUnusedCode();
$project_analyzer->getCodebase()->reportUnusedCode($find_unused_code);
}
if ($config->find_unused_variables) {
@ -580,14 +579,6 @@ if ($paths_to_check === null) {
if ($find_references_to) {
$project_analyzer->findReferencesTo($find_references_to);
} elseif (($find_unused_code === 'always') || ($find_unused_code === 'auto' && !$paths_to_check)) {
if ($is_diff) {
if ($output_format === ProjectAnalyzer::TYPE_CONSOLE) {
echo 'Unused classes and methods cannot currently be found in --diff mode' . PHP_EOL;
}
} else {
$project_analyzer->checkClassReferences();
}
}
if (isset($options['set-baseline']) && is_string($options['set-baseline'])) {

View File

@ -15,9 +15,9 @@ ini_set('memory_limit', '4096M');
$options = getopt(
'f:mhr:',
[
'help', 'debug', 'config:', 'file:', 'root:',
'help', 'debug', 'debug-by-line', 'config:', 'file:', 'root:',
'plugin:', 'issues:', 'php-version:', 'dry-run', 'safe-types',
'find-unused-code',
'find-unused-code', 'threads:',
]
);
@ -46,7 +46,7 @@ Options:
-h, --help
Display this help message
--debug
--debug, --debug-by-line
Debug information
-c, --config=psalm.xml
@ -75,6 +75,9 @@ Options:
--find-dead-code
Include unused code as a candidate for removal
--threads=INT
If greater than one, Psalm will run analysis on multiple threads, speeding things up.
HELP;
exit;
@ -130,6 +133,8 @@ if ($path_to_config) {
$config->setComposerClassLoader($first_autoloader);
$threads = isset($options['threads']) ? (int)$options['threads'] : 1;
$project_analyzer = new ProjectAnalyzer(
$config,
new Psalm\Internal\Provider\Providers(
@ -141,10 +146,14 @@ $project_analyzer = new ProjectAnalyzer(
!array_key_exists('m', $options),
false,
ProjectAnalyzer::TYPE_CONSOLE,
1,
$threads,
array_key_exists('debug', $options)
);
if (array_key_exists('debug-by-line', $options)) {
$project_analyzer->debug_lines = true;
}
$config->visitComposerAutoloadFiles($project_analyzer);
if (array_key_exists('issues', $options)) {
@ -186,8 +195,13 @@ foreach ($plugins as $plugin_path) {
Config::getInstance()->addPluginPath($current_dir . $plugin_path);
}
if (array_key_exists('find-unused-code', $options)) {
$project_analyzer->getCodebase()->collectReferences();
$find_unused_code = array_key_exists('find-unused-code', $options);
if ($config->find_unused_code) {
$find_unused_code = true;
}
if ($find_unused_code) {
$project_analyzer->getCodebase()->reportUnusedCode();
}
@ -211,8 +225,4 @@ if ($paths_to_check === null) {
}
}
if (array_key_exists('find-unused-code', $options)) {
$project_analyzer->checkClassReferences();
}
IssueBuffer::finish($project_analyzer, false, $start_time);

View File

@ -77,7 +77,6 @@ class FileManipulationTest extends TestCase
$safe_types
);
$this->project_analyzer->getCodebase()->collectReferences();
$this->project_analyzer->getCodebase()->reportUnusedCode();
$this->analyzeFile($file_path, $context);

View File

@ -56,7 +56,7 @@ class ErrorAfterUpdateTest extends \Psalm\Tests\TestCase
array $error_levels = []
) {
$this->project_analyzer->getCodebase()->diff_methods = true;
$this->project_analyzer->getCodebase()->collectReferences();
$this->project_analyzer->getCodebase()->reportUnusedCode();
$codebase = $this->project_analyzer->getCodebase();