Files
pest/src/Plugins/Help.php
2022-09-18 11:10:18 +01:00

118 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Support\View;
use function Pest\version;
use PHPUnit\TextUI\Help as PHPUnitHelp;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
final class Help implements HandlesArguments
{
use Concerns\HandleArguments;
/**
* Creates a new Plugin instance.
*/
public function __construct(
private readonly OutputInterface $output
) {
// ..
}
/**
* {@inheritDoc}
*/
public function handleArguments(array $arguments): array
{
if ($this->hasArgument('--help', $arguments)) {
View::render('version', [
'version' => version(),
]);
View::render('usage');
foreach ($this->getContent() as $title => $options) {
if ($title === 'Usage') {
continue;
}
$this->output->writeln([
'',
sprintf(' <fg=yellow;options=bold>%s OPTIONS:</>', mb_strtoupper($title)),
]);
foreach ($options as $option) {
if (! array_key_exists('arg', $option)) {
continue;
}
[
'arg' => $argument,
'desc' => $description,
] = $option;
$argument = $this->colorizeOptions($argument);
View::render('components.two-column-detail', [
'left' => $argument,
'right' => $description,
]);
}
}
$this->output->write('', true);
exit(0);
}
return $arguments;
}
/**
* Colorizes the given string options.
*/
private function colorizeOptions(string $argument): string
{
$argument = (string) preg_replace('/(--\w+)/', '<fg=blue;options=bold>$1</>', $argument);
return (string) preg_replace('/(-\w+)/', '<fg=blue;options=bold>$1</>', $argument);
}
/**
* @return array<string, array<int, array{arg?: string, desc: string}>>
*/
private function getContent(): array
{
// Access the PHPUnit help class's private const HELP
$helpReflection = new \ReflectionClass(PHPUnitHelp::class);
/** @var array<string, array<int, array{arg: string, desc: string}>> $content */
$content = $helpReflection->getConstant('HELP_TEXT');
$content['Configuration'] = [[
'arg' => '--init',
'desc' => 'Initialise a standard Pest configuration',
]] + $content['Configuration'];
$content['Code Coverage'] = [
[
'arg' => '--coverage ',
'desc' => 'Generate code coverage report and output to standard output',
],
[
'arg' => '--coverage --min',
'desc' => 'Set the minimum required coverage percentage, and fail if not met',
],
] + $content['Code Coverage'];
return $content;
}
}