You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/UserCopy/UserCopyCoverageScanner.php

66 lines
2.0 KiB
PHP

<?php
namespace App\Services\UserCopy;
/** Reports source patterns that must disappear as pages join the copy catalog. */
final class UserCopyCoverageScanner
{
private const RULES = [
'direct_toast_text' => '/\btoast\s*\(\s*[\'\"]/',
'direct_confirm_text' => '/\b(?:window\.)?confirm\s*\(\s*[\'\"]/',
'hardcoded_zh_locale' =>
'/\.toLocale(?:String|DateString|TimeString)\s*\(\s*[\'\"]zh-CN[\'\"]/',
'legacy_language_branch' => '/\$language\s*={2,3}\s*[\'\"]true[\'\"]/',
'visible_chinese_literal' => '/\p{Han}/u',
];
/**
* Lines can be temporarily excluded with a documented marker while a
* migration is in progress. The final strict gate will reject markers
* without a non-empty reason.
*/
public static function scanContents(string $path, string $contents): array
{
$violations = [];
$lines = preg_split('/\R/', $contents);
if (!is_array($lines)) {
return $violations;
}
foreach ($lines as $offset => $line) {
if (preg_match('/user-copy:\s*allow\s+\S+/i', $line) === 1) {
continue;
}
foreach (self::RULES as $rule => $pattern) {
if (preg_match($pattern, $line) !== 1) {
continue;
}
$violations[] = [
'path' => $path,
'line' => $offset + 1,
'rule' => $rule,
'snippet' => trim($line),
];
}
}
return $violations;
}
public static function scanFiles(array $paths): array
{
$violations = [];
foreach ($paths as $path) {
if (!is_string($path) || !is_file($path)) {
continue;
}
$contents = file_get_contents($path);
if (!is_string($contents)) {
continue;
}
array_push($violations, ...self::scanContents($path, $contents));
}
return $violations;
}
}