Skip to content

Commit ff96af4

Browse files
authored
Merge pull request #502 from phpmetrics/fix/501
Manage better description inference for stable dependencies principle.
2 parents b50121e + 38d6958 commit ff96af4

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Hal/Violation/Package/StableDependenciesPrinciple.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ public function getLevel(): int
6060

6161
public function getDescription(): string
6262
{
63-
$count = count($this->violatingInstabilities);
64-
/** @var float $instability */
6563
$instability = $this->metric->getInstability();
64+
if (null === $instability) {
65+
return 'Packages should depend in the direction of stability.';
66+
}
67+
68+
$count = count($this->violatingInstabilities);
6669
$thisInstability = round($instability, 3);
6770
$packages = implode(
6871
"\n* ",

tests/Violation/Package/StableDependenciesPrincipleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ public static function provideMetricToCheckIfViolationApplies(): Generator
4040
{
4141
yield 'Invalid metric' => [Phake::mock(Metric::class), Phake::mock(ViolationsHandlerInterface::class), false];
4242

43+
$violationsHandler = Phake::mock(ViolationsHandlerInterface::class);
44+
$packageMetric = Phake::mock(PackageMetric::class);
45+
Phake::when($packageMetric)->__call('get', ['violations'])->thenReturn($violationsHandler);
46+
Phake::when($packageMetric)->__call('getInstability', [])->thenReturn(null);
47+
Phake::when($packageMetric)->__call('getDependentInstabilities', [])->thenReturn([]);
48+
yield 'Instability not defined' => [$packageMetric, $violationsHandler, false];
49+
4350
$violationsHandler = Phake::mock(ViolationsHandlerInterface::class);
4451
$packageMetric = Phake::mock(PackageMetric::class);
4552
Phake::when($packageMetric)->__call('get', ['violations'])->thenReturn($violationsHandler);
@@ -113,6 +120,14 @@ public function testViolationApplies(
113120
Phake::verify($violationsHandler)->__call('add', [$violation]);
114121
Phake::verifyNoOtherInteractions($violationsHandler);
115122
$instability = $metric->getInstability();
123+
124+
if (null === $instability) {
125+
$expectedDescription = 'Packages should depend in the direction of stability.';
126+
self::assertSame($expectedDescription, $violation->getDescription());
127+
return;
128+
}
129+
/** @var int $violatingInstabilities the inference */
130+
116131
$violatingInstabilities = array_filter(
117132
$metric->getDependentInstabilities(),
118133
static fn (float $otherInstability): bool => $otherInstability >= $instability

0 commit comments

Comments
 (0)