|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\UserCopy;
|
|
|
|
|
|
|
|
|
|
/** Reports source patterns that must disappear as pages join the copy catalog. */
|
|
|
|
|
final class UserCopyCoverageScanner
|
|
|
|
|
{
|
|
|
|
|
private const ALLOW_MARKER =
|
|
|
|
|
'/user-copy:\s*allow\s+(?:technical-constant|technical-regex|test-fixture|admin-compatibility)\s+\S+/i';
|
|
|
|
|
|
|
|
|
|
private const RULES = [
|
|
|
|
|
'direct_toast_text' => '/\btoast\s*\(\s*[\'\"]/',
|
|
|
|
|
'direct_confirm_text' => '/\b(?:window\.)?confirm\s*\(\s*[\'\"]/',
|
|
|
|
|
'hardcoded_zh_locale' =>
|
|
|
|
|
'/(?:\.toLocale(?:String|DateString|TimeString)|\b(?:new\s+)?Intl\.(?:DateTimeFormat|NumberFormat|RelativeTimeFormat|PluralRules|ListFormat|Collator))\s*\(\s*[\'\"]zh-CN[\'\"]/',
|
|
|
|
|
'legacy_language_branch' => '/\$language\s*={2,3}\s*[\'\"]true[\'\"]/',
|
|
|
|
|
'raw_backend_copy' =>
|
|
|
|
|
'/(?<![.\w\'\"`])(?:payload|data|result|response|task)\s*(?:(?:\?\.|\.)\s*(?:message|detail|msg|errmsg|error)\b|(?:\?\.)?\[\s*[\'\"](?:message|detail|msg|errmsg|error)[\'\"]\s*\])/',
|
|
|
|
|
'raw_error_feedback' =>
|
|
|
|
|
'/\b(?:toast|showError|inform|text)\s*\([^;\n]{0,240}\berror\s*(?:\?\.|\.)\s*message\b/',
|
|
|
|
|
'generated_copy_key' => '/\.dynamic\.h[0-9a-f]{6,}\b/i',
|
|
|
|
|
'visible_chinese_literal' =>
|
|
|
|
|
'/[\x{3400}-\x{4DBF}\x{4E00}-\x{9FFF}\x{F900}-\x{FAFF}]/u',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
private const TEMPLATE_RULES = [
|
|
|
|
|
'visible_english_text' =>
|
|
|
|
|
'/>\s*[A-Za-z][A-Za-z0-9 _.,:;\/+&()#-]{1,160}\s*</',
|
|
|
|
|
'visible_english_attribute' =>
|
|
|
|
|
'/\b(?:aria-label|placeholder|title)\s*=\s*[\'\"][A-Za-z][^\'\"{}$]*[\'\"]/',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A non-rendered technical constant, technical regular expression or
|
|
|
|
|
* test fixture can be excluded with a same-line, categorized marker and
|
|
|
|
|
* a non-empty reason. All other markers are violations.
|
|
|
|
|
*/
|
|
|
|
|
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 (stripos($line, 'user-copy: allow') !== false) {
|
|
|
|
|
if (preg_match(self::ALLOW_MARKER, $line) === 1) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$violations[] = [
|
|
|
|
|
'path' => $path,
|
|
|
|
|
'line' => $offset + 1,
|
|
|
|
|
'rule' => 'invalid_allow_marker',
|
|
|
|
|
'snippet' => trim($line),
|
|
|
|
|
];
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$rules = self::RULES;
|
|
|
|
|
if (str_ends_with(strtolower($path), '.tpl')) {
|
|
|
|
|
$rules += self::TEMPLATE_RULES;
|
|
|
|
|
}
|
|
|
|
|
foreach ($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;
|
|
|
|
|
}
|
|
|
|
|
}
|