diff --git a/src/Type/Constant/ConstantStringType.php b/src/Type/Constant/ConstantStringType.php index 5ea29af96b..376fe45eb1 100644 --- a/src/Type/Constant/ConstantStringType.php +++ b/src/Type/Constant/ConstantStringType.php @@ -61,8 +61,14 @@ function (): string { return var_export($this->value, true); } + try { + $truncatedValue = \Nette\Utils\Strings::truncate($this->value, self::DESCRIBE_LIMIT); + } catch (\Nette\Utils\RegexpException $e) { + $truncatedValue = substr($this->value, 0, self::DESCRIBE_LIMIT) . "\u{2026}"; + } + return var_export( - \Nette\Utils\Strings::truncate($this->value, self::DESCRIBE_LIMIT), + $truncatedValue, true ); }, diff --git a/tests/PHPStan/Type/Constant/ConstantStringTypeTest.php b/tests/PHPStan/Type/Constant/ConstantStringTypeTest.php index dfe9fb2425..5e28ae9a68 100644 --- a/tests/PHPStan/Type/Constant/ConstantStringTypeTest.php +++ b/tests/PHPStan/Type/Constant/ConstantStringTypeTest.php @@ -148,4 +148,14 @@ public function testGeneralize(): void $this->assertSame('class-string', (new ConstantStringType('NonexistentClass', true))->generalize()->describe(VerbosityLevel::precise())); } + public function testTextInvalidEncoding(): void + { + $this->assertSame("'\xc3Lorem ipsum dolor s\u{2026}'", (new ConstantStringType("\xc3Lorem ipsum dolor sit"))->describe(VerbosityLevel::value())); + } + + public function testShortTextInvalidEncoding(): void + { + $this->assertSame("'\xc3Lorem ipsum dolor'", (new ConstantStringType("\xc3Lorem ipsum dolor"))->describe(VerbosityLevel::value())); + } + }