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 /src | |
| parent | f5f35ee2ddf29469a7fda73f7c9f15faa740debf (diff) | |
| download | phorkie-b9ffa6b92277d3dc8a4eca77119ea098e20f36f0.tar.gz phorkie-b9ffa6b92277d3dc8a4eca77119ea098e20f36f0.zip | |
php lint (syntax validation) support
Diffstat (limited to 'src')
| -rw-r--r-- | src/phorkie/Tool/PHPlint.php | 46 |
1 files changed, 46 insertions, 0 deletions
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; + } +} + +?> |
