php lint (syntax validation) support
authorChristian Weiske <cweiske@cweiske.de>
Fri, 13 Apr 2012 18:26:17 +0000 (20:26 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 13 Apr 2012 18:26:17 +0000 (20:26 +0200)
data/config.default.php
src/phorkie/Tool/PHPlint.php [new file with mode: 0644]

index 443328d68419981a5e63d1a8f9098f6b68517960..b07f042c76ef4a0fce229449ddba0c36202fa5af 100644 (file)
@@ -7,7 +7,8 @@ $GLOBALS['phorkie']['cfg'] = array(
     'css'     => 'http://twitter.github.com/bootstrap/assets/css/bootstrap.css',
 );
 $GLOBALS['phorkie']['tools'] = array(
-    '\\phorkie\\Tool_Xmllint'
+    '\\phorkie\\Tool_Xmllint',
+    '\\phorkie\\Tool_PHPlint',
 );
 /**
  * Array of supported file types / languages.
diff --git a/src/phorkie/Tool/PHPlint.php b/src/phorkie/Tool/PHPlint.php
new file mode 100644 (file)
index 0000000..1aa672d
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+namespace phorkie;
+
+class Tool_PHPlint
+{
+    public static $arSupportedExtensions = array(
+        'php'
+    );
+
+    public function run(File $file)
+    {
+        $fpath = $file->getPath();
+        $fpathlen = strlen($fpath);
+
+        $res = new Tool_Result();
+        $cmd = 'php -l ' . escapeshellarg($fpath) . ' 2>&1';
+        exec($cmd, $output, $retval);
+        if ($retval == 0) {
+            $res->annotations['general'][] = new Tool_Result_Line(
+                'No syntax errors detected', 'ok'
+            );
+            return $res;
+        }
+
+        $regex = '#^(.+) in ' . preg_quote($fpath) . ' on line ([0-9]+)$#';
+        for ($i = 0; $i < count($output) - 1; $i++) {
+            $line = $output[$i];
+            if (!preg_match($regex, trim($line), $matches)) {
+                throw new Exception('"php -l" does not behave as expected: ' . $line);
+            }
+            $msg     = $matches[1];
+            $linenum = $matches[2];
+            $res->annotations[$linenum][] = new Tool_Result_Line(
+                $msg, 'error'
+            );
+        }
+
+        $res->annotations['general'][] = new Tool_Result_Line(
+            'PHP code has syntax errors', 'error'
+        );
+
+        return $res;
+    }
+}
+
+?>