Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/baseline-7.4.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ parameters:
path: ../src/Parallel/ParallelAnalyser.php

-
message: "#^Class PHPStan\\\\Parallel\\\\Process has an uninitialized property \\$process\\. Give it default value or assign it in the constructor\\.$#"
message: "#^Class PHPStan\\\\Parallel\\\\SpawnedProcess has an uninitialized property \\$process\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: ../src/Parallel/Process.php
path: ../src/Parallel/SpawnedProcess.php
-
message: "#^Class PHPStan\\\\PhpDoc\\\\ResolvedPhpDocBlock has an uninitialized property \\$phpDocNodes\\. Give it default value or assign it in the constructor\\.$#"
count: 1
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
"phpstan/phpstan": "2.1.x",
"symfony/polyfill-php73": "*"
},
"suggest": {
"ext-pcntl": "Enables forking parallel analysis workers from the already-booted process (experimental, skips the per-worker re-boot)",
"ext-posix": "Used together with ext-pcntl for forked parallel analysis workers"
},
"require-dev": {
"cweagans/composer-patches": "^1.7.3",
"php-parallel-lint/php-parallel-lint": "^1.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ private function runFixer(InceptionResult $inceptionResult, Container $container
$fixerApplication = $container->getByType(FixerApplication::class);

return $fixerApplication->run(
$inceptionResult->getProjectConfigFile(),
$inceptionResult,
$input,
$output,
count($files),
Expand Down
2 changes: 1 addition & 1 deletion src/Command/AnalyserRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function runAnalyser(
if ($mainScript !== null && $schedule->getNumberOfProcesses() > 0) {
$loop = new StreamSelectLoop();
$result = null;
$promise = $this->parallelAnalyser->analyse($loop, $schedule, $mainScript, $postFileCallback, $projectConfigFile, $tmpFile, $insteadOfFile, $input, null);
$promise = $this->parallelAnalyser->analyse($loop, $schedule, $allAnalysedFiles, $mainScript, $postFileCallback, $projectConfigFile, $tmpFile, $insteadOfFile, $input, null);
$promise->then(static function (AnalyserResult $tmp) use (&$result): void {
$result = $tmp;
});
Expand Down
78 changes: 68 additions & 10 deletions src/Command/FixerApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
use PHPStan\File\FileMonitorResult;
use PHPStan\File\FileReader;
use PHPStan\File\FileWriter;
use PHPStan\File\PathNotFoundException;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Internal\DirectoryCreator;
use PHPStan\Internal\DirectoryCreatorException;
use PHPStan\Internal\HttpClientFactory;
use PHPStan\Parallel\ForkParallelChecker;
use PHPStan\PhpDoc\StubFilesProvider;
use PHPStan\Process\ForkedProcessPromise;
use PHPStan\Process\ProcessCanceledException;
use PHPStan\Process\ProcessCrashedException;
use PHPStan\Process\ProcessHelper;
use PHPStan\Process\ProcessPromise;
use PHPStan\Process\SpawnedProcessPromise;
use PHPStan\ShouldNotHappenException;
use React\ChildProcess\Process;
use React\EventLoop\LoopInterface;
Expand Down Expand Up @@ -94,18 +98,22 @@ public function __construct(
#[AutowiredParameter]
private string $usedLevel,
private HttpClientFactory $httpClientFactory,
private ForkParallelChecker $forkParallelChecker,
private FixerWorkerRunner $fixerWorkerRunner,
)
{
}

public function run(
?string $projectConfigFile,
InceptionResult $inceptionResult,
InputInterface $input,
OutputInterface $output,
int $filesCount,
string $mainScript,
): int
{
$projectConfigFile = $inceptionResult->getProjectConfigFile();

$loop = new StreamSelectLoop();
$server = new TcpServer('127.0.0.1:0', $loop);
/** @var string $serverAddress */
Expand All @@ -114,7 +122,7 @@ public function run(
/** @var int<0, 65535> $serverPort */
$serverPort = parse_url($serverAddress, PHP_URL_PORT);

$server->on('connection', function (ConnectionInterface $connection) use ($loop, $projectConfigFile, $input, $output, $mainScript, $filesCount): void {
$server->on('connection', function (ConnectionInterface $connection) use ($loop, $inceptionResult, $projectConfigFile, $input, $output, $mainScript, $filesCount): void {
// phpcs:disable SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly
$jsonInvalidUtf8Ignore = defined('JSON_INVALID_UTF8_IGNORE') ? JSON_INVALID_UTF8_IGNORE : 0;
// phpcs:enable
Expand Down Expand Up @@ -157,14 +165,15 @@ public function run(

$this->analyse(
$loop,
$inceptionResult,
$mainScript,
$projectConfigFile,
$input,
$output,
$encoder,
);

$this->monitorFileChanges($loop, function (FileMonitorResult $changes) use ($loop, $mainScript, $projectConfigFile, $input, $encoder, $output): void {
$this->monitorFileChanges($loop, function (FileMonitorResult $changes) use ($loop, $inceptionResult, $mainScript, $projectConfigFile, $input, $encoder, $output): void {
if ($this->processInProgress !== null) {
$this->processInProgress->cancel();
$this->processInProgress = null;
Expand All @@ -178,6 +187,7 @@ public function run(

$this->analyse(
$loop,
$inceptionResult,
$mainScript,
$projectConfigFile,
$input,
Expand Down Expand Up @@ -417,6 +427,7 @@ private function monitorFileChanges(LoopInterface $loop, callable $hasChangesCal

private function analyse(
LoopInterface $loop,
InceptionResult $inceptionResult,
string $mainScript,
?string $projectConfigFile,
InputInterface $input,
Expand Down Expand Up @@ -446,16 +457,16 @@ private function analyse(
});
});

$process = new ProcessPromise($loop, ProcessHelper::getWorkerCommand(
$process = $this->createProcessPromise(
$this->forkParallelChecker->isSupported(),
$loop,
$server,
$mainScript,
'fixer:worker',
$projectConfigFile,
[
'--server-port',
(string) $serverPort,
],
$input,
));
$serverPort,
$inceptionResult,
);
$this->processInProgress = $process->run();

$this->processInProgress->then(function () use ($server): void {
Expand Down Expand Up @@ -566,4 +577,51 @@ private function getStubFiles(): array
return $stubFiles;
}

/**
* @param int<0, 65535> $serverPort
*/
private function createProcessPromise(
bool $useFork,
LoopInterface $loop,
TcpServer $server,
string $mainScript,
?string $projectConfigFile,
InputInterface $input,
int $serverPort,
InceptionResult $inceptionResult,
): ProcessPromise
{
if ($useFork) {
try {
[$inceptionFiles, $isOnlyFiles] = $inceptionResult->getFiles();
} catch (PathNotFoundException | InceptionNotSuccessfulException) {
throw new ShouldNotHappenException();
}

return new ForkedProcessPromise(
$loop,
$this->fixerWorkerRunner,
$server,
$inceptionResult->getErrorOutput(),
$inceptionFiles,
$isOnlyFiles,
$inceptionResult->getProjectConfigArray(),
$projectConfigFile,
$serverPort,
$input,
);
}

return new SpawnedProcessPromise($loop, ProcessHelper::getWorkerCommand(
$mainScript,
'fixer:worker',
$projectConfigFile,
[
'--server-port',
(string) $serverPort,
],
$input,
));
}

}
Loading
Loading