66use PHPStan \Analyser \AnalyserResult ;
77use PHPStan \Parallel \ParallelAnalyser ;
88use PHPStan \Parallel \Scheduler ;
9+ use PHPStan \Process \CpuCoreCounter ;
910use Symfony \Component \Console \Input \InputInterface ;
1011
1112class AnalyserRunner
@@ -17,15 +18,19 @@ class AnalyserRunner
1718
1819 private ParallelAnalyser $ parallelAnalyser ;
1920
21+ private CpuCoreCounter $ cpuCoreCounter ;
22+
2023 public function __construct (
2124 Scheduler $ scheduler ,
2225 Analyser $ analyser ,
23- ParallelAnalyser $ parallelAnalyser
26+ ParallelAnalyser $ parallelAnalyser ,
27+ CpuCoreCounter $ cpuCoreCounter
2428 )
2529 {
2630 $ this ->scheduler = $ scheduler ;
2731 $ this ->analyser = $ analyser ;
2832 $ this ->parallelAnalyser = $ parallelAnalyser ;
33+ $ this ->cpuCoreCounter = $ cpuCoreCounter ;
2934 }
3035
3136 /**
@@ -54,7 +59,7 @@ public function runAnalyser(
5459 return new AnalyserResult ([], [], [], [], false );
5560 }
5661
57- $ schedule = $ this ->scheduler ->scheduleWork ($ this ->getNumberOfCpuCores (), $ files );
62+ $ schedule = $ this ->scheduler ->scheduleWork ($ this ->cpuCoreCounter -> getNumberOfCpuCores (), $ files );
5863 $ mainScript = null ;
5964 if (isset ($ _SERVER ['argv ' ][0 ]) && file_exists ($ _SERVER ['argv ' ][0 ])) {
6065 $ mainScript = $ _SERVER ['argv ' ][0 ];
@@ -77,39 +82,4 @@ public function runAnalyser(
7782 );
7883 }
7984
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-
11585}
0 commit comments