Skip to content

Commit

Permalink
Fix JSON reports with long UTF8 strings
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Mar 1, 2021
1 parent bca09d7 commit 6d3166c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Json/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public static function encode($data, ?int $options = null): string

$result = json_encode($data, $options);
if ($result === false) {
throw new RuntimeException('Cannot create JSON string.');
/** @psalm-suppress ImpureFunctionCall */
throw new RuntimeException('Cannot create JSON string: '.json_last_error_msg());
}

return $result;
Expand Down
8 changes: 4 additions & 4 deletions src/Psalm/Type/Atomic/TLiteralString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
namespace Psalm\Type\Atomic;

use function preg_replace;
use function strlen;
use function substr;
use function mb_strlen;
use function mb_substr;

/**
* Denotes a string whose value is known.
Expand Down Expand Up @@ -31,8 +31,8 @@ public function __toString(): string
public function getId(bool $nested = false): string
{
$no_newline_value = preg_replace("/\n/m", '\n', $this->value);
if (strlen($this->value) > 80) {
return '"' . substr($no_newline_value, 0, 80) . '...' . '"';
if (mb_strlen($this->value) > 80) {
return '"' . mb_substr($no_newline_value, 0, 80) . '...' . '"';
}

return '"' . $no_newline_value . '"';
Expand Down
9 changes: 9 additions & 0 deletions tests/TypeParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,15 @@ public function testEnumWithoutSpaces(): void
$this->assertSame($resolved_type->getId(), $docblock_type->getId());
}

public function testLongUtf8LiteralString(): void
{
$string = "АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя";
$string .= $string;
$expected = mb_substr($string, 0, 80);
$this->assertSame("\"$expected...\"", Type:: parseString("'$string'")->getId());
$this->assertSame("\"$expected...\"", Type:: parseString("\"$string\"")->getId());
}

public function testSingleLiteralString(): void
{
$this->assertSame(
Expand Down

0 comments on commit 6d3166c

Please sign in to comment.