Skip to content

Commit 00a9158

Browse files
committed
Add option --config-file
1 parent 6bc4556 commit 00a9158

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

src/PHPUnit/Cobertura/Formatter/Application.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ private function parseAndRender(): void
8686
(new Renderer(
8787
$this->consoleOutput,
8888
new Colorizer(
89-
new ConfigFile()
89+
new ConfigFile(
90+
$this->commandLine->optionConfigFile()
91+
)
9092
)
9193
))->render(
9294
(new ClassNameFilter(

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,16 @@ public function buildInputArgs(): void
3030
{
3131
$definition = new InputDefinition();
3232

33-
$argument = new InputArgument('cobertura-file', InputArgument::OPTIONAL);
34-
$definition->addArgument($argument);
33+
$definition->addArguments([
34+
new InputArgument('cobertura-file', InputArgument::OPTIONAL)
35+
]);
3536

36-
$option = new InputOption('init', null, InputOption::VALUE_NONE);
37-
$definition->addOption($option);
38-
39-
$option = new InputOption('no-color', null, InputOption::VALUE_NONE);
40-
$definition->addOption($option);
41-
42-
$option = new InputOption('filter-class-name', null, InputOption::VALUE_REQUIRED);
43-
$definition->addOption($option);
37+
$definition->addOptions([
38+
new InputOption('init', null, InputOption::VALUE_NONE),
39+
new InputOption('no-color', null, InputOption::VALUE_NONE),
40+
new InputOption('filter-class-name', null, InputOption::VALUE_REQUIRED),
41+
new InputOption('config-file', null, InputOption::VALUE_REQUIRED),
42+
]);
4443

4544
$this->input = new ArgvInput(null, $definition);
4645
}
@@ -50,7 +49,7 @@ public function coberturaFile(): string
5049
$coberturaFile = (string) $this->input->getArgument('cobertura-file');
5150

5251
if ('' === $coberturaFile) {
53-
throw new RuntimeException('Missing required argument \'path to cobertura XML file\'.');
52+
throw new RuntimeException('Missing required argument: path to cobertura XML file.');
5453
}
5554

5655
return $coberturaFile;
@@ -81,4 +80,20 @@ public function optionFilterClassName(): ?string
8180

8281
return $filterClassName;
8382
}
83+
84+
public function optionConfigFile(): ?string
85+
{
86+
/** @var string|null $configFile */
87+
$configFile = $this->input->getOption('config-file');
88+
89+
if (null === $configFile) {
90+
return null;
91+
}
92+
93+
if ('' === $configFile) {
94+
throw new RuntimeException('The "--config-file" option requires a value.');
95+
}
96+
97+
return $configFile;
98+
}
8499
}

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use function array_shift;
2121
use function count;
2222
use function explode;
23-
use function getcwd;
2423
use function implode;
2524
use function is_array;
2625
use function is_file;
@@ -39,12 +38,12 @@ final class File
3938
'phpunit-cobertura-formatter.yml.dist',
4039
];
4140

42-
private Validator $validator;
43-
4441
/**
45-
* @var string[]
42+
* @var list<string>
4643
*/
47-
private array $files;
44+
private array $files = self::DEFAULT_FILES;
45+
46+
private Validator $validator;
4847

4948
/**
5049
* @var array<mixed>|null
@@ -56,13 +55,13 @@ final class File
5655
*/
5756
private array $cache = [];
5857

59-
/**
60-
* @param string[]|null $files
61-
*/
62-
public function __construct(?array $files = null)
58+
public function __construct(?string $file)
6359
{
6460
$this->validator = new Validator();
65-
$this->files = $files ?? self::DEFAULT_FILES;
61+
62+
if (null !== $file) {
63+
$this->files = [$file];
64+
}
6665
}
6766

6867
/**
@@ -100,9 +99,7 @@ private function all(): array
10099

101100
private function findFile(): string
102101
{
103-
foreach ($this->files as $fileName) {
104-
$file = sprintf('%s/%s', (string) getcwd(), $fileName);
105-
102+
foreach ($this->files as $file) {
106103
if (!is_file($file)) {
107104
continue;
108105
}

0 commit comments

Comments
 (0)