Skip to content

Commit 411e64a

Browse files
committed
Normalize object array representation keys
1 parent 695d76c commit 411e64a

9 files changed

Lines changed: 87 additions & 198 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ All Notable changes to `bakame/stackwatch` will be documented in this file.
2121
- **BC BREAK:** `Visibility` is renamed `Display`
2222
- **BC BREAK:** `State` is renamed `Feature`
2323
- **BC BREAK:** `Input::TABLE_FORMAT` is renamed `Input::TEXT_FORMAT`
24+
- Added a `Translator` class to improve human-readable key translation.
2425

2526
### Fixed
2627

@@ -29,6 +30,8 @@ All Notable changes to `bakame/stackwatch` will be documented in this file.
2930
- **BC BREAK:** `Snapshot::cpu` property is replaced by 2 properties `Snapshot::cpuUserTime` and `Snapshot::cpuSystemTime`.
3031
- `LabelGenerator::generate` now also accepts the hyphen (`-`) character.
3132
- Adding support for `Timeline` on `Metrics::sum` and `Metrics::average` static methods.
33+
- **BC BREAK:** Fix `Report` and `Environment` keys used for `toArray` and `forHuman`
34+
- **BC BREAK:** Fix `Statistics::count` is not `Statistics::iterations` keys used for `toArray` and `forHuman`
3235

3336
### Deprecated
3437

src/Console/ConsoleFormatter.php

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@
66

77
use Bakame\Stackwatch\Exporter\ConsoleExporter;
88
use Bakame\Stackwatch\Exporter\LeaderPrinter;
9-
use Bakame\Stackwatch\Metrics;
109
use Bakame\Stackwatch\Report;
10+
use Bakame\Stackwatch\Translator;
1111
use Throwable;
1212

13-
/**
14-
* @phpstan-import-type MetricsHumanReadable from Metrics
15-
*/
1613
final class ConsoleFormatter implements Formatter
1714
{
1815
public function __construct(
1916
public readonly ConsoleExporter $exporter,
2017
public readonly LeaderPrinter $leaderPrinter = new LeaderPrinter(),
2118
public readonly Feature $dryRun = Feature::Disabled,
19+
public readonly Translator $translator = new Translator(),
2220
) {
2321
}
2422

@@ -43,36 +41,9 @@ public function format(iterable $unitOfWorks): void
4341
continue;
4442
}
4543

46-
/** @var MetricsHumanReadable $data */
47-
$data = $stats->forHuman();
48-
$this->exporter->output->writeln($this->leaderPrinter->render(self::exchangeKeys($data)));
44+
$data = $this->leaderPrinter->render($this->translator->translateArrayKeys($stats->forHuman())); /* @phpstan-ignore-line */
45+
$this->exporter->output->writeln($data);
4946
$this->exporter->output->writeln('');
5047
}
5148
}
52-
53-
/**
54-
* @param MetricsHumanReadable $metrics
55-
*
56-
* @return array<string, string>
57-
*/
58-
private static function exchangeKeys(array $metrics): array
59-
{
60-
static $humanKeys = [
61-
'cpu_time' => 'CPU Time',
62-
'memory_usage' => 'Memory Usage',
63-
'real_memory_usage' => 'Real Memory Usage',
64-
'peak_memory_usage' => 'Peak Memory Usage',
65-
'real_peak_memory_usage' => 'Real Peak Memory Usage',
66-
'execution_time' => 'Execution Time',
67-
];
68-
69-
$humans = [];
70-
foreach ($metrics as $key => $metric) {
71-
/** @var string $humanKey */
72-
$humanKey = $humanKeys[$key] ?? $key;
73-
$humans[ $humanKey] = $metric;
74-
}
75-
76-
return $humans;
77-
}
7849
}

