Skip to content

Commit 3216928

Browse files
committed
fix: Truncate multibyte namespaces in command
1 parent 33110c5 commit 3216928

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

system/Commands/Utilities/Namespaces.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ private function outputAllNamespaces(array $params): array
120120

121121
private function truncate(string $string, int $max): string
122122
{
123-
$length = strlen($string);
123+
$length = mb_strlen($string);
124124

125125
if ($length > $max) {
126-
return substr($string, 0, $max - 3) . '...';
126+
return mb_substr($string, 0, $max - 3) . '...';
127127
}
128128

129129
return $string;

tests/system/Commands/Utilities/NamespacesTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Commands\Utilities;
1515

1616
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\ReflectionHelper;
1718
use CodeIgniter\Test\StreamFilterTrait;
1819
use PHPUnit\Framework\Attributes\Group;
1920

@@ -24,6 +25,7 @@
2425
final class NamespacesTest extends CIUnitTestCase
2526
{
2627
use StreamFilterTrait;
28+
use ReflectionHelper;
2729

2830
protected function setUp(): void
2931
{
@@ -84,4 +86,14 @@ public function testNamespacesCommandAllNamespaces(): void
8486
str_replace(' ', '', $this->getBuffer())
8587
);
8688
}
89+
90+
public function testTruncateNamespaces(): void
91+
{
92+
$commandObject = new Namespaces(service('logger'), service('commands'));
93+
$truncateRunner = $this->getPrivateMethodInvoker($commandObject, 'truncate');
94+
95+
$this->assertSame('App\Controllers\...', $truncateRunner('App\Controllers\Admin', 19));
96+
// multibyte namespace
97+
$this->assertSame('App\Контроллеры\...', $truncateRunner('App\Контроллеры\Админ', 19));
98+
}
8799
}

0 commit comments

Comments
 (0)