<?php
$root = realpath(__DIR__ . '/..');
$files = array_merge(
    glob($root . '/validators/*.php') ?: [],
    glob($root . '/prompts/*.php') ?: [],
    glob($root . '/app/**/*.php') ?: []
);
$bad = [];
foreach ($files as $file) {
    $lines = file($file);
    foreach ($lines as $num => $line) {
        if (!preg_match_all("/preg_match\\s*\\(\\s*('([^'\\\\]|\\\\.)*'|\"([^\"\\\\]|\\\\.)*\")/s", $line, $m)) {
            continue;
        }
        foreach ($m[1] as $patLit) {
            $pat = eval('return ' . $patLit . ';');
            $ok = @preg_match($pat, '');
            if ($ok === false && preg_last_error() !== PREG_NO_ERROR) {
                $bad[] = [
                    'file' => str_replace($root . DIRECTORY_SEPARATOR, '', $file),
                    'line' => $num + 1,
                    'error' => preg_last_error(),
                    'pattern' => substr($pat, 0, 120),
                ];
            }
        }
    }
}
echo json_encode($bad, JSON_PRETTY_PRINT), PHP_EOL;
