Skip to content

Commit 1890a0d

Browse files
[metrics] Unify outputs under target (#99) (#100)
* [metrics] Unify outputs under target (#99) * Update wiki submodule pointer for PR #100 --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 14b3d12 commit 1890a0d

9 files changed

Lines changed: 61 additions & 75 deletions

File tree

.github/wiki

Submodule wiki updated from 3ca041c to 5e683c7

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ vendor/bin/dev-tools dependencies
5656

5757
# Analyze code metrics with PhpMetrics
5858
composer metrics
59-
composer dev-tools metrics -- --report-html=build/metrics
59+
composer dev-tools metrics -- --target=build/metrics
6060
composer dev-tools metrics -- --working-dir=packages/example
6161

6262
# Check and fix code style using ECS and Composer Normalize

docs/commands/metrics.rst

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ Options
3535
Default:
3636
``vendor,test,tests,tmp,cache,spec,build,backup,resources``.
3737

38-
``--report-html=<directory>``
39-
Optional output directory for the generated HTML report.
38+
``--target=<directory>``
39+
Output directory for the generated metrics reports.
4040

41-
``--report-json=<file>``
42-
Optional output file for the generated JSON report.
41+
Default: ``public/metrics``.
4342

44-
``--report-summary-json=<file>``
45-
Optional output file for the generated summary JSON report.
43+
The command writes:
44+
45+
- the HTML report to the target directory itself;
46+
- ``report.json`` inside the target directory;
47+
- ``report-summary.json`` inside the target directory.
4648

4749
Examples
4850
--------
@@ -57,17 +59,18 @@ Generate an HTML report for manual inspection:
5759

5860
.. code-block:: bash
5961
60-
composer dev-tools metrics -- --report-html=build/metrics
62+
composer dev-tools metrics -- --target=build/metrics
6163
62-
Generate JSON and HTML reports for CI artifacts:
64+
Generate the full metrics artifact set for CI previews:
6365

6466
.. code-block:: bash
6567
66-
vendor/bin/dev-tools metrics --report-json=build/metrics.json --report-html=build/metrics
68+
vendor/bin/dev-tools metrics --target=build/metrics
6769
6870
Behavior
6971
--------
7072

71-
- the command forwards report options directly to PhpMetrics;
73+
- the command derives ``report.json`` and ``report-summary.json`` from the
74+
selected ``--target`` directory;
7275
- it runs PhpMetrics through the active PHP binary and suppresses PhpMetrics
7376
deprecation notices emitted by the dependency itself.

docs/commands/reports.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ coverage. It combines:
1111

1212
- ``docs --target`` - generates API documentation
1313
- ``tests --coverage`` - generates test coverage reports
14-
- optionally ``metrics --report-html`` - generates PhpMetrics HTML reports
14+
- optionally ``metrics --target`` - generates PhpMetrics HTML and JSON reports
1515

1616
These are run in parallel for efficiency.
1717

@@ -78,7 +78,7 @@ Behavior
7878
---------
7979

8080
- Runs ``docs`` and ``tests --coverage`` in parallel.
81-
- Runs ``metrics --report-html`` in parallel when ``--metrics`` is enabled.
81+
- Runs ``metrics --target`` after tests when ``--metrics`` is enabled.
8282
- Runs tests with ``--no-progress`` and ``--coverage-summary`` so report builds
8383
keep PHPUnit output concise.
8484
- Used by the ``standards`` command as the final phase.

docs/running/specialized-commands.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Analyzes code metrics with PhpMetrics.
5050
.. code-block:: bash
5151
5252
composer metrics
53-
composer dev-tools metrics -- --report-html=build/metrics
53+
composer dev-tools metrics -- --target=build/metrics
5454
composer dev-tools metrics -- --working-dir=packages/example
5555
5656
Important details:
@@ -59,8 +59,9 @@ Important details:
5959
``fast-forward/dev-tools``;
6060
- it analyzes the selected ``--working-dir`` and forwards the requested
6161
report options directly to PhpMetrics;
62-
- ``--report-html``, ``--report-json``, and ``--report-summary-json`` allow
63-
persisting the native PhpMetrics reports for CI artifacts or manual review;
62+
- ``--target`` stores the HTML report plus ``report.json`` and
63+
``report-summary.json`` in the same directory for CI artifacts or manual
64+
review;
6465
- it suppresses deprecation notices emitted by the PhpMetrics dependency
6566
itself so the command output stays readable.
6667

@@ -161,7 +162,7 @@ Important details:
161162

162163
- it calls ``docs --target public``;
163164
- it calls ``tests --coverage public/coverage --no-progress --coverage-summary``;
164-
- ``--metrics`` adds ``metrics --report-html public/metrics``;
165+
- ``--metrics`` adds ``metrics --target public/metrics``;
165166
- it is the reporting stage used by ``standards``.
166167

167168
``skills``

src/Console/Command/MetricsCommand.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Symfony\Component\Console\Input\InputOption;
2828
use Symfony\Component\Console\Output\OutputInterface;
2929

30+
use function rtrim;
31+
3032
#[AsCommand(
3133
name: 'metrics',
3234
description: 'Analyzes code metrics with PhpMetrics.',
@@ -68,23 +70,11 @@ protected function configure(): void
6870
default: 'vendor,test,tests,tmp,cache,spec,build,backup,resources',
6971
)
7072
->addOption(
71-
name: 'report-html',
73+
name: 'target',
7274
mode: InputOption::VALUE_OPTIONAL,
73-
description: 'Optional target directory for the generated HTML report.',
75+
description: 'Target directory for the generated metrics reports.',
7476
default: 'public/metrics',
7577
)
76-
->addOption(
77-
name: 'report-json',
78-
mode: InputOption::VALUE_OPTIONAL,
79-
description: 'Optional target file for the generated JSON report.',
80-
default: 'public/metrics/report.json',
81-
)
82-
->addOption(
83-
name: 'report-summary-json',
84-
mode: InputOption::VALUE_OPTIONAL,
85-
description: 'Optional target file for the generated summary JSON report.',
86-
default: 'public/metrics/report-summary.json',
87-
)
8878
->addOption(
8979
name: 'junit',
9080
mode: InputOption::VALUE_OPTIONAL,
@@ -102,13 +92,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
10292
{
10393
$output->writeln('<info>Running code metrics analysis...</info>');
10494

95+
$target = rtrim((string) $input->getOption('target'), '/');
96+
10597
$processBuilder = $this->processBuilder
10698
->withArgument('--ansi')
10799
->withArgument('--git', 'git')
108100
->withArgument('--exclude', (string) $input->getOption('exclude'))
109-
->withArgument('--report-html', $input->getOption('report-html'))
110-
->withArgument('--report-json', $input->getOption('report-json'))
111-
->withArgument('--report-summary-json', $input->getOption('report-summary-json'));
101+
->withArgument('--report-html', $target)
102+
->withArgument('--report-json', $target . '/report.json')
103+
->withArgument('--report-summary-json', $target . '/report-summary.json');
112104

113105
if (null !== $input->getOption('junit')) {
114106
$processBuilder = $processBuilder->withArgument('--junit', $input->getOption('junit'));

src/Console/Command/ReportsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
108108
$metrics = $this->processBuilder
109109
->withArgument('--ansi')
110110
->withArgument('--junit', $input->getOption('coverage') . '/junit.xml')
111-
->withArgument('--report-html', $input->getOption('metrics'))
111+
->withArgument('--target', $input->getOption('metrics'))
112112
->build('composer dev-tools metrics --');
113113

114114
$this->processQueue->add(process: $docs, detached: true);

tests/Console/Command/MetricsCommandTest.php

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ final class MetricsCommandTest extends TestCase
6666
*/
6767
private ObjectProphecy $process;
6868

69-
private string $jsonReport;
70-
71-
private string $summaryReport;
69+
private string $target;
7270

7371
private MetricsCommand $command;
7472

@@ -82,12 +80,9 @@ protected function setUp(): void
8280
$this->input = $this->prophesize(InputInterface::class);
8381
$this->output = $this->prophesize(OutputInterface::class);
8482
$this->process = $this->prophesize(Process::class);
85-
$this->jsonReport = sys_get_temp_dir() . '/metrics-' . uniqid() . '.json';
86-
$this->summaryReport = sys_get_temp_dir() . '/metrics-summary-' . uniqid() . '.json';
87-
$jsonReport = $this->jsonReport;
88-
$summaryReport = $this->summaryReport;
83+
$this->target = sys_get_temp_dir() . '/metrics-' . uniqid();
8984

90-
foreach (['exclude', 'report-html', 'report-json', 'report-summary-json', 'junit'] as $option) {
85+
foreach (['exclude', 'target', 'junit'] as $option) {
9186
$this->input->getOption($option)
9287
->willReturn($this->commandDefaultOption($option));
9388
}
@@ -98,16 +93,7 @@ protected function setUp(): void
9893
->willReturn($this->process->reveal());
9994

10095
$this->processQueue->run($this->output->reveal())
101-
->will(static function () use ($summaryReport, $jsonReport): int {
102-
file_put_contents($summaryReport, <<<'JSON'
103-
{"OOP":{"classes":2},"Complexity":{"avgCyclomaticComplexityByClass":4}}
104-
JSON);
105-
file_put_contents($jsonReport, <<<'JSON'
106-
{"App\\Foo":{"_type":"Hal\\Metric\\ClassMetric","mi":80,"methods":[{"_type":"Hal\\Metric\\FunctionMetric"},{"_type":"Hal\\Metric\\FunctionMetric"}]},"App\\Bar":{"_type":"Hal\\Metric\\ClassMetric","mi":70}}
107-
JSON);
108-
109-
return MetricsCommand::SUCCESS;
110-
});
96+
->willReturn(MetricsCommand::SUCCESS);
11197

11298
$this->command = new MetricsCommand($this->processBuilder->reveal(), $this->processQueue->reveal());
11399
}
@@ -117,7 +103,7 @@ protected function setUp(): void
117103
*/
118104
protected function tearDown(): void
119105
{
120-
foreach ([$this->jsonReport, $this->summaryReport] as $path) {
106+
foreach ([$this->target . '/report.json', $this->target . '/report-summary.json'] as $path) {
121107
if (file_exists($path)) {
122108
\unlink($path);
123109
}
@@ -149,9 +135,10 @@ public function commandWillHaveExpectedOptions(): void
149135
self::assertFalse($definition->hasOption('working-dir'));
150136
self::assertFalse($definition->hasOption('src'));
151137
self::assertTrue($definition->hasOption('exclude'));
152-
self::assertTrue($definition->hasOption('report-html'));
153-
self::assertTrue($definition->hasOption('report-json'));
154-
self::assertTrue($definition->hasOption('report-summary-json'));
138+
self::assertTrue($definition->hasOption('target'));
139+
self::assertFalse($definition->hasOption('report-html'));
140+
self::assertFalse($definition->hasOption('report-json'));
141+
self::assertFalse($definition->hasOption('report-summary-json'));
155142
self::assertTrue($definition->hasOption('junit'));
156143
self::assertFalse($definition->hasOption('cache-dir'));
157144
}
@@ -176,10 +163,15 @@ public function executeWillRunPhpMetrics(): void
176163
)
177164
->shouldBeCalledOnce()
178165
->willReturn($this->processBuilder->reveal());
179-
$this->processBuilder->withArgument('--report-json', $this->jsonReport)
166+
$this->processBuilder->withArgument('--target', null)
167+
->shouldNotBeCalled();
168+
$this->processBuilder->withArgument('--report-html', $this->target)
169+
->shouldBeCalledOnce()
170+
->willReturn($this->processBuilder->reveal());
171+
$this->processBuilder->withArgument('--report-json', $this->target . '/report.json')
180172
->shouldBeCalledOnce()
181173
->willReturn($this->processBuilder->reveal());
182-
$this->processBuilder->withArgument('--report-summary-json', $this->summaryReport)
174+
$this->processBuilder->withArgument('--report-summary-json', $this->target . '/report-summary.json')
183175
->shouldBeCalledOnce()
184176
->willReturn($this->processBuilder->reveal());
185177
$this->processBuilder->withArgument('--junit', null)
@@ -199,17 +191,18 @@ public function executeWillRunPhpMetrics(): void
199191
#[Test]
200192
public function executeWillSkipUnsetOptionalReports(): void
201193
{
202-
$this->input->getOption('report-json')
203-
->willReturn(null);
204-
$this->input->getOption('report-summary-json')
205-
->willReturn(null);
194+
$this->input->getOption('target')
195+
->willReturn('build/metrics/');
206196
$this->input->getOption('junit')
207197
->willReturn(null);
208198

209-
$this->processBuilder->withArgument('--report-json', null)
199+
$this->processBuilder->withArgument('--report-html', 'build/metrics')
200+
->shouldBeCalledOnce()
201+
->willReturn($this->processBuilder->reveal());
202+
$this->processBuilder->withArgument('--report-json', 'build/metrics/report.json')
210203
->shouldBeCalledOnce()
211204
->willReturn($this->processBuilder->reveal());
212-
$this->processBuilder->withArgument('--report-summary-json', null)
205+
$this->processBuilder->withArgument('--report-summary-json', 'build/metrics/report-summary.json')
213206
->shouldBeCalledOnce()
214207
->willReturn($this->processBuilder->reveal());
215208
$this->processBuilder->withArgument('--junit', Argument::any())
@@ -224,14 +217,12 @@ public function executeWillSkipUnsetOptionalReports(): void
224217
* @return void
225218
*/
226219
#[Test]
227-
public function executeWillIncludeHtmlReportWhenRequested(): void
220+
public function executeWillIncludeJunitReportWhenRequested(): void
228221
{
229-
$this->input->getOption('report-html')
230-
->willReturn('build/metrics');
231222
$this->input->getOption('junit')
232-
->willReturn(null);
223+
->willReturn('build/metrics/junit.xml');
233224

234-
$this->processBuilder->withArgument('--report-html', 'build/metrics')
225+
$this->processBuilder->withArgument('--junit', 'build/metrics/junit.xml')
235226
->shouldBeCalledOnce()
236227
->willReturn($this->processBuilder->reveal());
237228
$this->processQueue->add($this->process->reveal())
@@ -249,8 +240,7 @@ private function commandDefaultOption(string $option): mixed
249240
{
250241
return match ($option) {
251242
'exclude' => 'vendor,test,tests,tmp,cache,spec,build,backup,resources',
252-
'report-json' => $this->jsonReport,
253-
'report-summary-json' => $this->summaryReport,
243+
'target' => $this->target,
254244
'junit' => null,
255245
default => null,
256246
};

tests/Console/Command/ReportsCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function executeWillRunDocsAsDetachedAndTestsAndMetricsInSequence(): void
168168
->shouldBeCalledOnce()
169169
->willReturn($this->processBuilder->reveal());
170170

171-
$this->processBuilder->withArgument('--report-html', 'public/metrics')
171+
$this->processBuilder->withArgument('--target', 'public/metrics')
172172
->shouldBeCalledOnce()
173173
->willReturn($this->processBuilder->reveal());
174174
$this->processBuilder->withArgument('--junit', 'public/coverage/junit.xml')
@@ -198,7 +198,7 @@ public function executeWillRunMetricsCommandWhenRequested(): void
198198
$this->input->getOption('metrics')
199199
->willReturn('tmp/metrics');
200200

201-
$this->processBuilder->withArgument('--report-html', 'tmp/metrics')
201+
$this->processBuilder->withArgument('--target', 'tmp/metrics')
202202
->shouldBeCalledOnce()
203203
->willReturn($this->processBuilder->reveal());
204204
$this->processBuilder->withArgument('--junit', 'public/coverage/junit.xml')

0 commit comments

Comments
 (0)