123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- require_once 'phing/Task.php';
- require_once 'phing/util/DataStore.php';
- require_once 'phing/system/io/FileWriter.php';
- class PhpLintTask extends Task {
- protected $file;
- protected $filesets = array();
- protected $errorProperty;
- protected $haltOnFailure = false;
- protected $hasErrors = false;
- protected $badFiles = array();
- protected $interpreter = '';
-
- protected $logLevel = Project::MSG_INFO;
-
- protected $cache = null;
-
- protected $tofile = null;
-
- protected $deprecatedAsError = false;
-
- public function __construct() {
- $this->setInterpreter(Phing::getProperty('php.interpreter'));
- }
-
- public function setInterpreter($sPhp) {
- $this->Interpreter = $sPhp;
- }
-
- public function setHaltOnFailure($aValue) {
- $this->haltOnFailure = $aValue;
- }
-
- public function setFile(PhingFile $file) {
- $this->file = $file;
- }
-
- public function setErrorproperty($propname)
- {
- $this->errorProperty = $propname;
- }
-
-
- public function setCacheFile(PhingFile $file)
- {
- $this->cache = new DataStore($file);
- }
-
- public function setToFile(PhingFile $tofile)
- {
- $this->tofile = $tofile;
- }
-
- function createFileSet() {
- $num = array_push($this->filesets, new FileSet());
- return $this->filesets[$num-1];
- }
-
-
- public function setLevel($level)
- {
- switch ($level)
- {
- case "error": $this->logLevel = Project::MSG_ERR; break;
- case "warning": $this->logLevel = Project::MSG_WARN; break;
- case "info": $this->logLevel = Project::MSG_INFO; break;
- case "verbose": $this->logLevel = Project::MSG_VERBOSE; break;
- case "debug": $this->logLevel = Project::MSG_DEBUG; break;
- }
- }
-
-
- public function setDeprecatedAsError($deprecatedAsError)
- {
- $this->deprecatedAsError = $deprecatedAsError;
- }
-
- public function main() {
- if(!isset($this->file) and count($this->filesets) == 0) {
- throw new BuildException("Missing either a nested fileset or attribute 'file' set");
- }
- if($this->file instanceof PhingFile) {
- $this->lint($this->file->getPath());
- } else {
- $project = $this->getProject();
- foreach($this->filesets as $fs) {
- $ds = $fs->getDirectoryScanner($project);
- $files = $ds->getIncludedFiles();
- $dir = $fs->getDir($this->project)->getPath();
- foreach($files as $file) {
- $this->lint($dir.DIRECTORY_SEPARATOR.$file);
- }
- }
- }
-
-
- if ($this->tofile) {
- $writer = new FileWriter($this->tofile);
-
- foreach ($this->badFiles as $file => $msg) {
- $writer->write($file . "=" . $msg . PHP_EOL);
- }
-
- $writer->close();
- }
- if ($this->haltOnFailure && $this->hasErrors) throw new BuildException('Syntax error(s) in PHP files: '.implode(', ',$this->badFiles));
- }
-
- protected function lint($file) {
- $command = $this->Interpreter == ''
- ? 'php'
- : $this->Interpreter;
- $command .= ' -l ';
- if(file_exists($file)) {
- if(is_readable($file)) {
- if ($this->cache)
- {
- $lastmtime = $this->cache->get($file);
-
- if ($lastmtime >= filemtime($file))
- {
- $this->log("Not linting '" . $file . "' due to cache", Project::MSG_DEBUG);
- return false;
- }
- }
-
- $messages = array();
- exec($command.'"'.$file.'" 2>&1', $messages);
- if(!preg_match('/^No syntax errors detected/', $messages[0])) {
- if (count($messages) < 2 ) {
- $this->log("Could not parse file", Project::MSG_ERR);
- } else {
- for ($i = 0; $i < count($messages) - 1; $i++) {
- $message = $messages[$i];
- if (trim($message) == '') {
- continue;
- }
-
- if (!preg_match('/^(.*)Deprecated:/', $message) || $this->deprecatedAsError) {
- $this->log($message, $this->logLevel);
-
- if ($this->errorProperty) {
- $this->project->setProperty($this->errorProperty, $message);
- }
-
- if (!isset($this->badFiles[$file])) {
- $this->badFiles[$file] = $message;
- }
-
- $this->hasErrors = true;
- }
- }
- }
- } else {
- $this->log($file.': No syntax errors detected', $this->logLevel);
- }
-
- if ($this->cache)
- {
- $this->cache->put($file, filemtime($file));
- }
- } else {
- throw new BuildException('Permission denied: '.$file);
- }
- } else {
- throw new BuildException('File not found: '.$file);
- }
- }
- }
|