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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"phpdocumentor/reflection-common": "~2.2",
"phpstan/phpdoc-parser": "~1.23",
"voku/simple-cache": "~5.0",
"nikic/php-parser": "^4.18 || ^5"
"nikic/php-parser": "^4.18 || ^5",
"fidry/cpu-core-counter": "^1.3"
},
"require-dev": {
"phpunit/phpunit": "~9.6",
Expand Down
51 changes: 5 additions & 46 deletions src/voku/SimplePhpParser/Parsers/Helper/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,59 +449,18 @@ public static function modernPhpdoc(string $input): string
}

/**
* @see https://gist.github.com/divinity76/01ef9ca99c111565a72d3a8a6e42f7fb + modified (do not use all cores, we still want to work)
*
* returns number of cpu cores
* Copyleft 2018, license: WTFPL
* Returns number of cpu cores available for parallelisation.
*
* @return int<1, max>
*/
public static function getCpuCores(): int
{
if (\defined('PHP_WINDOWS_VERSION_MAJOR')) {
$str = \trim((string) \shell_exec('wmic cpu get NumberOfCores 2>&1'));
$matches = [];
if (!$str || !\preg_match('#(\d+)#', $str, $matches)) {
return 1;
}

$return = (int)round((int)$matches[1] / 2);
if ($return > 1) {
return $return;
}

return 1;
}

/** @noinspection PhpUsageOfSilenceOperatorInspection */
$ret = @\shell_exec('nproc');
if (\is_string($ret)) {
$ret = \trim($ret);
/** @noinspection PhpAssignmentInConditionInspection */
if ($ret && ($tmp = \filter_var($ret, \FILTER_VALIDATE_INT)) !== false) {
$return = (int)round($tmp / 2);
if ($return > 1) {
return $return;
}

return 1;
}
}

if (\is_readable('/proc/cpuinfo')) {
$cpuinfo = (string) \file_get_contents('/proc/cpuinfo');
$count = \substr_count($cpuinfo, 'processor');
if ($count > 0) {
$return = (int)round($count / 2);
if ($return > 1) {
return $return;
}

return 1;
}
static $cores = null;
if ($cores === null) {
$cores = (new \Fidry\CpuCoreCounter\CpuCoreCounter())->getAvailableForParallelisation()->availableCpus;
}

return 1;
return $cores;
}

/**
Expand Down
Loading