Skip to content

Commit 6bc4556

Browse files
committed
Add option --filter-class-name
1 parent fd566b6 commit 6bc4556

File tree

4 files changed

+98
-10
lines changed

4 files changed

+98
-10
lines changed

src/PHPUnit/Cobertura/Formatter/Application.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Exception;
2020
use Symfony\Component\Console\Output\ConsoleOutput;
2121
use Throwable;
22+
use AndreyTech\PHPUnit\Cobertura\Formatter\Parser\Filter\ClassName as ClassNameFilter;
2223

2324
use function sprintf;
2425

@@ -62,6 +63,8 @@ public function run(): int
6263
*/
6364
private function doRun(): int
6465
{
66+
$this->commandLine->buildInputArgs();
67+
6568
$this->consoleOutput->getFormatter()->setDecorated(!$this->commandLine->optionNoColor());
6669

6770
if ($this->commandLine->optionInit()) {
@@ -86,9 +89,13 @@ private function parseAndRender(): void
8689
new ConfigFile()
8790
)
8891
))->render(
89-
(new Parser())->parse(
90-
new CoberturaFile(
91-
$this->commandLine->coberturaFile()
92+
(new ClassNameFilter(
93+
$this->commandLine->optionFilterClassName()
94+
))->filter(
95+
(new Parser())->parse(
96+
new CoberturaFile(
97+
$this->commandLine->coberturaFile()
98+
)
9299
)
93100
)
94101
);

src/PHPUnit/Cobertura/Formatter/Config/CommandLine.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
use Symfony\Component\Console\Input\InputDefinition;
1818
use Symfony\Component\Console\Input\InputOption;
1919

20-
final readonly class CommandLine
20+
final class CommandLine
2121
{
2222
private ArgvInput $input;
2323

2424
public function __construct()
2525
{
26-
$this->input = $this->buildArgvInput();
26+
$this->input = new ArgvInput();
2727
}
2828

29-
public function buildArgvInput(): ArgvInput
29+
public function buildInputArgs(): void
3030
{
3131
$definition = new InputDefinition();
3232

@@ -39,15 +39,18 @@ public function buildArgvInput(): ArgvInput
3939
$option = new InputOption('no-color', null, InputOption::VALUE_NONE);
4040
$definition->addOption($option);
4141

42-
return new ArgvInput(null, $definition);
42+
$option = new InputOption('filter-class-name', null, InputOption::VALUE_REQUIRED);
43+
$definition->addOption($option);
44+
45+
$this->input = new ArgvInput(null, $definition);
4346
}
4447

4548
public function coberturaFile(): string
4649
{
4750
$coberturaFile = (string) $this->input->getArgument('cobertura-file');
4851

4952
if ('' === $coberturaFile) {
50-
throw new RuntimeException('Missing required argument <path to cobertura XML file>.');
53+
throw new RuntimeException('Missing required argument \'path to cobertura XML file\'.');
5154
}
5255

5356
return $coberturaFile;
@@ -62,4 +65,20 @@ public function optionNoColor(): bool
6265
{
6366
return (bool) $this->input->getOption('no-color');
6467
}
68+
69+
public function optionFilterClassName(): ?string
70+
{
71+
/** @var string|null $filterClassName */
72+
$filterClassName = $this->input->getOption('filter-class-name');
73+
74+
if (null === $filterClassName) {
75+
return null;
76+
}
77+
78+
if ('' === $filterClassName) {
79+
throw new RuntimeException('The "--filter-class-name" option requires a value.');
80+
}
81+
82+
return $filterClassName;
83+
}
6584
}

src/PHPUnit/Cobertura/Formatter/Parser/ClassMetricsCollection.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@
1111

1212
namespace AndreyTech\PHPUnit\Cobertura\Formatter\Parser;
1313

14+
use Closure;
15+
16+
use function array_filter;
17+
use function array_values;
18+
1419
final class ClassMetricsCollection
1520
{
1621
/**
17-
* @var list<ClassMetrics>
22+
* @param list<ClassMetrics> $items
1823
*/
19-
private array $items = [];
24+
public function __construct(
25+
private array $items = []
26+
) {
27+
}
2028

2129
public function add(ClassMetrics $classMetrics): void
2230
{
@@ -30,4 +38,16 @@ public function all(): array
3038
{
3139
return $this->items;
3240
}
41+
42+
/**
43+
* @param Closure(ClassMetrics):bool $closure
44+
*/
45+
public function filter(Closure $closure): self
46+
{
47+
return new self(
48+
array_values(
49+
array_filter($this->items, $closure)
50+
)
51+
);
52+
}
3353
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* @author andrey-tech
5+
* @copyright 2025 andrey-tech
6+
* @link https://github.com/andrey-tech/
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace AndreyTech\PHPUnit\Cobertura\Formatter\Parser\Filter;
13+
14+
use AndreyTech\PHPUnit\Cobertura\Formatter\Parser\ClassMetrics;
15+
use AndreyTech\PHPUnit\Cobertura\Formatter\Parser\ClassMetricsCollection;
16+
17+
use function preg_match;
18+
use function sprintf;
19+
20+
final readonly class ClassName
21+
{
22+
public function __construct(
23+
private ?string $classNameRegex
24+
) {
25+
}
26+
27+
public function filter(ClassMetricsCollection $classMetricsCollection): ClassMetricsCollection
28+
{
29+
$regex = $this->classNameRegex;
30+
31+
if (null === $regex || '' === $regex) {
32+
return $classMetricsCollection;
33+
}
34+
35+
return $classMetricsCollection->filter(
36+
static fn (ClassMetrics $classMetrics): bool => (bool) preg_match(
37+
sprintf('|%s|', $regex),
38+
$classMetrics->name
39+
)
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)