Skip to content

Commit

Permalink
Fix wrong metric object handed over to BeforeMetricCalculate event
Browse files Browse the repository at this point in the history
  • Loading branch information
DZunke committed Apr 8, 2024
1 parent 5abb5b8 commit a93ba1b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Collector/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function collect(): Result
foreach ($executingGroup->metrics as $executingMetric) {
$metricHandler = $this->runtimeConfiguration->getMetric($executingMetric->metric);

$event = new BeforeMetricCalculate($metricHandler, $executingMetric->options);
$event = new BeforeMetricCalculate($executingMetric, $executingMetric->options);
$this->runtimeConfiguration->getEventDispatcher()->dispatch($event);

$metricResult = $metricHandler->calculate($event->getOptions());
Expand Down
7 changes: 5 additions & 2 deletions src/Configuration/ConfigurationFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static function buildMetricGroupConfig(array $metricGroupConfig): array
$metricGroup[] = new MetricGroup(
$groupIdentifier,
$options['title'] ?? '',
self::convertToMetricConfig($options['metrics'] ?? []),
self::convertToMetricConfig($groupIdentifier, $options['metrics'] ?? []),
);
}

Expand All @@ -109,7 +109,7 @@ private static function buildMetricGroupConfig(array $metricGroupConfig): array
*
* @return list<Metric>
*/
private static function convertToMetricConfig(array $metricConfig): array
private static function convertToMetricConfig(string $groupIdentifier, array $metricConfig): array
{
$metrics = [];
foreach ($metricConfig as $identifier => $options) {
Expand All @@ -123,6 +123,9 @@ private static function convertToMetricConfig(array $metricConfig): array
// Take the metric not from the key but from the option
$metric = $options['metric'];
unset($options['metric']);
} else {
// Because the name of the metric was utilized as key it is not unique, so combine it with the group
$identifier = $groupIdentifier . '.' . $identifier;
}

$metrics[] = new Metric($identifier, $metric, $title, $options);
Expand Down
8 changes: 4 additions & 4 deletions src/Configuration/ConfigurationFile/MetricGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use Panaly\Configuration\Exception\InvalidConfigurationFile;

class MetricGroup
readonly class MetricGroup
{
/** @param list<Metric> $metrics */
public function __construct(
public readonly string $identifier,
public readonly string $title,
public readonly array $metrics,
public string $identifier,
public string $title,
public array $metrics,
) {
if ($this->identifier === '') {
throw InvalidConfigurationFile::metricGroupMustNotHaveABlankName();
Expand Down
4 changes: 2 additions & 2 deletions src/Event/BeforeMetricCalculate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Panaly\Event;

use Panaly\Plugin\Plugin\Metric;
use Panaly\Configuration\ConfigurationFile\Metric;

final class BeforeMetricCalculate
{
public function __construct(
public readonly Metric $plugin,
public readonly Metric $metricConfiguration,
private array $options,
) {
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Configuration/ConfigurationFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public function testThatCreatingWithMetricGroupsIsValid(): void
self::assertCount(3, $configurationFile->metricGroups[0]->metrics);

// The "baz" metric
self::assertSame('baz', $configurationFile->metricGroups[0]->metrics[0]->identifier);
self::assertSame('foo.baz', $configurationFile->metricGroups[0]->metrics[0]->identifier);
self::assertSame('baz', $configurationFile->metricGroups[0]->metrics[0]->metric);
self::assertSame('foo_baz', $configurationFile->metricGroups[0]->metrics[0]->title);
self::assertSame(['foo' => 'bar'], $configurationFile->metricGroups[0]->metrics[0]->options);

// The "quo" metric
self::assertSame('quo', $configurationFile->metricGroups[0]->metrics[1]->identifier);
self::assertSame('foo.quo', $configurationFile->metricGroups[0]->metrics[1]->identifier);
self::assertSame('quo', $configurationFile->metricGroups[0]->metrics[1]->metric);
self::assertNull($configurationFile->metricGroups[0]->metrics[1]->title);
self::assertSame(['foo' => 'baz'], $configurationFile->metricGroups[0]->metrics[1]->options);
Expand Down

0 comments on commit a93ba1b

Please sign in to comment.