123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- require_once 'phing/Task.php';
- class ZendCodeAnalyzerTask extends Task
- {
- protected $analyzerPath = "";
- protected $file = "";
- protected $filesets = array();
- protected $counter = 0;
- protected $disable = array();
- protected $enable = array();
- private $haltonwarning = false;
-
- public function setFile(PhingFile $file) {
- $this->file = $file;
- }
-
- public function setAnalyzerPath($analyzerPath) {
- $this->analyzerPath = $analyzerPath;
- }
-
- public function setDisable($disable) {
- $this->disable = explode(",", $disable);
- }
-
- public function setEnable($enable) {
- $this->enable = explode(",", $enable);
- }
-
- function setHaltonwarning($value)
- {
- $this->haltonwarning = $value;
- }
-
-
- function createFileSet() {
- $num = array_push($this->filesets, new FileSet());
- return $this->filesets[$num-1];
- }
-
- public function main() {
- if(!isset($this->analyzerPath)) {
- throw new BuildException("Missing attribute 'analyzerPath'");
- }
-
- if(!isset($this->file) and count($this->filesets) == 0) {
- throw new BuildException("Missing either a nested fileset or attribute 'file' set");
- }
- if($this->file instanceof PhingFile) {
- $this->analyze($this->file->getPath());
- } else {
- $project = $this->getProject();
-
- foreach($this->filesets as $fs) {
- $ds = $fs->getDirectoryScanner($project);
- $files = $ds->getIncludedFiles();
- $dir = $fs->getDir($this->project)->getPath();
-
- foreach($files as $file) {
- $this->analyze($dir.DIRECTORY_SEPARATOR.$file);
- }
- }
- }
-
- $this->log("Number of findings: ".$this->counter, Project::MSG_INFO);
- }
-
- protected function analyze($file) {
- if(file_exists($file)) {
- if(is_readable($file)) {
-
- $cmd = $this->analyzerPath." ";
-
- foreach($this->enable as $enable) {
- $cmd .= " --enable $enable ";
- }
-
- foreach($this->disable as $disable) {
- $cmd .= " --disable $disable ";
- }
-
- $cmd .= "$file 2>&1";
-
- $result = shell_exec($cmd);
- $result = explode("\n", $result);
-
- for($i=2, $size=count($result); $i<($size-1); $i++) {
- $this->counter++;
- $this->log($result[$i], Project::MSG_WARN);
- }
-
- $total = count($result) - 3;
-
- if ($total > 0 && $this->haltonwarning) {
- throw new BuildException('zendcodeanalyzer detected ' . $total . ' warning' . ($total > 1 ? 's' : '') . ' in ' . $file);
- }
- }
- else
- {
- throw new BuildException('Permission denied: '.$file);
- }
- } else {
- throw new BuildException('File not found: '.$file);
- }
- }
- }
|