Skip to content

Commit 3f6be59

Browse files
authored
Refs #519 - handle summary option in JsonFormatter (#541)
Adding an optional summary option in the JSON Formatter.
1 parent 44f05ab commit 3f6be59

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/Application/Console/Formatters/Json.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use NunoMaduro\PhpInsights\Domain\DetailsComparator;
1212
use NunoMaduro\PhpInsights\Domain\Insights\Insight;
1313
use NunoMaduro\PhpInsights\Domain\Insights\InsightCollection;
14+
use Symfony\Component\Console\Input\InputInterface;
1415
use Symfony\Component\Console\Output\OutputInterface;
1516

1617
/**
@@ -19,10 +20,14 @@
1920
final class Json implements Formatter
2021
{
2122
private OutputInterface $output;
23+
private bool $summaryOnly = false;
2224

23-
public function __construct(OutputInterface $output)
25+
public function __construct(InputInterface $input, OutputInterface $output)
2426
{
2527
$this->output = $output;
28+
if ($input->hasOption('summary')) {
29+
$this->summaryOnly = (bool) $input->getOption('summary');
30+
}
2631
}
2732

2833
/**
@@ -47,7 +52,9 @@ public function format(InsightCollection $insightCollection, array $metrics): vo
4752
],
4853
];
4954

50-
$data += $this->issues($insightCollection, $metrics);
55+
if (! $this->summaryOnly) {
56+
$data += $this->issues($insightCollection, $metrics);
57+
}
5158

5259
$json = json_encode($data, JSON_THROW_ON_ERROR);
5360

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Application\Console\Formatters;
6+
7+
use NunoMaduro\PhpInsights\Application\Console\Formatters\Json;
8+
use NunoMaduro\PhpInsights\Domain\Metrics\Style\Style;
9+
use Symfony\Component\Console\Input\ArrayInput;
10+
use Symfony\Component\Console\Input\InputDefinition;
11+
use Symfony\Component\Console\Input\InputOption;
12+
use Symfony\Component\Console\Output\BufferedOutput;
13+
use Tests\TestCase;
14+
15+
final class JsonTest extends TestCase
16+
{
17+
public function testItHasSectionForMetricsGiven(): void
18+
{
19+
$collection = $this->runAnalyserOnPreset(
20+
'default',
21+
[__DIR__ . '/JsonTest.php']
22+
);
23+
$output = new BufferedOutput();
24+
$input = new ArrayInput(['--summary' => false], new InputDefinition([new InputOption('summary')]));
25+
26+
$console = new Json($input, $output);
27+
28+
$console->format($collection, [Style::class]);
29+
30+
$result = json_decode($output->fetch(), true);
31+
self::assertArrayHasKey('summary', $result);
32+
self::assertArrayHasKey('Style', $result);
33+
}
34+
35+
public function testItHasOnlySummary(): void
36+
{
37+
$collection = $this->runAnalyserOnPreset(
38+
'default',
39+
[__DIR__ . '/JsonTest.php']
40+
);
41+
$output = new BufferedOutput();
42+
$input = new ArrayInput(['--summary' => true], new InputDefinition([new InputOption('summary')]));
43+
44+
$console = new Json($input, $output);
45+
46+
$console->format($collection, [Style::class]);
47+
48+
$result = json_decode($output->fetch(), true);
49+
self::assertArrayHasKey('summary', $result);
50+
self::assertArrayNotHasKey('Style', $result);
51+
}
52+
}

0 commit comments

Comments
 (0)