Skip to content

Commit

Permalink
Fix truncating invalid UTF-8 strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordi Freixa Serrabassa authored Oct 23, 2020
1 parent 4034017 commit 0dee4c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Type/Constant/ConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
},
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Type/Constant/ConstantStringTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

}

0 comments on commit 0dee4c3

Please sign in to comment.