1<?php 2/* 3 * $Id: 5b7e3fb304bb5f406c919407d6881449a70b8a28 $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://phing.info>. 20 */ 21 22require_once 'phing/Task.php'; 23 24/** 25 * ZendCodeAnalyzerTask analyze PHP source code using the ZendCodeAnalyzer included in Zend Studio 5.1 26 * 27 * Available warnings: 28 * <b>zend-error</b> - %s(line %d): %s 29 * <b>oneline-comment</b> - One-line comment ends with tag. 30 * <b>bool-assign</b> - Assignment seen where boolean expression is expected. Did you mean '==' instead of '='? 31 * <b>bool-print</b> - Print statement used when boolean expression is expected. 32 * <b>bool-array</b> - Array used when boolean expression is expected. 33 * <b>bool-object</b> - Object used when boolean expression is expected. 34 * <b>call-time-ref</b> - Call-time reference is deprecated. Define function as accepting parameter by reference instead. 35 * <b>if-if-else</b> - In if-if-else construction else relates to the closest if. Use braces to make the code clearer. 36 * <b>define-params</b> - define() requires two or three parameters. 37 * <b>define-const</b> - First parameter for define() should be string. Maybe you forgot quotes? 38 * <b>break-var</b> - Break/continue with variable is dangerous - break level can be out of scope. 39 * <b>break-depth</b> - Break/continue with depth more than current nesting level. 40 * <b>var-once</b> - Variable '%s' encountered only once. May be a typo? 41 * <b>var-arg-unused</b> - Function argument '%s' is never used. 42 * <b>var-global-unused</b> - Global variable '%s' is defined but never used. 43 * <b>var-use-before-def</b> - Variable '%s' is used before it was assigned. 44 * <b>var-use-before-def-global</b> - Global variable '%s' is used without being assigned. You are probably relying on register_globals feature of PHP. Note that this feature is off by default. 45 * <b>var-no-global</b> - PHP global variable '%s' is used as local. Maybe you wanted to define '%s' as global? 46 * <b>var-value-unused</b> - Value assigned to variable '%s' is never used 47 * <b>var-ref-notmodified</b> - Function parameter '%s' is passed by reference but never modified. Consider passing by value. 48 * <b>return-empty-val</b> - Function '%s' has both empty return and return with value. 49 * <b>return-empty-used</b> - Function '%s' has empty return but return value is used. 50 * <b>return-noref</b> - Function '%s' returns reference but the value is not assigned by reference. Maybe you meant '=&' instead of '='? 51 * <b>return-end-used</b> - Control reaches the end of function '%s'(file %s, line %d) but return value is used. 52 * <b>sprintf-miss-args</b> - Missing arguments for sprintf: format reqires %d arguments but %d are supplied. 53 * <b>sprintf-extra-args</b> - Extra arguments for sprintf: format reqires %d arguments but %d are supplied. 54 * <b>unreach-code</b> - Unreachable code in function '%s'. 55 * <b>include-var</b> - include/require with user-accessible variable can be dangerous. Consider using constant instead. 56 * <b>non-object</b> - Variable '%s' used as object, but has different type. 57 * <b>bad-escape</b> - Bad escape sequence: \%c, did you mean \\%c? 58 * <b>empty-cond</b> - Condition without a body 59 * <b>expr-unused</b> - Expression result is never used 60 * 61 * @author Knut Urdalen <knut.urdalen@gmail.com> 62 * @version $Id: 5b7e3fb304bb5f406c919407d6881449a70b8a28 $ 63 * @package phing.tasks.ext 64 */ 65class ZendCodeAnalyzerTask extends Task 66{ 67 protected $analyzerPath = ""; // Path to ZendCodeAnalyzer binary 68 protected $file = ""; // the source file (from xml attribute) 69 protected $filesets = array(); // all fileset objects assigned to this task 70 protected $counter = 0; 71 protected $disable = array(); 72 protected $enable = array(); 73 74 private $haltonwarning = false; 75 76 /** 77 * File to be analyzed 78 * 79 * @param PhingFile $file 80 */ 81 public function setFile(PhingFile $file) { 82 $this->file = $file; 83 } 84 85 /** 86 * Path to ZendCodeAnalyzer binary 87 * 88 * @param string $analyzerPath 89 */ 90 public function setAnalyzerPath($analyzerPath) { 91 $this->analyzerPath = $analyzerPath; 92 } 93 94 /** 95 * Disable warning levels. Seperate warning levels with ',' 96 * 97 * @param string $disable 98 */ 99 public function setDisable($disable) { 100 $this->disable = explode(",", $disable); 101 } 102 103 /** 104 * Enable warning levels. Seperate warning levels with ',' 105 * 106 * @param string $enable 107 */ 108 public function setEnable($enable) { 109 $this->enable = explode(",", $enable); 110 } 111 112 /** 113 * Sets the haltonwarning flag 114 * @param boolean $value 115 */ 116 public function setHaltonwarning($value) 117 { 118 $this->haltonwarning = $value; 119 } 120 121 /** 122 * Nested creator, creates a FileSet for this task 123 * 124 * @return FileSet The created fileset object 125 */ 126 public function createFileSet() { 127 $num = array_push($this->filesets, new FileSet()); 128 return $this->filesets[$num-1]; 129 } 130 131 /** 132 * Analyze against PhingFile or a FileSet 133 */ 134 public function main() { 135 if(!isset($this->analyzerPath)) { 136 throw new BuildException("Missing attribute 'analyzerPath'"); 137 } 138 139 if(!isset($this->file) and count($this->filesets) == 0) { 140 throw new BuildException("Missing either a nested fileset or attribute 'file' set"); 141 } 142 143 if($this->file instanceof PhingFile) { 144 $this->analyze($this->file->getPath()); 145 } else { // process filesets 146 $project = $this->getProject(); 147 148 foreach($this->filesets as $fs) { 149 $ds = $fs->getDirectoryScanner($project); 150 $files = $ds->getIncludedFiles(); 151 $dir = $fs->getDir($this->project)->getPath(); 152 153 foreach($files as $file) { 154 $this->analyze($dir.DIRECTORY_SEPARATOR.$file); 155 } 156 } 157 } 158 159 $this->log("Number of findings: ".$this->counter, Project::MSG_INFO); 160 } 161 162 /** 163 * Analyze file 164 * 165 * @param string $file 166 * @return void 167 */ 168 protected function analyze($file) { 169 if(file_exists($file)) { 170 if(is_readable($file)) { 171 // Construct shell command 172 $cmd = $this->analyzerPath." "; 173 174 foreach($this->enable as $enable) { // Enable warning levels 175 $cmd .= " --enable $enable "; 176 } 177 178 foreach($this->disable as $disable) { // Disable warning levels 179 $cmd .= " --disable $disable "; 180 } 181 182 $cmd .= "$file 2>&1"; 183 184 // Execute command 185 $result = shell_exec($cmd); 186 $result = explode("\n", $result); 187 188 for($i=2, $size=count($result); $i<($size-1); $i++) { 189 $this->counter++; 190 $this->log($result[$i], Project::MSG_WARN); 191 } 192 193 $total = count($result) - 3; 194 195 if ($total > 0 && $this->haltonwarning) { 196 throw new BuildException('zendcodeanalyzer detected ' . $total . ' warning' . ($total > 1 ? 's' : '') . ' in ' . $file); 197 } 198 } 199 else 200 { 201 throw new BuildException('Permission denied: '.$file); 202 } 203 } else { 204 throw new BuildException('File not found: '.$file); 205 } 206 } 207} 208