Skip to content

Commit 257c8a1

Browse files
committed
Fix common prefix computation so it only goes up to the last backslash
1 parent f7a5333 commit 257c8a1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Structures/NamespacedType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public static function commonPrefix(string $a, string $b): string {
3434
$i++;
3535
}
3636

37-
return substr($a, 0, $i);
37+
$prefix = substr($a, 0, $i);
38+
$lastSlashPos = strrpos($prefix, '\\');
39+
40+
return $lastSlashPos !== false ? substr($prefix, 0, $lastSlashPos + 1) : '';
3841
}
3942

4043
public static function shortName(string|ReflectionClass $type): string {

tests/Structures/NamespacedTypeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,19 @@
2121
)
2222
);
2323
});
24+
it('common prefix goes up to last backslash even with another nesting level', function () {
25+
assertEquals(
26+
'app\\', // not 'app\\co'
27+
NamespacedType::commonPrefix(
28+
'app\\companies',
29+
'app\\countries'
30+
)
31+
);
32+
assertEquals(
33+
'app\\',
34+
NamespacedType::commonPrefix(
35+
'app\\data',
36+
'app\\data\\stuff'
37+
)
38+
);
39+
});

0 commit comments

Comments
 (0)