Skip to content

Commit efbe009

Browse files
committed
Refactored part of AnalyseApplication into AnalyserRunner
1 parent e8ee74f commit efbe009

File tree

3 files changed

+123
-78
lines changed

3 files changed

+123
-78
lines changed

conf/config.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ services:
410410
memoryLimitFile: %memoryLimitFile%
411411
internalErrorsCountLimit: %internalErrorsCountLimit%
412412

413+
-
414+
class: PHPStan\Command\AnalyserRunner
415+
413416
-
414417
class: PHPStan\Command\IgnoredRegexValidator
415418
arguments:

src/Command/AnalyseApplication.php

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@
22

33
namespace PHPStan\Command;
44

5-
use PHPStan\Analyser\Analyser;
65
use PHPStan\Analyser\AnalyserResult;
76
use PHPStan\Analyser\IgnoredErrorHelper;
87
use PHPStan\Analyser\ResultCache\ResultCacheManager;
9-
use PHPStan\Parallel\ParallelAnalyser;
10-
use PHPStan\Parallel\Scheduler;
118
use PHPStan\PhpDoc\StubValidator;
129
use Symfony\Component\Console\Input\InputInterface;
13-
use function file_exists;
1410

1511
class AnalyseApplication
1612
{
1713

18-
private \PHPStan\Analyser\Analyser $analyser;
14+
private AnalyserRunner $analyserRunner;
1915

2016
private \PHPStan\PhpDoc\StubValidator $stubValidator;
2117

22-
private ParallelAnalyser $parallelAnalyser;
23-
24-
private Scheduler $scheduler;
25-
2618
private \PHPStan\Analyser\ResultCache\ResultCacheManager $resultCacheManager;
2719

2820
private IgnoredErrorHelper $ignoredErrorHelper;
@@ -32,20 +24,16 @@ class AnalyseApplication
3224
private int $internalErrorsCountLimit;
3325

3426
public function __construct(
35-
Analyser $analyser,
27+
AnalyserRunner $analyserRunner,
3628
StubValidator $stubValidator,
37-
ParallelAnalyser $parallelAnalyser,
38-
Scheduler $scheduler,
3929
ResultCacheManager $resultCacheManager,
4030
IgnoredErrorHelper $ignoredErrorHelper,
4131
string $memoryLimitFile,
4232
int $internalErrorsCountLimit
4333
)
4434
{
45-
$this->analyser = $analyser;
35+
$this->analyserRunner = $analyserRunner;
4636
$this->stubValidator = $stubValidator;
47-
$this->parallelAnalyser = $parallelAnalyser;
48-
$this->scheduler = $scheduler;
4937
$this->resultCacheManager = $resultCacheManager;
5038
$this->ignoredErrorHelper = $ignoredErrorHelper;
5139
$this->memoryLimitFile = $memoryLimitFile;
@@ -175,18 +163,14 @@ private function runAnalyser(
175163
$progressStarted = false;
176164
$fileOrder = 0;
177165
$preFileCallback = null;
178-
$postFileCallback = function (int $step) use ($errorOutput, &$progressStarted, $allAnalysedFilesCount, $filesCount, &$fileOrder, &$runningInParallel): void {
166+
$postFileCallback = function (int $step) use ($errorOutput, &$progressStarted, $allAnalysedFilesCount, $filesCount, &$fileOrder): void {
179167
if (!$progressStarted) {
180168
$errorOutput->getStyle()->progressStart($allAnalysedFilesCount);
181169
$errorOutput->getStyle()->progressAdvance($allAnalysedFilesCount - $filesCount);
182170
$progressStarted = true;
183171
}
184172
$errorOutput->getStyle()->progressAdvance($step);
185173

186-
if ($runningInParallel) {
187-
return;
188-
}
189-
190174
if ($fileOrder >= 100) {
191175
$this->updateMemoryLimitFile();
192176
$fileOrder = 0;
@@ -200,29 +184,7 @@ private function runAnalyser(
200184
$postFileCallback = null;
201185
}
202186

203-
// todo what about hyperthreading? should I divide CPU cores by 2?
204-
$schedule = $this->scheduler->scheduleWork($this->getNumberOfCpuCores(), $files);
205-
$mainScript = null;
206-
if (isset($_SERVER['argv'][0]) && file_exists($_SERVER['argv'][0])) {
207-
$mainScript = $_SERVER['argv'][0];
208-
}
209-
210-
if (
211-
!$debug
212-
&& $mainScript !== null
213-
&& $schedule->getNumberOfProcesses() > 1
214-
) {
215-
$runningInParallel = true;
216-
$analyserResult = $this->parallelAnalyser->analyse($schedule, $mainScript, $postFileCallback, $projectConfigFile, $input);
217-
} else {
218-
$analyserResult = $this->analyser->analyse(
219-
$files,
220-
$preFileCallback,
221-
$postFileCallback,
222-
$debug,
223-
$allAnalysedFiles
224-
);
225-
}
187+
$analyserResult = $this->analyserRunner->runAnalyser($files, $allAnalysedFiles, $preFileCallback, $postFileCallback, $debug, $projectConfigFile, $input);
226188

227189
if (isset($progressStarted) && $progressStarted) {
228190
$errorOutput->getStyle()->progressFinish();
@@ -238,39 +200,4 @@ private function updateMemoryLimitFile(): void
238200
file_put_contents($this->memoryLimitFile, sprintf('%d MB', $megabytes));
239201
}
240202

241-
private function getNumberOfCpuCores(): int
242-
{
243-
// from brianium/paratest
244-
$cores = 2;
245-
if (is_file('/proc/cpuinfo')) {
246-
// Linux (and potentially Windows with linux sub systems)
247-
$cpuinfo = @file_get_contents('/proc/cpuinfo');
248-
if ($cpuinfo !== false) {
249-
preg_match_all('/^processor/m', $cpuinfo, $matches);
250-
return count($matches[0]);
251-
}
252-
}
253-
254-
if (\DIRECTORY_SEPARATOR === '\\') {
255-
// Windows
256-
$process = @popen('wmic cpu get NumberOfLogicalProcessors', 'rb');
257-
if ($process !== false) {
258-
fgets($process);
259-
$cores = (int) fgets($process);
260-
pclose($process);
261-
}
262-
263-
return $cores;
264-
}
265-
266-
$process = @\popen('sysctl -n hw.ncpu', 'rb');
267-
if ($process !== false) {
268-
// *nix (Linux, BSD and Mac)
269-
$cores = (int) fgets($process);
270-
pclose($process);
271-
}
272-
273-
return $cores;
274-
}
275-
276203
}

src/Command/AnalyserRunner.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Command;
4+
5+
use PHPStan\Analyser\Analyser;
6+
use PHPStan\Analyser\AnalyserResult;
7+
use PHPStan\Parallel\ParallelAnalyser;
8+
use PHPStan\Parallel\Scheduler;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
11+
class AnalyserRunner
12+
{
13+
14+
private Scheduler $scheduler;
15+
16+
private Analyser $analyser;
17+
18+
private ParallelAnalyser $parallelAnalyser;
19+
20+
public function __construct(
21+
Scheduler $scheduler,
22+
Analyser $analyser,
23+
ParallelAnalyser $parallelAnalyser
24+
)
25+
{
26+
$this->scheduler = $scheduler;
27+
$this->analyser = $analyser;
28+
$this->parallelAnalyser = $parallelAnalyser;
29+
}
30+
31+
/**
32+
* @param string[] $files
33+
* @param string[] $allAnalysedFiles
34+
* @param \Closure|null $preFileCallback
35+
* @param \Closure|null $postFileCallback
36+
* @param bool $debug
37+
* @param string|null $projectConfigFile
38+
* @param InputInterface $input
39+
* @return AnalyserResult
40+
* @throws \Throwable
41+
*/
42+
public function runAnalyser(
43+
array $files,
44+
array $allAnalysedFiles,
45+
?\Closure $preFileCallback,
46+
?\Closure $postFileCallback,
47+
bool $debug,
48+
?string $projectConfigFile,
49+
InputInterface $input
50+
): AnalyserResult
51+
{
52+
$filesCount = count($files);
53+
if ($filesCount === 0) {
54+
return new AnalyserResult([], [], [], false);
55+
}
56+
57+
$schedule = $this->scheduler->scheduleWork($this->getNumberOfCpuCores(), $files);
58+
$mainScript = null;
59+
if (isset($_SERVER['argv'][0]) && file_exists($_SERVER['argv'][0])) {
60+
$mainScript = $_SERVER['argv'][0];
61+
}
62+
63+
if (
64+
!$debug
65+
&& $mainScript !== null
66+
&& $schedule->getNumberOfProcesses() > 1
67+
) {
68+
return $this->parallelAnalyser->analyse($schedule, $mainScript, $postFileCallback, $projectConfigFile, $input);
69+
}
70+
71+
return $this->analyser->analyse(
72+
$files,
73+
$preFileCallback,
74+
$postFileCallback,
75+
$debug,
76+
$allAnalysedFiles
77+
);
78+
}
79+
80+
private function getNumberOfCpuCores(): int
81+
{
82+
// from brianium/paratest
83+
$cores = 2;
84+
if (is_file('/proc/cpuinfo')) {
85+
// Linux (and potentially Windows with linux sub systems)
86+
$cpuinfo = @file_get_contents('/proc/cpuinfo');
87+
if ($cpuinfo !== false) {
88+
preg_match_all('/^processor/m', $cpuinfo, $matches);
89+
return count($matches[0]);
90+
}
91+
}
92+
93+
if (\DIRECTORY_SEPARATOR === '\\') {
94+
// Windows
95+
$process = @popen('wmic cpu get NumberOfLogicalProcessors', 'rb');
96+
if ($process !== false) {
97+
fgets($process);
98+
$cores = (int) fgets($process);
99+
pclose($process);
100+
}
101+
102+
return $cores;
103+
}
104+
105+
$process = @\popen('sysctl -n hw.ncpu', 'rb');
106+
if ($process !== false) {
107+
// *nix (Linux, BSD and Mac)
108+
$cores = (int) fgets($process);
109+
pclose($process);
110+
}
111+
112+
return $cores;
113+
}
114+
115+
}

0 commit comments

Comments
 (0)