ZendCodeAnalyzerTask.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*
  3. * $Id: ZendCodeAnalyzerTask.php 905 2010-10-05 16:28:03Z mrook $
  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. require_once 'phing/Task.php';
  22. /**
  23. * ZendCodeAnalyzerTask analyze PHP source code using the ZendCodeAnalyzer included in Zend Studio 5.1
  24. *
  25. * Available warnings:
  26. * <b>zend-error</b> - %s(line %d): %s
  27. * <b>oneline-comment</b> - One-line comment ends with tag.
  28. * <b>bool-assign</b> - Assignment seen where boolean expression is expected. Did you mean '==' instead of '='?
  29. * <b>bool-print</b> - Print statement used when boolean expression is expected.
  30. * <b>bool-array</b> - Array used when boolean expression is expected.
  31. * <b>bool-object</b> - Object used when boolean expression is expected.
  32. * <b>call-time-ref</b> - Call-time reference is deprecated. Define function as accepting parameter by reference instead.
  33. * <b>if-if-else</b> - In if-if-else construction else relates to the closest if. Use braces to make the code clearer.
  34. * <b>define-params</b> - define() requires two or three parameters.
  35. * <b>define-const</b> - First parameter for define() should be string. Maybe you forgot quotes?
  36. * <b>break-var</b> - Break/continue with variable is dangerous - break level can be out of scope.
  37. * <b>break-depth</b> - Break/continue with depth more than current nesting level.
  38. * <b>var-once</b> - Variable '%s' encountered only once. May be a typo?
  39. * <b>var-arg-unused</b> - Function argument '%s' is never used.
  40. * <b>var-global-unused</b> - Global variable '%s' is defined but never used.
  41. * <b>var-use-before-def</b> - Variable '%s' is used before it was assigned.
  42. * <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.
  43. * <b>var-no-global</b> - PHP global variable '%s' is used as local. Maybe you wanted to define '%s' as global?
  44. * <b>var-value-unused</b> - Value assigned to variable '%s' is never used
  45. * <b>var-ref-notmodified</b> - Function parameter '%s' is passed by reference but never modified. Consider passing by value.
  46. * <b>return-empty-val</b> - Function '%s' has both empty return and return with value.
  47. * <b>return-empty-used</b> - Function '%s' has empty return but return value is used.
  48. * <b>return-noref</b> - Function '%s' returns reference but the value is not assigned by reference. Maybe you meant '=&' instead of '='?
  49. * <b>return-end-used</b> - Control reaches the end of function '%s'(file %s, line %d) but return value is used.
  50. * <b>sprintf-miss-args</b> - Missing arguments for sprintf: format reqires %d arguments but %d are supplied.
  51. * <b>sprintf-extra-args</b> - Extra arguments for sprintf: format reqires %d arguments but %d are supplied.
  52. * <b>unreach-code</b> - Unreachable code in function '%s'.
  53. * <b>include-var</b> - include/require with user-accessible variable can be dangerous. Consider using constant instead.
  54. * <b>non-object</b> - Variable '%s' used as object, but has different type.
  55. * <b>bad-escape</b> - Bad escape sequence: \%c, did you mean \\%c?
  56. * <b>empty-cond</b> - Condition without a body
  57. * <b>expr-unused</b> - Expression result is never used
  58. *
  59. * @author Knut Urdalen <knut.urdalen@gmail.com>
  60. * @version $Id: ZendCodeAnalyzerTask.php 905 2010-10-05 16:28:03Z mrook $
  61. * @package phing.tasks.ext
  62. */
  63. class ZendCodeAnalyzerTask extends Task
  64. {
  65. protected $analyzerPath = ""; // Path to ZendCodeAnalyzer binary
  66. protected $file = ""; // the source file (from xml attribute)
  67. protected $filesets = array(); // all fileset objects assigned to this task
  68. protected $counter = 0;
  69. protected $disable = array();
  70. protected $enable = array();
  71. private $haltonwarning = false;
  72. /**
  73. * File to be analyzed
  74. *
  75. * @param PhingFile $file
  76. */
  77. public function setFile(PhingFile $file) {
  78. $this->file = $file;
  79. }
  80. /**
  81. * Path to ZendCodeAnalyzer binary
  82. *
  83. * @param string $analyzerPath
  84. */
  85. public function setAnalyzerPath($analyzerPath) {
  86. $this->analyzerPath = $analyzerPath;
  87. }
  88. /**
  89. * Disable warning levels. Seperate warning levels with ','
  90. *
  91. * @param string $disable
  92. */
  93. public function setDisable($disable) {
  94. $this->disable = explode(",", $disable);
  95. }
  96. /**
  97. * Enable warning levels. Seperate warning levels with ','
  98. *
  99. * @param string $enable
  100. */
  101. public function setEnable($enable) {
  102. $this->enable = explode(",", $enable);
  103. }
  104. /**
  105. * Sets the haltonwarning flag
  106. * @param boolean $value
  107. */
  108. function setHaltonwarning($value)
  109. {
  110. $this->haltonwarning = $value;
  111. }
  112. /**
  113. * Nested creator, creates a FileSet for this task
  114. *
  115. * @return FileSet The created fileset object
  116. */
  117. function createFileSet() {
  118. $num = array_push($this->filesets, new FileSet());
  119. return $this->filesets[$num-1];
  120. }
  121. /**
  122. * Analyze against PhingFile or a FileSet
  123. */
  124. public function main() {
  125. if(!isset($this->analyzerPath)) {
  126. throw new BuildException("Missing attribute 'analyzerPath'");
  127. }
  128. if(!isset($this->file) and count($this->filesets) == 0) {
  129. throw new BuildException("Missing either a nested fileset or attribute 'file' set");
  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. * Analyze file
  148. *
  149. * @param string $file
  150. * @return void
  151. */
  152. protected function analyze($file) {
  153. if(file_exists($file)) {
  154. if(is_readable($file)) {
  155. // Construct shell command
  156. $cmd = $this->analyzerPath." ";
  157. foreach($this->enable as $enable) { // Enable warning levels
  158. $cmd .= " --enable $enable ";
  159. }
  160. foreach($this->disable as $disable) { // Disable warning levels
  161. $cmd .= " --disable $disable ";
  162. }
  163. $cmd .= "$file 2>&1";
  164. // Execute command
  165. $result = shell_exec($cmd);
  166. $result = explode("\n", $result);
  167. for($i=2, $size=count($result); $i<($size-1); $i++) {
  168. $this->counter++;
  169. $this->log($result[$i], Project::MSG_WARN);
  170. }
  171. $total = count($result) - 3;
  172. if ($total > 0 && $this->haltonwarning) {
  173. throw new BuildException('zendcodeanalyzer detected ' . $total . ' warning' . ($total > 1 ? 's' : '') . ' in ' . $file);
  174. }
  175. }
  176. else
  177. {
  178. throw new BuildException('Permission denied: '.$file);
  179. }
  180. } else {
  181. throw new BuildException('File not found: '.$file);
  182. }
  183. }
  184. }