|
| 1 | +#!/usr/bin/php |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * This file is part of the Kdyby (http://www.kdyby.org) |
| 6 | + * |
| 7 | + * Copyright (c) 2008 Filip Procházka (filip@prochazka.su) |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the file license.txt that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +$parseOptions = function () use ($_SERVER) { |
| 13 | + $options = array('quiet' => false, 'files' => array()); |
| 14 | + foreach (array_keys(getopt('qh', array('quiet', 'help'))) as $arg) { |
| 15 | + switch ($arg) { |
| 16 | + case 'q': |
| 17 | + case 'quiet': |
| 18 | + $options['quiet'] = true; |
| 19 | + break; |
| 20 | + |
| 21 | + case 'h': |
| 22 | + case 'help': |
| 23 | + default: |
| 24 | + echo <<<HELP |
| 25 | +usage: lint [-q] [path] |
| 26 | +
|
| 27 | +options: |
| 28 | + -q, --quiet: disable verbose output |
| 29 | + -h, --help: display this help screen |
| 30 | +
|
| 31 | +HELP; |
| 32 | + exit(0); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + stream_set_blocking(STDIN, FALSE); |
| 37 | + while ($line = trim(fgets(STDIN))) { |
| 38 | + $options['files'][] = $_SERVER['PWD'] . '/' . $line; |
| 39 | + } |
| 40 | + |
| 41 | + if (empty($options['files']) && $_SERVER['argc'] > 1) { |
| 42 | + foreach ($_SERVER['argv'] as $i => $arg) { |
| 43 | + if (substr($arg, 0, 1) === '-' || $i === 0) continue; |
| 44 | + $options['files'][] = $arg; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (empty($options['files'])) $options['files'][] = $_SERVER['PWD']; |
| 49 | + |
| 50 | + foreach ($options['files'] as $i => $file) { |
| 51 | + if (($options['files'][$i] = realpath($file)) !== false) continue; |
| 52 | + echo "$file is not a file or directory.\n"; |
| 53 | + exit(1); |
| 54 | + } |
| 55 | + |
| 56 | + return $options; |
| 57 | +}; |
| 58 | + |
| 59 | +$echo = function () use (&$context) { |
| 60 | + if ($context['quiet']) return; |
| 61 | + foreach (func_get_args() as $arg) echo $arg; |
| 62 | +}; |
| 63 | + |
| 64 | +$lintFile = function ($path) use (&$echo, &$context) { |
| 65 | + if (substr($path, -4) != '.php') return; |
| 66 | + |
| 67 | + if ($context['filesCount'] % 63 == 0) { |
| 68 | + $echo("\n"); |
| 69 | + } |
| 70 | + |
| 71 | + exec("php -l " . escapeshellarg($path) . " 2>&1 1> /dev/null", $output, $code); |
| 72 | + if ($code) { |
| 73 | + $context['errors'][] = implode($output); |
| 74 | + $echo('E'); |
| 75 | + } else { |
| 76 | + $echo('.'); |
| 77 | + } |
| 78 | + |
| 79 | + $context['filesCount']++; |
| 80 | +}; |
| 81 | + |
| 82 | +$check = function ($path) use (&$check, &$lintFile, &$context) { |
| 83 | + if (!is_dir($path)) return $lintFile($path); |
| 84 | + foreach (scandir($path) as $item) { |
| 85 | + if ($item == '.' || $item == '..') continue; |
| 86 | + $check(rtrim($path, '/') . '/' . $item); |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | + |
| 91 | +$context = $parseOptions(); |
| 92 | +$context['filesCount'] = 0; |
| 93 | +$context['errors'] = array(); |
| 94 | +foreach ($context['files'] as $file) $check($file); |
| 95 | +if ($context['errors']) { |
| 96 | + $echo("\n\n", implode($context['errors'])); |
| 97 | +} |
| 98 | + |
| 99 | +$echo( |
| 100 | + "\n\n", ($context['errors'] ? 'FAILED' : 'OK'), |
| 101 | + ' (', $context['filesCount'], " files checked, ", count($context['errors']), " errors)\n" |
| 102 | +); |
| 103 | +exit($context['errors'] ? 1 : 0); |
0 commit comments