Skip to content

Commit 22d94c3

Browse files
reinfijanedbal
andauthored
add Junit formatter to use in ci/cd environments (#110)
Co-authored-by: Jan Nedbal <janedbal@gmail.com>
1 parent dde6128 commit 22d94c3

14 files changed

+923
-413
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ This tool reads your `composer.json` and scans all paths listed in `autoload` &
8080
- `--help` display usage & cli options
8181
- `--verbose` to see more example classes & usages
8282
- `--show-all-usages` to see all usages
83+
- `--format` to use different output format, available are: console (default), junit
8384
- `--ignore-unknown-classes` to globally ignore unknown classes
8485
- `--ignore-shadow-deps` to globally ignore shadow dependencies
8586
- `--ignore-unused-deps` to globally ignore unused dependencies

bin/composer-dependency-analyser

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidConfigException;
77
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidPathException;
88
use ShipMonk\ComposerDependencyAnalyser\Initializer;
99
use ShipMonk\ComposerDependencyAnalyser\Printer;
10-
use ShipMonk\ComposerDependencyAnalyser\Result\ResultFormatter;
1110
use ShipMonk\ComposerDependencyAnalyser\Stopwatch;
1211

1312
error_reporting(E_ALL);
@@ -42,7 +41,7 @@ try {
4241
$analyser = new Analyser($stopwatch, $classLoaders, $configuration, $composerJson->dependencies);
4342
$result = $analyser->run();
4443

45-
$formatter = new ResultFormatter($cwd, $printer);
44+
$formatter = $initializer->initFormatter($options);
4645
$exitCode = $formatter->format($result, $options, $configuration);
4746

4847
} catch (

src/Cli.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Cli
2424
'config' => true,
2525
'dump-usages' => true,
2626
'show-all-usages' => false,
27+
'format' => true,
2728
];
2829

2930
/**
@@ -167,6 +168,10 @@ public function getProvidedOptions(): CliOptions
167168
$options->showAllUsages = true;
168169
}
169170

171+
if (isset($this->providedOptions['format'])) {
172+
$options->format = $this->providedOptions['format']; // @phpstan-ignore-line type is ensured
173+
}
174+
170175
return $options;
171176
}
172177

src/CliOptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,9 @@ class CliOptions
6060
*/
6161
public $showAllUsages = null;
6262

63+
/**
64+
* @var string|null
65+
*/
66+
public $format = null;
67+
6368
}

src/Initializer.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidCliException;
99
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidConfigException;
1010
use ShipMonk\ComposerDependencyAnalyser\Exception\InvalidPathException;
11+
use ShipMonk\ComposerDependencyAnalyser\Result\ConsoleFormatter;
12+
use ShipMonk\ComposerDependencyAnalyser\Result\JunitFormatter;
13+
use ShipMonk\ComposerDependencyAnalyser\Result\ResultFormatter;
1114
use Throwable;
1215
use function count;
1316
use function get_class;
@@ -32,6 +35,7 @@ class Initializer
3235
--composer-json <path> Provide custom path to composer.json
3336
--config <path> Provide path to php configuration file
3437
(must return \ShipMonk\ComposerDependencyAnalyser\Config\Configuration instance)
38+
--format <format> Change output format. Available values: console (default), junit
3539
3640
Ignore options:
3741
(or use --config for better granularity)
@@ -210,4 +214,22 @@ public function initCliOptions(string $cwd, array $argv): CliOptions
210214
return $cliOptions;
211215
}
212216

217+
/**
218+
* @throws InvalidConfigException
219+
*/
220+
public function initFormatter(CliOptions $options): ResultFormatter
221+
{
222+
switch ($options->format) {
223+
case 'junit':
224+
return new JunitFormatter($this->cwd, $this->printer);
225+
226+
case 'console':
227+
case null:
228+
return new ConsoleFormatter($this->cwd, $this->printer);
229+
230+
default:
231+
throw new InvalidConfigException("Invalid format option provided, allowed are 'console' or 'junit'.");
232+
}
233+
}
234+
213235
}

src/Printer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public function printLine(string $string): void
2626
echo $this->colorize($string) . PHP_EOL;
2727
}
2828

29+
public function print(string $string): void
30+
{
31+
echo $this->colorize($string);
32+
}
33+
2934
private function colorize(string $string): string
3035
{
3136
return str_replace(array_keys(self::COLORS), array_values(self::COLORS), $string);

0 commit comments

Comments
 (0)