src/Console/ConsoleHandler.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Bakame\Stackwatch\Environment;
88
use Bakame\Stackwatch\Exporter\LeaderPrinter;
9+
use Bakame\Stackwatch\Translator;
910
use Bakame\Stackwatch\Version;
1011
use Bakame\Stackwatch\Warning;
1112
use Psr\Log\LoggerInterface;
@@ -21,6 +22,8 @@ public function __construct(
2122
private readonly OutputInterface $stdout,
2223
private readonly LoggerInterface $logger,
2324
private readonly Environment $environment,
25+
private readonly LeaderPrinter $leaderPrinter = new LeaderPrinter(),
26+
private readonly Translator $translator = new Translator(),
2427
) {
2528
}
2629

@@ -34,16 +37,14 @@ public function handle(Input $input): void
3437
$output = new StreamOutput($handler);
3538
}
3639

37-
$profiler = PathProfiler::forConsole($input, $output, $this->logger);
40+
$profiler = PathProfiler::forConsole($input, $output, $this->logger, $this->translator);
3841
$output->writeln(Version::toConsoleString());
3942

4043
if ($input->infoSection->isVisible()) {
4144
$formatter = $profiler->formatter;
4245
if ($formatter instanceof ConsoleFormatter) {
43-
$leaderPrinter = new LeaderPrinter(filler: '.', padExtra: 1);
44-
/** @var EnvironmentHumanReadable $data */
45-
$data = $this->environment->forHuman();
46-
$formatter->exporter->output->writeln($leaderPrinter->render($data));
46+
$data = $this->translator->translateArrayKeys($this->environment->forHuman()); /* @phpstan-ignore-line */
47+
$formatter->exporter->output->writeln($this->leaderPrinter->render($data));
4748
$formatter->exporter->output->writeln('');
4849
}
4950
} else {

src/Console/PathProfiler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Bakame\Stackwatch\Exporter\JsonExporter;
1010
use Bakame\Stackwatch\Exporter\LeaderPrinter;
1111
use Bakame\Stackwatch\Profile;
12+
use Bakame\Stackwatch\Translator;
1213
use Bakame\Stackwatch\UnableToProfile;
1314
use CallbackFilterIterator;
1415
use FilesystemIterator;
@@ -50,10 +51,11 @@ public static function forConsole(
5051
Input $input,
5152
OutputInterface $output = new ConsoleOutput(),
5253
LoggerInterface $logger = new NullLogger(),
54+
Translator $translator = new Translator(),
5355
): self {
5456
return new self(
5557
new UnitOfWorkGenerator(new PathInspector(Profile::class), $logger),
56-
new ConsoleFormatter(new ConsoleExporter($output), new LeaderPrinter(), $input->dryRun),
58+
new ConsoleFormatter(new ConsoleExporter($output), new LeaderPrinter(), $input->dryRun, $translator),
5759
$input,
5860
$logger,
5961
);

src/Console/Stackwatch.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Bakame\Stackwatch\Console;
66

77
use Bakame\Stackwatch\Environment;
8+
use Bakame\Stackwatch\Exporter\LeaderPrinter;
9+
use Bakame\Stackwatch\Translator;
810
use Bakame\Stackwatch\Version;
911
use Bakame\Stackwatch\Warning;
1012
use Psr\Log\LoggerInterface;
@@ -70,6 +72,8 @@ public function __construct(
7072
private readonly OutputInterface $stderr,
7173
private readonly LoggerInterface $logger,
7274
private readonly Environment $environment,
75+
private readonly LeaderPrinter $leaderPrinter = new LeaderPrinter(),
76+
private readonly Translator $translator = new Translator(),
7377
) {
7478
}
7579

@@ -116,7 +120,7 @@ public function execute(Input $input): int
116120

117121
try {
118122
(match ($input->format) {
119-
Input::TEXT_FORMAT => new ConsoleHandler($this->stdout, $this->logger, $this->environment),
123+
Input::TEXT_FORMAT => new ConsoleHandler($this->stdout, $this->logger, $this->environment, $this->leaderPrinter, $this->translator),
120124
Input::JSON_FORMAT => new JsonHandler($this->logger, $this->environment),
121125
default => throw new RuntimeException('Unknown output format: '.$input->format),
122126
})->handle($input);

0 commit comments

Comments
 (0)