diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2012-04-13 20:26:17 +0200 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2012-04-13 20:26:17 +0200 |
| commit | b9ffa6b92277d3dc8a4eca77119ea098e20f36f0 (patch) | |
| tree | 06118d54a75dc7bf3d6a68812d5bfd8357ab7c6b | |
| parent | f5f35ee2ddf29469a7fda73f7c9f15faa740debf (diff) | |
| download | phorkie-b9ffa6b92277d3dc8a4eca77119ea098e20f36f0.tar.gz phorkie-b9ffa6b92277d3dc8a4eca77119ea098e20f36f0.zip | |
php lint (syntax validation) support
| -rw-r--r-- | data/config.default.php | 3 | ||||
| -rw-r--r-- | src/phorkie/Tool/PHPlint.php | 46 |
2 files changed, 48 insertions, 1 deletions
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 @@ +<?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; + } +} + +?> |
