Install psalm as dev, sync scripts updates

This commit is contained in:
Clemens Schwaighofer
2023-03-09 16:27:10 +09:00
parent 6bec59e387
commit feba79a2e8
2099 changed files with 283333 additions and 32 deletions

View File

@@ -0,0 +1,89 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter;
use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
use Fidry\CpuCoreCounter\Finder\FinderRegistry;
final class CpuCoreCounter
{
/**
* @var list<CpuCoreFinder>
*/
private $finders;
/**
* @var positive-int|null
*/
private $count;
/**
* @param list<CpuCoreFinder>|null $finders
*/
public function __construct(?array $finders = null)
{
$this->finders = $finders ?? FinderRegistry::getDefaultLogicalFinders();
}
/**
* @throws NumberOfCpuCoreNotFound
*
* @return positive-int
*/
public function getCount(): int
{
// Memoize result
if (null === $this->count) {
$this->count = $this->findCount();
}
return $this->count;
}
/**
* @throws NumberOfCpuCoreNotFound
*
* @return positive-int
*/
private function findCount(): int
{
foreach ($this->finders as $finder) {
$cores = $finder->find();
if (null !== $cores) {
return $cores;
}
}
throw NumberOfCpuCoreNotFound::create();
}
/**
* @throws NumberOfCpuCoreNotFound
*
* @return array{CpuCoreFinder, positive-int}
*/
public function getFinderAndCores(): array
{
foreach ($this->finders as $finder) {
$cores = $finder->find();
if (null !== $cores) {
return [$finder, $cores];
}
}
throw NumberOfCpuCoreNotFound::create();
}
}

View File

@@ -0,0 +1,101 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter;
use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
use function array_map;
use function explode;
use function implode;
use function max;
use function str_repeat;
use const PHP_EOL;
/**
* Utility to debug.
*
* @private
*/
final class Diagnoser
{
/**
* Provides an aggregated diagnosis based on each finders diagnosis.
*
* @param list<CpuCoreFinder> $finders
*/
public static function diagnose(array $finders): string
{
$diagnoses = array_map(
static function (CpuCoreFinder $finder): string {
return self::diagnoseFinder($finder);
},
$finders
);
return implode(PHP_EOL, $diagnoses);
}
/**
* Executes each finders.
*
* @param list<CpuCoreFinder> $finders
*/
public static function execute(array $finders): string
{
$diagnoses = array_map(
static function (CpuCoreFinder $finder): string {
$coresCount = $finder->find();
return implode(
'',
[
$finder->toString(),
': ',
null === $coresCount ? 'NULL' : $coresCount,
]
);
},
$finders
);
return implode(PHP_EOL, $diagnoses);
}
private static function diagnoseFinder(CpuCoreFinder $finder): string
{
$diagnosis = $finder->diagnose();
$maxLineLength = max(
array_map(
'strlen',
explode(PHP_EOL, $diagnosis)
)
);
$separator = str_repeat('-', $maxLineLength);
return implode(
'',
[
$finder->toString().':'.PHP_EOL,
$separator.PHP_EOL,
$diagnosis.PHP_EOL,
$separator.PHP_EOL,
]
);
}
private function __construct()
{
}
}

View File

@@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Executor;
use function fclose;
use function function_exists;
use function is_resource;
use function proc_close;
use function proc_open;
use function stream_get_contents;
final class ProcOpenExecutor implements ProcessExecutor
{
public function execute(string $command): ?array
{
if (!function_exists('proc_open')) {
return null;
}
$pipes = [];
$process = @proc_open(
$command,
[
['pipe', 'rb'],
['pipe', 'wb'], // stdout
['pipe', 'wb'], // stderr
],
$pipes
);
if (!is_resource($process)) {
return null;
}
fclose($pipes[0]);
$stdout = (string) stream_get_contents($pipes[1]);
$stderr = (string) stream_get_contents($pipes[2]);
proc_close($process);
return [$stdout, $stderr];
}
}

View File

