isFile() || $file->getExtension() !== 'php') { continue; } $path = str_replace($root, '', $file->getPathname()); if (!str_starts_with($path, '/app/') && !str_starts_with($path, '/tests/')) { continue; } foreach ($allowlist as $allowed) { if (str_starts_with($path, $allowed) || $path === $allowed) { continue 2; } } $lines = file($file->getPathname(), FILE_IGNORE_NEW_LINES) ?: []; foreach ($lines as $lineNumber => $line) { foreach ($patterns as $pattern) { if (preg_match('/' . $pattern . '/i', $line)) { $violations[] = sprintf('%s:%d matches /%s/i', $path, $lineNumber + 1, $pattern); } } } } if ($violations !== []) { sort($violations); } $baselineFile = $root . '/scripts/financial-formula-baseline.txt'; if (in_array('--update-baseline', $argv, true)) { file_put_contents($baselineFile, implode("\n", $violations) . ($violations === [] ? '' : "\n")); echo 'Updated financial formula baseline with ' . count($violations) . " entries.\n"; exit(0); } $baseline = []; if (is_file($baselineFile)) { $baseline = array_values(array_filter(file($baselineFile, FILE_IGNORE_NEW_LINES) ?: [], static fn ($line) => trim($line) !== '')); sort($baseline); } $newViolations = array_values(array_diff($violations, $baseline)); if ($newViolations !== []) { fwrite(STDERR, "New financial formula patterns found outside allowlist:\n"); fwrite(STDERR, implode("\n", $newViolations) . "\n"); fwrite(STDERR, "Run `php scripts/check-financial-formulas.php --update-baseline` only after reviewing intentional legacy exceptions.\n"); exit(1); } echo 'No new financial formula patterns found outside baseline. Current baseline entries: ' . count($baseline) . ".\n";