Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Internal/Reporting/ViolationCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions tests/Internal/Reporting/ViolationCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand Down
Loading