83 lines
2.6 KiB
PHP
83 lines
2.6 KiB
PHP
<?php
|
|
|
|
$root = dirname(__DIR__);
|
|
$patterns = [
|
|
'refund_paid_amount',
|
|
'SUM\\(paid_amount\\)',
|
|
'SUM\\(amount\\)',
|
|
'newBalance',
|
|
'balance\\s*=',
|
|
'total\\s*-\\s*discount',
|
|
'total\\s*-\\s*paid',
|
|
'Partially Paid',
|
|
'Unpaid',
|
|
'Paid',
|
|
];
|
|
|
|
$allowlist = [
|
|
'/app/Libraries/InvoiceLedgerService.php',
|
|
'/app/Libraries/ParentLedgerService.php',
|
|
'/app/Libraries/FinancialReportProjectionService.php',
|
|
'/app/Libraries/FinancialCorrectionReportService.php',
|
|
'/app/Libraries/FinancialStatus.php',
|
|
'/app/Database/Migrations/',
|
|
'/tests/',
|
|
'/scripts/check-financial-formulas.php',
|
|
];
|
|
|
|
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS));
|
|
$violations = [];
|
|
|
|
foreach ($rii as $file) {
|
|
if (!$file->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";
|