diff --git a/src/Internal/Reporting/ViolationCollector.php b/src/Internal/Reporting/ViolationCollector.php index ca4d77ff..564bc171 100644 --- a/src/Internal/Reporting/ViolationCollector.php +++ b/src/Internal/Reporting/ViolationCollector.php @@ -267,7 +267,7 @@ public static function writeWorkerShard(string $reportFile): void 'violations' => array_values(self::$violations), 'instrumented_files' => StreamWrapper::getInstrumentedFiles(), ]; - $content = json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + $content = json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE); if ($content !== false) { @file_put_contents($shardFile, $content); @@ -294,7 +294,7 @@ public static function exportReport(?string $filePath = null): ?string } $document = self::buildReportDocument($allViolations); - $json = json_encode($document, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + $json = json_encode($document, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE); if ($json === false || @file_put_contents($targetFile, $json) === false) { return null; diff --git a/tests/Internal/Reporting/ViolationCollectorTest.php b/tests/Internal/Reporting/ViolationCollectorTest.php index eb98300e..2262db85 100644 --- a/tests/Internal/Reporting/ViolationCollectorTest.php +++ b/tests/Internal/Reporting/ViolationCollectorTest.php @@ -5,6 +5,7 @@ namespace TypePHP\Tests\Internal\Reporting; use TypePHP\Internal\Diagnostic\ErrorMessage; +use TypePHP\Internal\Diagnostic\TypeFormatter; use TypePHP\Internal\Reporting\ViolationCollector; use TypePHP\Internal\Reporting\ViolationRecord; use TypePHP\Internal\Util\Config; @@ -491,6 +492,54 @@ function: 'test', @rmdir($tempDir); } }); + + test('exports report and worker shard when a violation carries invalid UTF-8', function () { + $tempDir = sys_get_temp_dir() . '/typephp_utf8_test_' . uniqid(); + mkdir($tempDir, 0777, true); + $reportFile = $tempDir . '/typephp-report.json'; + + // 16 ASCII bytes followed by 2-byte characters: the 17-byte preview cut splits the first "ä". + $given = TypeFormatter::formatGivenValue(str_repeat('a', 16) . 'ääääää'); + expect(preg_match('//u', $given))->toBeFalse(); + + putenv('TEST_TOKEN=7'); + + try { + Config::set([ + 'on_violation' => 'report', + 'report_file' => $reportFile, + ]); + + $err = new ErrorMessage("Argument \$name must be of type int, {$given} given"); + ViolationCollector::handle($err, 'parameter', null, 'src/Action.php', 10); + + expect(ViolationCollector::exportReport($reportFile))->toBe($reportFile); + + $doc = json_decode((string) file_get_contents($reportFile), true); + expect($doc['summary']['total_violations'])->toBe(1) + ->and($doc['violations'][0]['message'])->toContain('aaaaaaaaaaaaaaaa') + ; + + ViolationCollector::writeWorkerShard($reportFile); + + $shards = glob($tempDir . '/.typephp-shards/shard_7_*.json'); + expect($shards)->toHaveCount(1); + + $shard = json_decode((string) file_get_contents($shards[0]), true); + expect($shard['violations'])->toHaveCount(1); + } finally { + putenv('TEST_TOKEN'); + $files = glob($tempDir . '/.typephp-shards/*'); + if ($files !== false) { + foreach ($files as $f) { + @unlink($f); + } + } + @rmdir($tempDir . '/.typephp-shards'); + @unlink($reportFile); + @rmdir($tempDir); + } + }); }); describe('Metadata Parser', function () {