Changeset 226

Show
Ignore:
Timestamp:
08/28/07 02:12:30 (1 year ago)
Author:
hans
Message:

#110 - Added an errorproperty attribute to phplint task.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/classes/phing/tasks/ext/PhpLintTask.php

    r180 r226  
    11<?php 
    22/* 
    3  *  $Id$ 
     3 *     $Id$ 
    44 * 
    55 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
     
    2525 * A PHP lint task. Checking syntax of one or more PHP source file. 
    2626 * 
    27  * @author  Knut Urdalen <knut.urdalen@telio.no> 
    28  * @author  Stefan Priebsch <stefan.priebsch@e-novative.de> 
    29  * @package  phing.tasks.ext 
     27 * @author      Knut Urdalen <knut.urdalen@telio.no> 
     28 * @author      Stefan Priebsch <stefan.priebsch@e-novative.de> 
     29 * @package    phing.tasks.ext 
    3030 */ 
    3131class PhpLintTask extends Task { 
    3232 
    33   protected $file;  // the source file (from xml attribute) 
    34   protected $filesets = array(); // all fileset objects assigned to this task 
     33       protected $file;        // the source file (from xml attribute) 
     34       protected $filesets = array(); // all fileset objects assigned to this task 
    3535 
    36   protected $haltOnFailure = false; 
    37   protected $hasErrors = false; 
     36        protected $errorProperty; 
     37        protected $haltOnFailure = false; 
     38        protected $hasErrors = false; 
    3839 
    39   /** 
    40   * The haltonfailure property 
    41   * @param boolean $aValue 
    42   */ 
    43   public function setHaltOnFailure($aValue) { 
    44     $this->haltOnFailure = $aValue; 
    45  
     40       /** 
     41        * The haltonfailure property 
     42        * @param boolean $aValue 
     43        */ 
     44       public function setHaltOnFailure($aValue) { 
     45               $this->haltOnFailure = $aValue; 
     46       
    4647 
    47   /** 
    48    * File to be performed syntax check on 
    49    * @param PhingFile $file 
    50    */ 
    51   public function setFile(PhingFile $file) { 
    52     $this->file = $file; 
    53   } 
    54    
    55   /** 
    56    * Nested creator, creates a FileSet for this task 
    57    * 
    58    * @return FileSet The created fileset object 
    59    */ 
    60   function createFileSet() { 
    61     $num = array_push($this->filesets, new FileSet()); 
    62     return $this->filesets[$num-1]; 
    63   } 
     48        /** 
     49         * File to be performed syntax check on 
     50         * @param PhingFile $file 
     51         */ 
     52        public function setFile(PhingFile $file) { 
     53                $this->file = $file; 
     54        } 
    6455 
    65   /** 
    66    * Execute lint check against PhingFile or a FileSet 
    67    */ 
    68   public function main() { 
    69     if(!isset($this->file) and count($this->filesets) == 0) { 
    70       throw new BuildException("Missing either a nested fileset or attribute 'file' set"); 
    71     } 
     56        /** 
     57         * Set an property name in which to put any errors. 
     58         * @param string $propname  
     59         */ 
     60        public function setErrorproperty($propname) 
     61        { 
     62                $this->errorProperty = $propname; 
     63        } 
    7264 
    73     if($this->file instanceof PhingFile) { 
    74       $this->lint($this->file->getPath()); 
    75     } else { // process filesets 
    76       $project = $this->getProject(); 
    77       foreach($this->filesets as $fs) { 
    78               $ds = $fs->getDirectoryScanner($project); 
    79               $files = $ds->getIncludedFiles(); 
    80               $dir = $fs->getDir($this->project)->getPath(); 
    81               foreach($files as $file) { 
    82                 $this->lint($dir.DIRECTORY_SEPARATOR.$file); 
    83               } 
    84       } 
    85     } 
     65        /** 
     66         * Nested creator, creates a FileSet for this task 
     67         * 
     68         * @return FileSet The created fileset object 
     69         */ 
     70        function createFileSet() { 
     71                $num = array_push($this->filesets, new FileSet()); 
     72                return $this->filesets[$num-1]; 
     73        } 
    8674 
    87     if ($this->haltOnFailure && $this->hasErrors) throw new BuildException('Syntax error(s) in PHP files'); 
    88   } 
     75        /** 
     76         * Execute lint check against PhingFile or a FileSet 
     77         */ 
     78        public function main() { 
     79                if(!isset($this->file) and count($this->filesets) == 0) { 
     80                        throw new BuildException("Missing either a nested fileset or attribute 'file' set"); 
     81                } 
    8982 
    90   /** 
    91    * Performs the actual syntax check 
    92    * 
    93    * @param string $file 
    94    * @return void 
    95    */ 
    96   protected function lint($file) { 
    97     $command = 'php -l '; 
    98     if(file_exists($file)) { 
    99       if(is_readable($file)) { 
    100         $message = array(); 
    101         exec($command.$file, $message); 
    102         if(!preg_match('/^No syntax errors detected/', $message[0])) {     
    103           if (count($messages) > 1) 
    104           { 
    105             $this->log($message[1], Project::MSG_ERR); 
    106           } 
    107           else 
    108           { 
    109             $this->log("Could not parse file", Project::MSG_ERR); 
    110           } 
    111            
    112       $this->hasErrors = true; 
    113         } else { 
    114           $this->log($file.': No syntax errors detected', Project::MSG_INFO); 
     83                if($this->file instanceof PhingFile) { 
     84                        $this->lint($this->file->getPath()); 
     85                } else { // process filesets 
     86                        $project = $this->getProject(); 
     87                        foreach($this->filesets as $fs) { 
     88                                $ds = $fs->getDirectoryScanner($project); 
     89                                $files = $ds->getIncludedFiles(); 
     90                                $dir = $fs->getDir($this->project)->getPath(); 
     91                                foreach($files as $file) { 
     92                                        $this->lint($dir.DIRECTORY_SEPARATOR.$file); 
     93                                } 
     94                        } 
     95                } 
     96 
     97                if ($this->haltOnFailure && $this->hasErrors) throw new BuildException('Syntax error(s) in PHP files'); 
    11598        } 
    116       } else { 
    117         throw new BuildException('Permission denied: '.$file); 
    118       } 
    119     } else { 
    120       throw new BuildException('File not found: '.$file); 
    121     } 
    122   } 
     99 
     100        /** 
     101         * Performs the actual syntax check 
     102         * 
     103         * @param string $file 
     104         * @return void 
     105         */ 
     106        protected function lint($file) { 
     107                $command = 'php -l '; 
     108                if(file_exists($file)) { 
     109                        if(is_readable($file)) { 
     110                                $messages = array(); 
     111                                exec($command.$file, $messages); 
     112                                if(!preg_match('/^No syntax errors detected/', $messages[0])) { 
     113                                        if (count($messages) > 1) { 
     114                                                if ($this->errorProperty) { 
     115                                                        $this->project->setProperty($this->errorProperty, $messages[1]); 
     116                                                } 
     117                                                $this->log($messages[1], Project::MSG_ERR); 
     118                                        } else { 
     119                                                $this->log("Could not parse file", Project::MSG_ERR); 
     120                                        } 
     121                                                 
     122                                        $this->hasErrors = true; 
     123                                         
     124                                } else { 
     125                                        $this->log($file.': No syntax errors detected', Project::MSG_INFO); 
     126                                } 
     127                        } else { 
     128                                throw new BuildException('Permission denied: '.$file); 
     129                        } 
     130                } else { 
     131                        throw new BuildException('File not found: '.$file); 
     132                } 
     133        } 
    123134} 
    124135 
  • trunk/docs/phing_guide/book/chapters/appendixes/AppendixC-OptionalTasks.html

    r222 r226  
    956956      <td> Stop the build process if the linting process. </td> 
    957957      <td>false</td> 
     958      <td>No</td> 
     959    </tr> 
     960    <tr> 
     961          <td>errorproperty</td> 
     962      <td>String</td> 
     963      <td>The name of a property that will be set to contain the error string (if any).</td> 
     964      <td>n/a</td> 
    958965      <td>No</td> 
    959966    </tr>