Skip to content

Commit 317ffef

Browse files
committed
Fix isVisible() initial value bug in NestedListStringFormatter
The array_reduce in isVisible() used `true` as its initial value with an `||` reducer, meaning it always returned `true` when siblings existed regardless of their actual visibility. This caused unnecessary nesting in full messages by showing single-child wrapper nodes that should have been collapsed. Replace array_reduce with a foreach loop starting from `false`, which also enables early return on the first visible sibling.
1 parent 50a3ab3 commit 317ffef

3 files changed

Lines changed: 13 additions & 17 deletions

File tree

src/Message/Formatter/NestedListStringFormatter.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Respect\Validation\Result;
1818

1919
use function array_filter;
20-
use function array_reduce;
2120
use function count;
2221
use function rtrim;
2322
use function sprintf;
@@ -89,13 +88,14 @@ private function isVisible(Result $result, Result ...$siblings): bool
8988
}
9089

9190
// The visibility of a result then will depend on whether any of its siblings is visible
92-
return array_reduce(
93-
$siblings,
94-
fn(bool $carry, Result $currentSibling) => $carry || $this->isVisible(
95-
$currentSibling,
96-
...array_filter($siblings, static fn(Result $sibling) => $sibling !== $currentSibling),
97-
),
98-
true,
99-
);
91+
foreach ($siblings as $key => $currentSibling) {
92+
$otherSiblings = $siblings;
93+
unset($otherSiblings[$key]);
94+
if ($this->isVisible($currentSibling, ...$otherSiblings)) {
95+
return true;
96+
}
97+
}
98+
99+
return false;
100100
}
101101
}

tests/feature/AssertWithPropertiesTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@
4343
]))),
4444
fn(string $fullMessage) => expect($fullMessage)->toBe(<<<'FULL_MESSAGE'
4545
- the given data must pass all the rules
46-
- `.mysql` must pass the rules
47-
- `.mysql.host` must be a string
48-
- `.postgresql` must pass the rules
49-
- `.postgresql.user` must be a string
46+
- `.mysql.host` must be a string
47+
- `.postgresql.user` must be a string
5048
FULL_MESSAGE),
5149
));

tests/feature/Issues/Issue796Test.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@
4646
->and($message)->toBe('`.mysql.host` (<- the given data) must be a string')
4747
->and($fullMessage)->toBe(<<<'FULL_MESSAGE'
4848
- the given data must pass all the rules
49-
- `.mysql` must pass the rules
50-
- `.mysql.host` must be a string
51-
- `.postgresql` must pass the rules
52-
- `.postgresql.user` must be a string
49+
- `.mysql.host` must be a string
50+
- `.postgresql.user` must be a string
5351
FULL_MESSAGE)
5452
->and($messages)->toBe([
5553
'__root__' => 'the given data must pass all the rules',

0 commit comments

Comments
 (0)