Skip to content
Permalink
Browse files

Allow psalter to be used in multithreaded mode

  • Loading branch information...
muglug committed Apr 17, 2019
1 parent 80e28d6 commit fea9faaec5bb6e7f264f8ca6c090af4d4a0d7f58
@@ -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 @@ public function enterServerMode()
$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 @@ public function collectLocations()
}
/**
* @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;
}
@@ -107,6 +107,11 @@ class ProjectAnalyzer
*/
public $dry_run = false;
/**
* @var bool
*/
public $full_run = false;
/**
* @var bool
*/
@@ -335,6 +340,8 @@ public function check($base_dir, $is_diff = false)
$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 @@ public function checkClassReferences()
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);
}
/**
@@ -39,6 +39,7 @@
* 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 @@ function () {
'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 @@ function () {
}
}
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 @@ function () {
}
$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());
@@ -665,8 +665,12 @@ public function getUnAliasedName(string $alias_name)
/**
* @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);
@@ -57,6 +57,14 @@ public static function getForFile($file_path)
return self::$file_manipulations[$file_path];
}
/**
* @return array<string, FileManipulation[]>
*/
public static function getAll()
{
return self::$file_manipulations;
}
/**
* @return void
*/
@@ -239,7 +239,6 @@ function ($arg) use ($valid_long_options, $valid_short_options) {
$config->visitComposerAutoloadFiles($project_analyzer);
if ($find_dead_code) {
$project_analyzer->getCodebase()->collectReferences();
$project_analyzer->getCodebase()->reportUnusedCode();
}
@@ -559,8 +559,7 @@ function ($arg) {
}
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 @@ function ($arg) {
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'])) {
@@ -15,9 +15,9 @@
$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 @@
-h, --help
Display this help message
--debug
--debug, --debug-by-line
Debug information
-c, --config=psalm.xml
@@ -75,6 +75,9 @@
--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 @@
$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 @@
!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 @@
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 (array_key_exists('find-unused-code', $options)) {
$project_analyzer->checkClassReferences();
}
IssueBuffer::finish($project_analyzer, false, $start_time);
@@ -77,7 +77,6 @@ public function testValidCode($input_code, $output_code, $php_version, array $is
$safe_types
);
$this->project_analyzer->getCodebase()->collectReferences();
$this->project_analyzer->getCodebase()->reportUnusedCode();
$this->analyzeFile($file_path, $context);
@@ -56,7 +56,7 @@ public function testErrorAfterUpdate(
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();

0 comments on commit fea9faa

Please sign in to comment.
You can’t perform that action at this time.