33namespace Psalm \Internal \Analyzer ;
44
55use Amp \Loop ;
6+ use Fidry \CpuCoreCounter \CpuCoreCounter ;
7+ use Fidry \CpuCoreCounter \NumberOfCpuCoreNotFound ;
68use InvalidArgumentException ;
7- use LogicException ;
89use Psalm \Codebase ;
910use Psalm \Config ;
1011use Psalm \Context ;
5354use Psalm \Report \ReportOptions ;
5455use Psalm \Type ;
5556use ReflectionProperty ;
56- use RuntimeException ;
5757use UnexpectedValueException ;
5858
5959use function array_combine ;
7373use function explode ;
7474use function extension_loaded ;
7575use function file_exists ;
76- use function file_get_contents ;
77- use function filter_var ;
78- use function function_exists ;
7976use function fwrite ;
8077use function implode ;
8178use function in_array ;
8279use function ini_get ;
8380use function is_dir ;
8481use function is_file ;
85- use function is_int ;
86- use function is_readable ;
87- use function is_string ;
8882use function microtime ;
8983use function mkdir ;
9084use function number_format ;
9185use function pcntl_fork ;
9286use function preg_match ;
9387use function rename ;
94- use function shell_exec ;
9588use function stream_set_blocking ;
9689use function stream_socket_accept ;
9790use function stream_socket_client ;
10093use function strpos ;
10194use function strtolower ;
10295use function substr ;
103- use function substr_count ;
104- use function trim ;
10596use function usort ;
10697use function version_compare ;
10798
108- use const FILTER_VALIDATE_INT ;
10999use const PHP_EOL ;
110100use const PHP_OS ;
111101use const PHP_VERSION ;
@@ -1468,23 +1458,16 @@ public function getFunctionLikeAnalyzer(
14681458 * Adapted from https://gist.github.com/divinity76/01ef9ca99c111565a72d3a8a6e42f7fb
14691459 * returns number of cpu cores
14701460 * Copyleft 2018, license: WTFPL
1471- * @throws RuntimeException
1472- * @throws LogicException
1473- * @psalm-suppress ForbiddenCode
1461+ * @throws NumberOfCpuCoreNotFound
14741462 */
14751463 public static function getCpuCount (): int
14761464 {
14771465 if (defined ('PHP_WINDOWS_VERSION_MAJOR ' )) {
1478- /*
1479- $str = trim((string) shell_exec('wmic cpu get NumberOfCores 2>&1'));
1480- if (!preg_match('/(\d+)/', $str, $matches)) {
1481- throw new RuntimeException('wmic failed to get number of cpu cores on windows!');
1482- }
1483- return ((int) $matches [1]);
1484- */
1466+ // No support desired for Windows at the moment
14851467 return 1 ;
14861468 }
14871469
1470+ // PHP 7.3 with JIT on OSX is screwed for multi-threads
14881471 if (ini_get ('pcre.jit ' ) === '1 '
14891472 && PHP_OS === 'Darwin '
14901473 && version_compare (PHP_VERSION , '7.3.0 ' ) >= 0
@@ -1493,40 +1476,12 @@ public static function getCpuCount(): int
14931476 return 1 ;
14941477 }
14951478
1496- if (!extension_loaded ('pcntl ' ) || !function_exists ('shell_exec ' )) {
1479+ if (!extension_loaded ('pcntl ' )) {
1480+ // Psalm requires pcntl for multi-threads support
14971481 return 1 ;
14981482 }
14991483
1500- $ has_nproc = trim ((string ) @shell_exec ('command -v nproc ' ));
1501- if ($ has_nproc ) {
1502- $ ret = @shell_exec ('nproc ' );
1503- if (is_string ($ ret )) {
1504- $ ret = trim ($ ret );
1505- $ tmp = filter_var ($ ret , FILTER_VALIDATE_INT );
1506- if (is_int ($ tmp )) {
1507- return $ tmp ;
1508- }
1509- }
1510- }
1511-
1512- $ ret = @shell_exec ('sysctl -n hw.ncpu ' );
1513- if (is_string ($ ret )) {
1514- $ ret = trim ($ ret );
1515- $ tmp = filter_var ($ ret , FILTER_VALIDATE_INT );
1516- if (is_int ($ tmp )) {
1517- return $ tmp ;
1518- }
1519- }
1520-
1521- if (is_readable ('/proc/cpuinfo ' )) {
1522- $ cpuinfo = file_get_contents ('/proc/cpuinfo ' );
1523- $ count = substr_count ($ cpuinfo , 'processor ' );
1524- if ($ count > 0 ) {
1525- return $ count ;
1526- }
1527- }
1528-
1529- throw new LogicException ('failed to detect number of CPUs! ' );
1484+ return (new CpuCoreCounter ())->getCount ();
15301485 }
15311486
15321487 /**
0 commit comments