Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds 'parallel' option support to phpcs task. #1155

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions doc/tasks/phpcs.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ grumphp:
triggered_by: [php]
exclude: []
show_sniffs_error_path: true
parallel: null
```

**standard**
Expand Down Expand Up @@ -136,6 +137,13 @@ A list of rules that should not be checked. Leave this option blank to run all c

Displays the sniff that triggered the error, allowing you to more easily find the specific rules with their namespaces.

**parallel**

*Default: null*

Determines the number of processes that phpcs / phpcbf will use when running. Pass a positive integer for multiple
processes. You can also pass the string 'auto' and it will use the number of cores / vCPUs your machine supports.

## Framework presets

### Symfony 2
Expand Down
74 changes: 73 additions & 1 deletion src/Task/Phpcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

/**
Expand Down Expand Up @@ -47,7 +48,8 @@ public static function getConfigurableOptions(): ConfigOptionsResolver
'report' => 'full',
'report_width' => null,
'exclude' => [],
'show_sniffs_error_path' => true
'show_sniffs_error_path' => true,
'parallel' => null,
]);

$resolver->addAllowedTypes('standard', ['array', 'null', 'string']);
Expand All @@ -64,6 +66,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver
$resolver->addAllowedTypes('report_width', ['null', 'int']);
$resolver->addAllowedTypes('exclude', ['array']);
$resolver->addAllowedTypes('show_sniffs_error_path', ['bool']);
$resolver->addAllowedTypes('parallel', ['null', 'int', 'string']);

return ConfigOptionsResolver::fromOptionsResolver($resolver);
}
Expand Down Expand Up @@ -161,7 +164,76 @@ private function addArgumentsFromConfig(
$arguments->addOptionalCommaSeparatedArgument('--ignore=%s', $config['ignore_patterns']);
$arguments->addOptionalCommaSeparatedArgument('--exclude=%s', $config['exclude']);
$arguments->addOptionalArgument('-s', $config['show_sniffs_error_path']);
$arguments->addOptionalArgument('--parallel=%s', $this->getParallelValue($config['parallel']));

return $arguments;
}

protected function getParallelValue(string|int|null $option): ?int
{
if ($option === null) {
return null;
}

if (is_string($option)) {
if ($option === 'auto') {
return $this->getNumberOfCpuCores() ?? 1;
}

throw new \InvalidArgumentException(
sprintf("When option 'parallel' is a string it can only be 'auto', got '%s'", $option)
);
}

if ($option < 1) {
throw new \InvalidArgumentException(
sprintf("When option 'parallel' is an integer it must be greater than zero, got %d", $option)
);
}

return $option;
}

/**
* @return int|null
*/
protected function getNumberOfCpuCores(): ?int
{
try {
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
$process = new Process(['wmic', 'cpu', 'get', 'NumberOfCores']);
}

if (strncasecmp(PHP_OS, 'Linux', 5) === 0 || strncasecmp(PHP_OS, 'Darwin', 6) === 0) {
$process = new Process(['nproc']);
}

if (!isset($process)) {
return null;
}

$process->run();

if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}

$output = $process->getOutput();

if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
$lines = explode("\n", trim($output));
foreach ($lines as $line) {
$cores = (int) trim($line);
if ($cores > 0) {
return $cores;
}
}
return null;
}

return (int) trim($output);
} catch (\Exception) {
return null;
}
}
}
18 changes: 17 additions & 1 deletion test/Unit/Task/PhpcsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function provideConfigurableOptions(): iterable
'report' => 'full',
'report_width' => null,
'exclude' => [],
'show_sniffs_error_path' => true
'show_sniffs_error_path' => true,
'parallel' => null,
]
];
}
Expand Down Expand Up @@ -357,6 +358,21 @@ public function provideExternalTaskRuns(): iterable
$this->expectFileList('hello.php'.PHP_EOL.'hello2.php'),
]
];
yield 'parallel' => [
[
'parallel' => 4,
],
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'phpcs',
[
'--extensions=php',
'--report=full',
'-s',
'--parallel=4',
'--report-json',
$this->expectFileList('hello.php'.PHP_EOL.'hello2.php'),
]
];
}

private function expectFileList(string $expectedContents): callable
Expand Down