function check_for_typeof($dir)
{
    $r = 0;
    foreach (glob($dir . "/*.js") as $f) {
        $file_contents = file_get_contents($f);
        $pattern = '#(.*)for\\((.*)#U';
        $replacement = '$1for ($2';
        $foundMatches = preg_match_all($pattern, $file_contents, $matches);
        if ($foundMatches) {
            fail("Bad usage found in: " . $f);
            if ($foundMatches) {
                echo "Found matches:\n";
                foreach ($matches[0] as $line) {
                    $replaced_with = preg_replace($pattern, $replacement, $line);
                    echo "\t" . $line . " -=> " . $replaced_with . "\n";
                }
            }
            global $argv;
            if (isset($argv[1]) && $argv[1] === "fixit") {
                $file_contents = preg_replace($pattern, $replacement, $file_contents);
                changed_file($f);
                file_put_contents($f, $file_contents);
                echo "----- File automatically fixed. -----\n";
            }
        }
    }
}
Exemplo n.º 2
0
function check_for_typeof($dir)
{
    $r = 0;
    foreach (glob($dir . "/*.js") as $f) {
        $file_contents = file_get_contents($f);
        if (strpos($file_contents, 'typeof(') > -1) {
            fail("Bad usage found in: " . $f);
            $pattern = '#typeof\\(([a-zA-Z0-9\\.\\-\\_]+)\\)#smU';
            $replacement = 'typeof $1';
            if (preg_match_all($pattern, $file_contents, $matches)) {
                echo "Found matches:\n";
                foreach ($matches[0] as $line) {
                    $replaced_with = preg_replace($pattern, $replacement, $line);
                    echo "\t" . $line . " -=> " . $replaced_with . "\n";
                }
            }
            global $argv;
            if (isset($argv[1]) && $argv[1] === "fixit") {
                $file_contents = preg_replace($pattern, $replacement, $file_contents);
                changed_file($f);
                file_put_contents($f, $file_contents);
                echo "----- File automatically fixed. -----\n";
            }
        }
    }
}