| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: ZendCodeAnalyzerTask.php 325 2007-12-20 15:44:58Z hans $
|
|
|
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 |
|
|
|
22 |
require_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 |
* @package phing.tasks.ext
|
|
|
63 |
*/
|
|
|
64 |
class ZendCodeAnalyzerTask extends Task {
|
|
|
65 |
|
|
|
66 |
protected $analyzerPath = ""; // Path to ZendCodeAnalyzer binary
|
|
|
67 |
protected $file = ""; // the source file (from xml attribute)
|
|
|
68 |
protected $filesets = array(); // all fileset objects assigned to this task
|
|
|
69 |
protected $warnings = array();
|
|
|
70 |
protected $counter = 0;
|
|
|
71 |
protected $disable = array();
|
|
|
72 |
protected $enable = array();
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* File to be analyzed
|
|
|
76 |
*
|
|
|
77 |
* @param PhingFile $file
|
|
|
78 |
*/
|
|
|
79 |
public function setFile(PhingFile $file) {
|
|
|
80 |
$this->file = $file;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Path to ZendCodeAnalyzer binary
|
|
|
85 |
*
|
|
|
86 |
* @param string $analyzerPath
|
|
|
87 |
*/
|
|
|
88 |
public function setAnalyzerPath($analyzerPath) {
|
|
|
89 |
$this->analyzerPath = $analyzerPath;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Disable warning levels. Seperate warning levels with ','
|
|
|
94 |
*
|
|
|
95 |
* @param string $disable
|
|
|
96 |
*/
|
|
|
97 |
public function setDisable($disable) {
|
|
|
98 |
$this->disable = explode(",", $disable);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Enable warning levels. Seperate warning levels with ','
|
|
|
103 |
*
|
|
|
104 |
* @param string $enable
|
|
|
105 |
*/
|
|
|
106 |
public function setEnable($enable) {
|
|
|
107 |
$this->enable = explode(",", $enable);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Nested creator, creates a FileSet for this task
|
|
|
112 |
*
|
|
|
113 |
* @return FileSet The created fileset object
|
|
|
114 |
*/
|
|
|
115 |
function createFileSet() {
|
|
|
116 |
$num = array_push($this->filesets, new FileSet());
|
|
|
117 |
return $this->filesets[$num-1];
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Analyze against PhingFile or a FileSet
|
|
|
122 |
*/
|
|
|
123 |
public function main() {
|
|
|
124 |
if(!isset($this->analyzerPath)) {
|
|
|
125 |
throw new BuildException("Missing attribute 'analyzerPath'");
|
|
|
126 |
}
|
|
|
127 |
if(!isset($this->file) and count($this->filesets) == 0) {
|
|
|
128 |
throw new BuildException("Missing either a nested fileset or attribute 'file' set");
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
if($this->file instanceof PhingFile) {
|
|
|
132 |
$this->analyze($this->file->getPath());
|
|
|
133 |
} else { // process filesets
|
|
|
134 |
$project = $this->getProject();
|
|
|
135 |
foreach($this->filesets as $fs) {
|
|
|
136 |
$ds = $fs->getDirectoryScanner($project);
|
|
|
137 |
$files = $ds->getIncludedFiles();
|
|
|
138 |
$dir = $fs->getDir($this->project)->getPath();
|
|
|
139 |
foreach($files as $file) {
|
|
|
140 |
$this->analyze($dir.DIRECTORY_SEPARATOR.$file);
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
$this->log("Number of findings: ".$this->counter, Project::MSG_INFO);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Analyze file
|
|
|
149 |
*
|
|
|
150 |
* @param string $file
|
|
|
151 |
* @return void
|
|
|
152 |
*/
|
|
|
153 |
protected function analyze($file) {
|
|
|
154 |
if(file_exists($file)) {
|
|
|
155 |
if(is_readable($file)) {
|
|
|
156 |
|
|
|
157 |
// Construct shell command
|
|
|
158 |
$cmd = $this->analyzerPath." ";
|
|
|
159 |
foreach($this->enable as $enable) { // Enable warning levels
|
|
|
160 |
$cmd .= " --enable $enable ";
|
|
|
161 |
}
|
|
|
162 |
foreach($this->disable as $disable) { // Disable warning levels
|
|
|
163 |
$cmd .= " --disable $disable ";
|
|
|
164 |
}
|
|
|
165 |
$cmd .= "$file 2>&1";
|
|
|
166 |
|
|
|
167 |
// Execute command
|
|
|
168 |
$result = shell_exec($cmd);
|
|
|
169 |
$result = explode("\n", $result);
|
|
|
170 |
for($i=2, $size=count($result); $i<($size-1); $i++) {
|
|
|
171 |
$this->counter++;
|
|
|
172 |
$this->log($result[$i], Project::MSG_WARN);
|
|
|
173 |
}
|
|
|
174 |
} else {
|
|
|
175 |
throw new BuildException('Permission denied: '.$file);
|
|
|
176 |
}
|
|
|
177 |
} else {
|
|
|
178 |
throw new BuildException('File not found: '.$file);
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
|