@@ -0,0 +1,22 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Executor;
interface ProcessExecutor
{
/**
* @return array{string, string}|null STDOUT & STDERR tuple
*/
public function execute(string $command): ?array;
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
interface CpuCoreFinder
{
/**
* Provides an explanation which may offer some insight as to what the finder
* will be able to find.
*
* This is practical to have an idea of what each finder will find collect
* information for the unit tests, since integration tests are quite complicated
* as dependent on complex infrastructures.
*/
public function diagnose(): string;
/**
* Find the number of CPU cores. If it could not find it, returns null. The
* means used to find the cores are at the implementation discretion.
*
* @return positive-int|null
*/
public function find(): ?int;
public function toString(): string;
}

View File

@@ -0,0 +1,98 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
use function file_get_contents;
use function is_file;
use function sprintf;
use function substr_count;
use const PHP_EOL;
/**
* Find the number of CPU cores looking up at the cpuinfo file which is available
* on Linux systems and Windows systems with a Linux sub-system.
*
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L903-L909
* @see https://unix.stackexchange.com/questions/146051/number-of-processors-in-proc-cpuinfo
*/
final class CpuInfoFinder implements CpuCoreFinder
{
private const CPU_INFO_PATH = '/proc/cpuinfo';
public function diagnose(): string
{
if (!is_file(self::CPU_INFO_PATH)) {
return sprintf(
'The file "%s" could not be found.',
self::CPU_INFO_PATH
);
}
$cpuInfo = file_get_contents(self::CPU_INFO_PATH);
if (false === $cpuInfo) {
return sprintf(
'Could not get the content of the file "%s".',
self::CPU_INFO_PATH
);
}
return sprintf(
'Found the file "%s" with the content:%s%s',
self::CPU_INFO_PATH,
PHP_EOL,
$cpuInfo
);
}
/**
* @return positive-int|null
*/
public function find(): ?int
{
$cpuInfo = self::getCpuInfo();
return null === $cpuInfo ? null : self::countCpuCores($cpuInfo);
}
public function toString(): string
{
return 'CpuInfoFinder';
}
private static function getCpuInfo(): ?string
{
if (!@is_file(self::CPU_INFO_PATH)) {
return null;
}
$cpuInfo = @file_get_contents(self::CPU_INFO_PATH);
return false === $cpuInfo
? null
: $cpuInfo;
}
/**
* @internal
*
* @return positive-int|null
*/
public static function countCpuCores(string $cpuInfo): ?int
{
$processorCount = substr_count($cpuInfo, 'processor');
return $processorCount > 0 ? $processorCount : null;
}
}

View File

@@ -0,0 +1,57 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
use function sprintf;
/**
* This finder returns whatever value you gave to it. This is useful for testing
* or as a fallback to avoid to catch the NumberOfCpuCoreNotFound exception.
*/
final class DummyCpuCoreFinder implements CpuCoreFinder
{
/**
* @var positive-int
*/
private $count;
public function diagnose(): string
{
return sprintf(
'Will return "%d".',
$this->count
);
}
/**
* @param positive-int $count
*/
public function __construct(int $count)
{
$this->count = $count;
}
public function find(): ?int
{
return $this->count;
}
public function toString(): string
{
return sprintf(
'DummyCpuCoreFinder(value=%d)',
$this->count
);
}
}

View File

@@ -0,0 +1,77 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
final class FinderRegistry
{
/**
* @return list<CpuCoreFinder> List of all the known finders with all their variants.
*/
public static function getAllVariants(): array
{
return [
new CpuInfoFinder(),
new DummyCpuCoreFinder(1),
new HwLogicalFinder(),
new HwPhysicalFinder(),
new LscpuLogicalFinder(),
new LscpuPhysicalFinder(),
new _NProcessorFinder(),
new NProcessorFinder(),
new NProcFinder(true),
new NProcFinder(false),
new NullCpuCoreFinder(),
SkipOnOSFamilyFinder::forWindows(
new DummyCpuCoreFinder(1)
),
OnlyOnOSFamilyFinder::forWindows(
new DummyCpuCoreFinder(1)
),
new WmicPhysicalFinder(),
new WmicLogicalFinder(),
];
}
/**
* @return list<CpuCoreFinder>
*/
public static function getDefaultLogicalFinders(): array
{
return [
OnlyOnOSFamilyFinder::forWindows(new WmicLogicalFinder()),
new NProcFinder(),
new HwLogicalFinder(),
new _NProcessorFinder(),
new NProcessorFinder(),
new LscpuLogicalFinder(),
new CpuInfoFinder(),
];
}
/**
* @return list<CpuCoreFinder>
*/
public static function getDefaultPhysicalFinders(): array
{
return [
OnlyOnOSFamilyFinder::forWindows(new WmicPhysicalFinder()),
new HwPhysicalFinder(),
new LscpuPhysicalFinder(),
];
}
private function __construct()
{
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
/**
* Find the number of logical CPU cores for Linux, BSD and OSX.
*
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L903-L909
* @see https://opensource.apple.com/source/xnu/xnu-792.2.4/libkern/libkern/sysctl.h.auto.html
*/
final class HwLogicalFinder extends ProcOpenBasedFinder
{
protected function getCommand(): string
{
return 'sysctl -n hw.logicalcpu';
}
public function toString(): string
{
return 'HwLogicalFinder';
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
/**
* Find the number of physical CPU cores for Linux, BSD and OSX.
*
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L903-L909
* @see https://opensource.apple.com/source/xnu/xnu-792.2.4/libkern/libkern/sysctl.h.auto.html
*/
final class HwPhysicalFinder extends ProcOpenBasedFinder
{
protected function getCommand(): string
{
return 'sysctl -n hw.physicalcpu';
}
public function toString(): string
{
return 'HwPhysicalFinder';
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
use function count;
use function explode;
use function is_array;
use function preg_grep;
use const PHP_EOL;
/**
* The number of logical cores.
*
* @see https://stackoverflow.com/a/23378780/5846754
*/
final class LscpuLogicalFinder extends ProcOpenBasedFinder
{
public function getCommand(): string
{
return 'lscpu -p';
}
protected function countCpuCores(string $process): ?int
{
$lines = explode(PHP_EOL, $process);
$actualLines = preg_grep('/^\d+,/', $lines);
if (!is_array($actualLines)) {
return null;
}
$count = count($actualLines);
return 0 === $count ? null : $count;
}
public function toString(): string
{
return 'LscpuLogicalFinder';
}
}

View File

@@ -0,0 +1,66 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
use function count;
use function explode;
use function is_array;
use function preg_grep;
use function strtok;
use const PHP_EOL;
/**
* The number of physical processors.
*
* @see https://stackoverflow.com/a/23378780/5846754
*/
final class LscpuPhysicalFinder extends ProcOpenBasedFinder
{
public function toString(): string
{
return 'LscpuPhysicalFinder';
}
public function getCommand(): string
{
return 'lscpu -p';
}
protected function countCpuCores(string $process): ?int
{
$lines = explode(PHP_EOL, $process);
$actualLines = preg_grep('/^\d+/', $lines);
if (!is_array($actualLines)) {
return null;
}
$cores = [];
foreach ($actualLines as $line) {
strtok($line, ',');
$core = strtok(',');
if (false === $core) {
continue;
}
$cores[$core] = true;
}
unset($cores['-']);
$count = count($cores);
return 0 === $count ? null : $count;
}
}

View File

@@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
use function sprintf;
/**
* The number of (logical) cores.
*
* @see https://github.com/infection/infection/blob/fbd8c44/src/Resource/Processor/CpuCoresCountProvider.php#L69-L82
* @see https://unix.stackexchange.com/questions/146051/number-of-processors-in-proc-cpuinfo
*/
final class NProcFinder extends ProcOpenBasedFinder
{
/**
* @var bool
*/
private $all;
/**
* @param bool $all If disabled will give the number of cores available for the current process only.
*/
public function __construct(
bool $all = true,
?ProcessExecutor $executor = null
) {
parent::__construct($executor);
$this->all = $all;
}
public function toString(): string
{
return sprintf(
'NProcFinder(all=%s)',
$this->all ? 'true' : 'false'
);
}
protected function getCommand(): string
{
return 'nproc'.($this->all ? ' --all' : '');
}
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
/**
* Find the number of logical CPU cores for FreeSBD, Solaris and the likes.
*
* @see https://twitter.com/freebsdfrau/status/1052016199452700678?s=20&t=M2pHkRqmmna-UF68lfL2hw
*/
final class NProcessorFinder extends ProcOpenBasedFinder
{
protected function getCommand(): string
{
return 'getconf NPROCESSORS_ONLN';
}
public function toString(): string
{
return 'NProcessorFinder';
}
}

View File

@@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
/**
* This finder returns whatever value you gave to it. This is useful for testing.
*/
final class NullCpuCoreFinder implements CpuCoreFinder
{
public function diagnose(): string
{
return 'Will return "null".';
}
public function find(): ?int
{
return null;
}
public function toString(): string
{
return 'NullCpuCoreFinder';
}
}

View File

@@ -0,0 +1,113 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
use function implode;
use function sprintf;
use const PHP_OS_FAMILY;
final class OnlyOnOSFamilyFinder implements CpuCoreFinder
{
/**
* @var list<string>
*/
private $skippedOSFamilies;
/**
* @var CpuCoreFinder
*/
private $decoratedFinder;
/**
* @param string|list<string> $skippedOSFamilyOrFamilies
*/
public function __construct(
$skippedOSFamilyOrFamilies,
CpuCoreFinder $decoratedFinder
) {
$this->skippedOSFamilies = (array) $skippedOSFamilyOrFamilies;
$this->decoratedFinder = $decoratedFinder;
}
public static function forWindows(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Windows',
$decoratedFinder
);
}
public static function forBSD(CpuCoreFinder $decoratedFinder): self
{
return new self(
'BSD',
$decoratedFinder
);
}
public static function forDarwin(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Darwin',
$decoratedFinder
);
}
public static function forSolaris(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Solaris',
$decoratedFinder
);
}
public static function forLinux(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Linux',
$decoratedFinder
);
}
public function diagnose(): string
{
return $this->skip()
? sprintf(
'Skipped platform detected ("%s").',
PHP_OS_FAMILY
)
: $this->decoratedFinder->diagnose();
}
public function find(): ?int
{
return $this->skip()
? null
: $this->decoratedFinder->find();
}
public function toString(): string
{
return sprintf(
'OnlyOnOSFamilyFinder(only=(%s),%s)',
implode(',', $this->skippedOSFamilies),
$this->decoratedFinder->toString()
);
}
private function skip(): bool
{
return !in_array(PHP_OS_FAMILY, $this->skippedOSFamilies, true);
}
}

View File

@@ -0,0 +1,104 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
use Fidry\CpuCoreCounter\Executor\ProcOpenExecutor;
use function filter_var;
use function function_exists;
use function is_int;
use function sprintf;
use function trim;
use const FILTER_VALIDATE_INT;
use const PHP_EOL;
abstract class ProcOpenBasedFinder implements CpuCoreFinder
{
/**
* @var ProcessExecutor
*/
private $executor;
public function __construct(?ProcessExecutor $executor = null)
{
$this->executor = $executor ?? new ProcOpenExecutor();
}
public function diagnose(): string
{
if (!function_exists('proc_open')) {
return 'The function "proc_open" is not available.';
}
$command = $this->getCommand();
$output = $this->executor->execute($command);
if (null === $output) {
return sprintf(
'Failed to execute the command "%s".',
$command
);
}
[$stdout, $stderr] = $output;
$failed = '' !== trim($stderr);
return $failed
? sprintf(
'Executed the command "%s" which wrote the following output to the STDERR:%s%s',
$command,
PHP_EOL,
$stderr
)
: sprintf(
'Executed the command "%s" and got the following (STDOUT) output:%s%s',
$command,
PHP_EOL,
$stdout
);
}
/**
* @return positive-int|null
*/
public function find(): ?int
{
$output = $this->executor->execute($this->getCommand());
if (null === $output) {
return null;
}
[$stdout, $stderr] = $output;
$failed = '' !== trim($stderr);
return $failed
? null
: $this->countCpuCores($stdout);
}
/**
* @internal
*
* @return positive-int|null
*/
protected function countCpuCores(string $process): ?int
{
$cpuCount = filter_var($process, FILTER_VALIDATE_INT);
return is_int($cpuCount) && $cpuCount > 0 ? $cpuCount : null;
}
abstract protected function getCommand(): string;
}

View File

@@ -0,0 +1,113 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
use function implode;
use function in_array;
use function sprintf;
final class SkipOnOSFamilyFinder implements CpuCoreFinder
{
/**
* @var list<string>
*/
private $skippedOSFamilies;
/**
* @var CpuCoreFinder
*/
private $decoratedFinder;
/**
* @param string|list<string> $skippedOSFamilyOrFamilies
*/
public function __construct(
$skippedOSFamilyOrFamilies,
CpuCoreFinder $decoratedFinder
) {
$this->skippedOSFamilies = (array) $skippedOSFamilyOrFamilies;
$this->decoratedFinder = $decoratedFinder;
}
public static function forWindows(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Windows',
$decoratedFinder
);
}
public static function forBSD(CpuCoreFinder $decoratedFinder): self
{
return new self(
'BSD',
$decoratedFinder
);
}
public static function forDarwin(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Darwin',
$decoratedFinder
);
}
public static function forSolaris(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Solaris',
$decoratedFinder
);
}
public static function forLinux(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Linux',
$decoratedFinder
);
}
public function diagnose(): string
{
return $this->skip()
? sprintf(
'Skipped platform detected ("%s").',
PHP_OS_FAMILY
)
: $this->decoratedFinder->diagnose();
}
public function find(): ?int
{
return $this->skip()
? null
: $this->decoratedFinder->find();
}
public function toString(): string
{
return sprintf(
'SkipOnOSFamilyFinder(skip=(%s),%s)',
implode(',', $this->skippedOSFamilies),
$this->decoratedFinder->toString()
);
}
private function skip(): bool
{
return in_array(PHP_OS_FAMILY, $this->skippedOSFamilies, true);
}
}

