Changeset 374

Show
Ignore:
Timestamp:
05/29/08 05:06:14 (7 months ago)
Author:
bender
Message:

#214 - PhpCodeSniffer support for multiple output reports and directing reports to files.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/2.4/classes/phing/tasks/ext/PhpCodeSnifferTask.php

    r347 r374  
    4747        protected $showSniffs = false; 
    4848        protected $outputFormat = 'default'; 
     49        protected $formatters   = array(); 
    4950 
    5051        /** 
     
    184185        } 
    185186 
     187  /** 
     188   * Create object for nested formatter element. 
     189   * @return CodeSniffer_FormatterElement 
     190   */ 
     191        public function createFormatter () { 
     192    $num = array_push($this->formatters,  
     193        new PhpCodeSnifferTask_FormatterElement()); 
     194    return $this->formatters[$num-1]; 
     195        } 
     196 
    186197        /** 
    187198         * Executes PHP code sniffer against PhingFile or a FileSet 
     
    190201                if(!isset($this->file) and count($this->filesets) == 0) { 
    191202                        throw new BuildException("Missing either a nested fileset or attribute 'file' set"); 
    192                 } 
     203    } 
     204 
     205    if (count($this->formatters) == 0) { 
     206      // turn legacy outputFormat attribute into formatter 
     207      $fmt = new CodeSniffer_FormatterElement(); 
     208      $fmt->setType($this->outputFormat); 
     209      $fmt->setUseFile(false); 
     210      $this->formatters[] = $fmt; 
     211    } 
    193212 
    194213                require_once 'PHP/CodeSniffer.php'; 
     
    213232                                        $fileList[] = $dir.DIRECTORY_SEPARATOR.$file; 
    214233                                } 
    215                        
     234     
    216235                        $codeSniffer->process($fileList, $this->standard, $this->sniffs, $this->noSubdirectories); 
    217                 } 
     236    } 
     237 
    218238                $this->output($codeSniffer); 
    219239        } 
     
    231251                        } 
    232252                        $this->log('The list of used sniffs (#' . count($sniffs) . '): ' . PHP_EOL . $sniffStr, Project::MSG_INFO); 
    233                 } 
    234  
    235                 switch ($this->outputFormat) { 
    236                         case 'default': 
    237                                 $this->outputCustomFormat($codeSniffer); 
    238                                 break; 
    239                         case 'xml': 
    240                                 $codeSniffer->printXMLErrorReport($this->showWarnings); 
    241                                 break; 
    242                         case 'checkstyle': 
    243                                 $codeSniffer->printCheckstyleErrorReport($this->showWarnings); 
    244                                 break; 
    245                         case 'csv': 
    246                                 $codeSniffer->printCSVErrorReport($this->showWarnings); 
    247                                 break; 
    248                         case 'report': 
    249                                 $codeSniffer->printErrorReport($this->showWarnings); 
    250                                 break; 
    251                         case 'summary': 
    252                                 $codeSniffer->printErrorReportSummary($this->showWarnings); 
    253                                 break; 
    254                         case 'doc': 
    255                                 $codeSniffer->generateDocs($this->standard, $this->sniffs); 
    256                                 break; 
    257                         default: 
    258                                 $this->log('Unknown output format "' . $this->outputFormat . '"', Project::MSG_INFO); 
    259                                 break; 
    260                 } 
    261         } 
     253    } 
     254 
     255    // process output 
     256    foreach ($this->formatters as $fe) { 
     257      $output = ''; 
     258 
     259      switch ($fe->getType()) { 
     260        case 'default': 
     261          // default format goes to logs, no buffering 
     262          $this->outputCustomFormat($codeSniffer); 
     263          $fe->setUseFile(false); 
     264          break; 
     265 
     266        case 'xml': 
     267          ob_start(); 
     268          $codeSniffer->printXMLErrorReport($this->showWarnings); 
     269          $output = ob_get_contents(); 
     270          ob_end_clean(); 
     271          break; 
     272 
     273        case 'checkstyle': 
     274          ob_start(); 
     275          $codeSniffer->printCheckstyleErrorReport($this->showWarnings); 
     276          $output = ob_get_contents(); 
     277          ob_end_clean(); 
     278          break; 
     279 
     280        case 'csv': 
     281          ob_start(); 
     282          $codeSniffer->printCSVErrorReport($this->showWarnings); 
     283          $output = ob_get_contents(); 
     284          ob_end_clean(); 
     285          break; 
     286 
     287        case 'report': 
     288          ob_start(); 
     289          $codeSniffer->printErrorReport($this->showWarnings); 
     290          $output = ob_get_contents(); 
     291          ob_end_clean(); 
     292          break; 
     293 
     294        case 'summary': 
     295          ob_start(); 
     296          $codeSniffer->printErrorReportSummary($this->showWarnings); 
     297          $output = ob_get_contents(); 
     298          ob_end_clean(); 
     299          break; 
     300 
     301        case 'doc': 
     302          ob_start(); 
     303          $codeSniffer->generateDocs($this->standard, $this->sniffs); 
     304          $output = ob_get_contents(); 
     305          ob_end_clean(); 
     306          break; 
     307 
     308        default: 
     309          $this->log('Unknown output format "' . $fe->getType() . '"', Project::MSG_INFO); 
     310          continue; //skip to next formatter in list 
     311          break; 
     312      } //end switch 
     313                         
     314                        if (!$fe->getUseFile()) { 
     315        // output raw to console 
     316        echo $output; 
     317 
     318                        } else { 
     319                          // write to file 
     320                          $outputFile = $fe->getOutfile(); 
     321                          $check = file_put_contents($outputFile, $output); 
     322        if (is_bool($check) && !$check) { 
     323          throw new BuildException('Error writing output to ' . $outputFile); 
     324        } 
     325                        } 
     326    } //end foreach 
     327        } //end output 
    262328 
    263329        /** 
     
    324390        } 
    325391 
    326 
     392} //end phpCodeSnifferTask 
     393 
     394class PhpCodeSnifferTask_FormatterElement extends DataType { 
     395 
     396  /** 
     397   * Type of output to generate 
     398   * @var string 
     399   */ 
     400  protected $type      = ""; 
     401 
     402  /** 
     403   * Output to file? 
     404   * @var bool 
     405   */ 
     406  protected $useFile   = true; 
     407 
     408  /** 
     409   * Output file. 
     410   * @var string 
     411   */ 
     412  protected $outfile   = ""; 
     413 
     414  /** 
     415   * Validate config. 
     416   */ 
     417  public function parsingComplete () { 
     418                if(empty($this->type)) { 
     419                        throw new BuildException("Format missing required 'type' attribute."); 
     420    } 
     421    if ($useFile && empty($this->outfile)) { 
     422      throw new BuildException("Format requres 'outfile' attribute when 'useFile' is true."); 
     423    }  
     424 
     425  } 
     426 
     427  public function setType ($type)  { 
     428    $this->type = $type; 
     429  } 
     430 
     431  public function getType () { 
     432    return $this->type; 
     433  } 
     434 
     435  public function setUseFile ($useFile) { 
     436    $this->useFile = $useFile; 
     437  } 
     438   
     439  public function getUseFile () { 
     440    return $this->useFile; 
     441  } 
     442   
     443  public function setOutfile ($outfile) { 
     444    $this->outfile = $outfile; 
     445  } 
     446   
     447  public function getOutfile () { 
     448    return $this->outfile; 
     449  } 
     450   
     451} //end FormatterElement 
  • branches/2.4/docs/phing_guide/book/chapters/appendixes/AppendixC-OptionalTasks.html

    • Property svn:executable deleted
    r352 r374  
    883883      <td>The output format. The <em>default</em> format is specified in the task itself. Additionally the formats of CodeSniffer can be choosen: <em>xml</em>, <em>checkstyle</em>, <em>csv</em>, <em>report</em> and <em>summary</em>. A special case is the format <em>doc</em> which generates the documentation of the standard.</td> 
    884884      <td>default</td> 
    885       <td>No</td> 
     885      <td>No<br/>Ignored if nested <em>formatter</em> elements are supplied.</td> 
    886886    </tr> 
    887887    <tr> 
     
    986986    </table> 
    987987  </li> 
     988  <li>formatter 
     989  <p>The results of the tests can be printed in different formats. Output will always be sent to a file, unless you set the <em>usefile</em> attribute to <em>false.</em></p> 
     990    <h3>Attributes</h3> 
     991    <table> 
     992      <thead> 
     993        <tr> 
     994          <th>Name</th> 
     995          <th>Type</th> 
     996          <th>Description</th> 
     997          <th>Default</th> 
     998          <th>Required</th> 
     999        </tr> 
     1000      </thead> 
     1001      <tbody> 
     1002        <tr> 
     1003          <td>type</td> 
     1004          <td>String</td> 
     1005          <td>The output format. Accepts the the same values as the <em>outputFormat</em> attribute (<em>default</em>, <em>xml</em>, <em>checkstyle</em>, <em>csv</em>, <em>report</em>, <em>summary</em> &amp; <em>doc</em>).</td> 
     1006          <td>n/a</td> 
     1007          <td>Yes</td> 
     1008        </tr> 
     1009        <tr> 
     1010          <td>usefile</td> 
     1011          <td>Boolean</td> 
     1012          <td>Boolean that determines whether output should be sent to a file.</td> 
     1013          <td>true</td> 
     1014          <td>No</td> 
     1015        </tr> 
     1016        <tr> 
     1017          <td>outfile</td> 
     1018          <td>String</td> 
     1019          <td>Path to write output file to.</td> 
     1020          <td>n/a</td> 
     1021          <td>Yes, if <em>usefile</em> is <em>true</em>.</td> 
     1022        </tr> 
     1023      </tbody> 
     1024    </table> 
     1025  </li> 
    9881026</ul> 
    9891027<h3>Examples</h3> 
     
    9961034&lt;/phpcodesniffer&gt; 
    9971035</pre> 
    998 <p>Checks all matching files in the directory <em>dir</em> with a custom external standard, sets the <em>zend_ca_path</em> configuration which may be required by one of the sniffs, prints a list of used sniffs and prints the <em>default</em> report with warnings.</p> 
     1036<p>Checks all matching files in the directory <em>dir</em> with a custom external standard, sets the <em>zend_ca_path</em> configuration which may be required by one of the sniffs, prints a list of used sniffs and prints the <em>default</em> report with warnings and the <em>checkstyle</em> report to <em>/path/to/checkstyle.xml</em>.</p> 
    9991037<pre>&lt;phpunit 
    10001038  standard=&quot;/path/to/external-standard&quot; 
     
    10041042    &lt;include name=&quot;**/*.php&quot;/&gt; 
    10051043  &lt;/fileset&gt; 
    1006   &lt;config name=&quot;zend_ca_path&quot; value=&quot;/path/to/ZendStudio/bin/ZendCodeAnalyzer&quot;&gt; 
     1044  &lt;config name=&quot;zend_ca_path&quot; value=&quot;/path/to/ZendStudio/bin/ZendCodeAnalyzer&quot;/&gt; 
     1045  &lt;formatter type=&quot;default&quot; usefile=&quot;false&quot;/&gt; 
     1046  &lt;formatter type=&quot;checkstyle&quot; outfile=&quot;/path/to/checkstyle.xml&quot;/&gt; 
    10071047&lt;/phpunit&gt; 
    10081048</pre> 
  • branches/2.4/docs/phing_guide/book/chapters/appendixes/AppendixD-CoreTypes.html

    • Property svn:executable deleted
  • branches/2.4/docs/phing_guide/book/chapters/appendixes/AppendixE-ProjectComponents.html

    • Property svn:executable deleted
  • branches/2.4/docs/phing_guide/book/chapters/appendixes/AppendixF-FileFormats.html

    • Property svn:executable deleted