use Date_HumanDiff from PEAR to display changelog times
[phorkie.git] / src / phorkie / Tool / PHPlint.php
1 <?php
2 namespace phorkie;
3
4 class Tool_PHPlint
5 {
6     public static $arSupportedExtensions = array(
7         'php'
8     );
9
10     public function run(File $file)
11     {
12         $fpath = $file->getPath();
13         $fpathlen = strlen($fpath);
14
15         $res = new Tool_Result();
16         $cmd = 'php -l ' . escapeshellarg($fpath) . ' 2>&1';
17         exec($cmd, $output, $retval);
18         if ($retval == 0) {
19             $res->annotations['general'][] = new Tool_Result_Line(
20                 'No syntax errors detected', 'ok'
21             );
22             return $res;
23         }
24
25         $regex = '#^(.+) in ' . preg_quote($fpath) . ' on line ([0-9]+)$#';
26         for ($i = 0; $i < count($output) - 1; $i++) {
27             $line = $output[$i];
28             if (!preg_match($regex, trim($line), $matches)) {
29                 throw new Exception('"php -l" does not behave as expected: ' . $line);
30             }
31             $msg     = $matches[1];
32             $linenum = $matches[2];
33             $res->annotations[$linenum][] = new Tool_Result_Line(
34                 $msg, 'error'
35             );
36         }
37
38         $res->annotations['general'][] = new Tool_Result_Line(
39             'PHP code has syntax errors', 'error'
40         );
41
42         return $res;
43     }
44 }
45
46 ?>