From b9ffa6b92277d3dc8a4eca77119ea098e20f36f0 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 13 Apr 2012 20:26:17 +0200 Subject: [PATCH 1/1] php lint (syntax validation) support --- data/config.default.php | 3 ++- src/phorkie/Tool/PHPlint.php | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/phorkie/Tool/PHPlint.php diff --git a/data/config.default.php b/data/config.default.php index 443328d..b07f042 100644 --- a/data/config.default.php +++ b/data/config.default.php @@ -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 index 0000000..1aa672d --- /dev/null +++ b/src/phorkie/Tool/PHPlint.php @@ -0,0 +1,46 @@ +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; + } +} + +?> -- 2.30.2