Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jan 28, 2025
1 parent e8c7e21 commit 2db445b
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 60 deletions.
22 changes: 22 additions & 0 deletions src/Psalm/Internal/Fork/AnalyzerTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Psalm\Internal\Fork;

use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Codebase\Analyzer;

/** @internal */
final class AnalyzerTask implements Task
{
public function __construct(private string $file)
{
}
public function run(Channel $channel, Cancellation $cancellation): mixed
{
$codebase = ProjectAnalyzer::getInstance()->getCodebase();
return Analyzer::analysisWorker($codebase->config, $codebase->getProgress(), $this->file);
}
}
20 changes: 0 additions & 20 deletions src/Psalm/Internal/Fork/ForkProcessDoneMessage.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Psalm/Internal/Fork/ForkProcessErrorMessage.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Psalm/Internal/Fork/ForkTaskDoneMessage.php

This file was deleted.

25 changes: 25 additions & 0 deletions src/Psalm/Internal/Fork/InitAnalyzerTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Psalm\Internal\Fork;

use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Codebase\Scanner;

/** @psalm-import-type PoolData from Scanner */
final class InitAnalyzerTask implements Task
{
/** @var PoolData */
private readonly array $data;
public function __construct()
{
$this->data = ShutdownScannerTask::getPoolData();
}
public function run(Channel $channel, Cancellation $cancellation): mixed
{
ProjectAnalyzer::getInstance()->getCodebase()->addThreadData($this->data);
return null;
}
}
34 changes: 34 additions & 0 deletions src/Psalm/Internal/Fork/InitScannerTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Psalm\Internal\Fork;

use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Provider\ClassLikeStorageProvider;
use Psalm\Internal\Provider\FileStorageProvider;

use const PHP_EOL;

final class InitScannerTask implements Task
{
final public function run(Channel $channel, Cancellation $cancellation): mixed
{
$analyzer = ProjectAnalyzer::getInstance();
$analyzer->progress->debug('Initialising forked process for scanning' . PHP_EOL);

$codebase = $analyzer->getCodebase();
$statements_provider = $codebase->statements_provider;

$codebase->scanner->isForked();
FileStorageProvider::deleteAll();
ClassLikeStorageProvider::deleteAll();

$statements_provider->resetDiffs();

$analyzer->progress->debug('Have initialised forked process for scanning' . PHP_EOL);

return null;
}
}
64 changes: 64 additions & 0 deletions src/Psalm/Internal/Fork/InitStartupTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Psalm\Internal\Fork;

use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use Psalm\Config;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\CliUtils;
use Psalm\Internal\ErrorHandler;
use Psalm\Internal\VersionUtils;
use Psalm\IssueBuffer;

use function cli_get_process_title;
use function define;
use function function_exists;
use function gc_collect_cycles;
use function gc_disable;
use function ini_get;
use function ini_set;

final class InitStartupTask implements Task
{
private readonly string $memoryLimit;
private readonly array $server;
private readonly ?string $processTitle;
final public function __construct(private ProjectAnalyzer $analyzer)
{
$this->memoryLimit = ini_get('memory_limit');
$this->server = IssueBuffer::getServer();
if (function_exists('cli_get_process_title')) {
$this->processTitle = cli_get_process_title();
} else {
$this->processTitle = null;
}
}
final public function run(Channel $channel, Cancellation $cancellation): mixed
{
define('PSALM_VERSION', VersionUtils::getPsalmVersion());
define('PHP_PARSER_VERSION', VersionUtils::getPhpParserVersion());

CliUtils::checkRuntimeRequirements();
gc_collect_cycles();
gc_disable();

ErrorHandler::install([]);

ini_set('memory_limit', $this->memoryLimit);

IssueBuffer::captureServer($this->server);

// More initialization is needed but doing just this for now...

ProjectAnalyzer::$instance = $this->analyzer;
Config::setInstance($this->analyzer->getConfig());

/*if (function_exists('cli_set_process_title') && $this->processTitle !== null) {
@cli_set_process_title($this->processTitle);
}*/

return null;
}
}
20 changes: 20 additions & 0 deletions src/Psalm/Internal/Fork/ScannerTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Psalm\Internal\Fork;

use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use Psalm\Internal\Analyzer\ProjectAnalyzer;

/** @internal */
final class ScannerTask implements Task
{
public function __construct(private string $file)
{
}
public function run(Channel $channel, Cancellation $cancellation): mixed
{
return ProjectAnalyzer::getInstance()->getCodebase()->scanner->scanAPath($this->file);
}
}
69 changes: 69 additions & 0 deletions src/Psalm/Internal/Fork/ShutdownAnalyzerTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Psalm\Internal\Fork;

use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Codebase\Analyzer;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use Psalm\Internal\FileManipulation\FunctionDocblockManipulator;
use Psalm\IssueBuffer;

