22
33namespace PHPStan \Command ;
44
5- use PHPStan \Analyser \Analyser ;
65use PHPStan \Analyser \AnalyserResult ;
76use PHPStan \Analyser \IgnoredErrorHelper ;
87use PHPStan \Analyser \ResultCache \ResultCacheManager ;
9- use PHPStan \Parallel \ParallelAnalyser ;
10- use PHPStan \Parallel \Scheduler ;
118use PHPStan \PhpDoc \StubValidator ;
129use Symfony \Component \Console \Input \InputInterface ;
13- use function file_exists ;
1410
1511class 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}
0 commit comments