View File

@@ -0,0 +1,47 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
use function preg_match;
/**
* Find the number of logical CPU cores for Windows.
*
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L912-L916
*/
final class WmicLogicalFinder extends ProcOpenBasedFinder
{
private const CPU_CORE_COUNT_REGEX = '/NumberOfLogicalProcessors[\s\n]+(?<count>\d+)/';
protected function getCommand(): string
{
return 'wmic cpu get NumberOfLogicalProcessors';
}
public function toString(): string
{
return 'WmicLogicalFinder';
}
protected function countCpuCores(string $process): ?int
{
if (0 === preg_match(self::CPU_CORE_COUNT_REGEX, $process, $matches)) {
return parent::countCpuCores($process);
}
$count = $matches['count'];
return parent::countCpuCores($count);
}
}

View File

@@ -0,0 +1,47 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
use function preg_match;
/**
* Find the number of physical CPU cores for Windows.
*
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L912-L916
*/
final class WmicPhysicalFinder extends ProcOpenBasedFinder
{
private const CPU_CORE_COUNT_REGEX = '/NumberOfCores[\s\n]+(?<count>\d+)/';
protected function getCommand(): string
{
return 'wmic cpu get NumberOfCores';
}
public function toString(): string
{
return 'WmicPhysicalFinder';
}
protected function countCpuCores(string $process): ?int
{
if (0 === preg_match(self::CPU_CORE_COUNT_REGEX, $process, $matches)) {
return parent::countCpuCores($process);
}
$count = $matches['count'];
return parent::countCpuCores($count);
}
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter\Finder;
/**
* Find the number of logical CPU cores for Linux and the likes.
*
* @see https://twitter.com/freebsdfrau/status/1052016199452700678?s=20&t=M2pHkRqmmna-UF68lfL2hw
*/
final class _NProcessorFinder extends ProcOpenBasedFinder
{
protected function getCommand(): string
{
return 'getconf _NPROCESSORS_ONLN';
}
public function toString(): string
{
return '_NProcessorFinder';
}
}

View File

@@ -0,0 +1,26 @@
<?php
/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <theo.fidry@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fidry\CpuCoreCounter;
use RuntimeException;
final class NumberOfCpuCoreNotFound extends RuntimeException
{
public static function create(): self
{
return new self(
'Could not find the number of CPU cores available.'
);
}
}