/**
* @internal
* @psalm-import-type WorkerData from Analyzer
* @implements Task<void, void, WorkerData>
*/
final class ShutdownAnalyzerTask implements Task
{
/**
* @return WorkerData
*/
public function run(Channel $channel, Cancellation $cancellation): mixed
{
$project_analyzer = ProjectAnalyzer::getInstance();
$codebase = $project_analyzer->getCodebase();
$analyzer = $codebase->analyzer;
$file_reference_provider = $codebase->file_reference_provider;

$project_analyzer->progress->debug('Gathering data for forked process'."\n");

$channel->send($channel->receive($cancellation));

// @codingStandardsIgnoreStart
return [
'issues' => IssueBuffer::getIssuesData(),
'fixable_issue_counts' => IssueBuffer::getFixableIssues(),
'nonmethod_references_to_classes' => $file_reference_provider->getAllNonMethodReferencesToClasses(),
'method_references_to_classes' => $file_reference_provider->getAllMethodReferencesToClasses(),
'file_references_to_class_members' => $file_reference_provider->getAllFileReferencesToClassMembers(),
'method_references_to_class_members' => $file_reference_provider->getAllMethodReferencesToClassMembers(),
'method_dependencies' => $file_reference_provider->getAllMethodDependencies(),
'file_references_to_class_properties' => $file_reference_provider->getAllFileReferencesToClassProperties(),
'file_references_to_method_returns' => $file_reference_provider->getAllFileReferencesToMethodReturns(),
'method_references_to_class_properties' => $file_reference_provider->getAllMethodReferencesToClassProperties(),
'method_references_to_method_returns' => $file_reference_provider->getAllMethodReferencesToMethodReturns(),
'file_references_to_missing_class_members' => $file_reference_provider->getAllFileReferencesToMissingClassMembers(),
'method_references_to_missing_class_members' => $file_reference_provider->getAllMethodReferencesToMissingClassMembers(),
'method_param_uses' => $file_reference_provider->getAllMethodParamUses(),
'mixed_member_names' => $analyzer->getMixedMemberNames(),
'file_manipulations' => FileManipulationBuffer::getAll(),
'mixed_counts' => $analyzer->getMixedCounts(),
'function_timings' => $analyzer->getFunctionTimings(),
'analyzed_methods' => $analyzer->getAnalyzedMethods(),
'file_maps' => $analyzer->getFileMaps(),
'class_locations' => $file_reference_provider->getAllClassLocations(),
'class_method_locations' => $file_reference_provider->getAllClassMethodLocations(),
'class_property_locations' => $file_reference_provider->getAllClassPropertyLocations(),
'possible_method_param_types' => $analyzer->getPossibleMethodParamTypes(),
'taint_data' => $codebase->taint_flow_graph,
'unused_suppressions' => $codebase->track_unused_suppressions ? IssueBuffer::getUnusedSuppressions() : [],
'used_suppressions' => $codebase->track_unused_suppressions ? IssueBuffer::getUsedSuppressions() : [],
'function_docblock_manipulators' => FunctionDocblockManipulator::getManipulators(),
'mutable_classes' => $codebase->analyzer->mutable_classes,
];
// @codingStandardsIgnoreEnd
}
}
57 changes: 57 additions & 0 deletions src/Psalm/Internal/Fork/ShutdownScannerTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Psalm\Internal\Fork;

use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\Codebase\Scanner;
use Psalm\IssueBuffer;

use const PHP_EOL;

/**
* @internal
* @psalm-import-type PoolData from Scanner
* @implements Task<void, void, PoolData>
*/
final class ShutdownScannerTask implements Task
{
/**
* @return PoolData
*/
public function run(Channel $channel, Cancellation $cancellation): mixed
{
return self::getPoolData();
}
/**
* @return PoolData
*/
public static function getPoolData(): array
{
$project_analyzer = ProjectAnalyzer::getInstance();
$project_analyzer->progress->debug('Collecting data from forked scanner process' . PHP_EOL);

$project_analyzer = ProjectAnalyzer::getInstance();
$codebase = $project_analyzer->getCodebase();
$statements_provider = $codebase->statements_provider;

return [
'classlikes_data' => $codebase->classlikes->getThreadData(),
'scanner_data' => $codebase->scanner->getThreadData(),
'issues' => IssueBuffer::getIssuesData(),
'changed_members' => $statements_provider->getChangedMembers(),
'unchanged_signature_members' => $statements_provider->getUnchangedSignatureMembers(),
'diff_map' => $statements_provider->getDiffMap(),
'deletion_ranges' => $statements_provider->getDeletionRanges(),
'errors' => $statements_provider->getErrors(),
'classlike_storage' => $codebase->classlike_storage_provider->getAll(),
'file_storage' => $codebase->file_storage_provider->getAll(),
'new_file_content_hashes' => $statements_provider->parser_cache_provider
? $statements_provider->parser_cache_provider->getNewFileContentHashes()
: [],
'taint_data' => $codebase->taint_flow_graph,
];
}
}

0 comments on commit 2db445b

Please sign in to comment.