Skip to content

Commit 0dee4c3

Browse files
author
Jordi Freixa Serrabassa
authored
Fix truncating invalid UTF-8 strings
1 parent 4034017 commit 0dee4c3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Type/Constant/ConstantStringType.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ function (): string {
6161
return var_export($this->value, true);
6262
}
6363

64+
try {
65+
$truncatedValue = \Nette\Utils\Strings::truncate($this->value, self::DESCRIBE_LIMIT);
66+
} catch (\Nette\Utils\RegexpException $e) {
67+
$truncatedValue = substr($this->value, 0, self::DESCRIBE_LIMIT) . "\u{2026}";
68+
}
69+
6470
return var_export(
65-
\Nette\Utils\Strings::truncate($this->value, self::DESCRIBE_LIMIT),
71+
$truncatedValue,
6672
true
6773
);
6874
},

tests/PHPStan/Type/Constant/ConstantStringTypeTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,14 @@ public function testGeneralize(): void
148148
$this->assertSame('class-string', (new ConstantStringType('NonexistentClass', true))->generalize()->describe(VerbosityLevel::precise()));
149149
}
150150

151+
public function testTextInvalidEncoding(): void
152+
{
153+
$this->assertSame("'\xc3Lorem ipsum dolor s\u{2026}'", (new ConstantStringType("\xc3Lorem ipsum dolor sit"))->describe(VerbosityLevel::value()));
154+
}
155+
156+
public function testShortTextInvalidEncoding(): void
157+
{
158+
$this->assertSame("'\xc3Lorem ipsum dolor'", (new ConstantStringType("\xc3Lorem ipsum dolor"))->describe(VerbosityLevel::value()));
159+
}
160+
151161
}

0 commit comments

Comments
 (0)