/**
  * Executes PHP code sniffer against PhingFile or a FileSet
  */
 public function main()
 {
     if (!class_exists('PHP_CodeSniffer')) {
         @(include_once 'PHP/CodeSniffer.php');
         if (!class_exists('PHP_CodeSniffer')) {
             throw new BuildException("This task requires the PHP_CodeSniffer package installed and available on the include path", $this->getLocation());
         }
     }
     /**
      * Determine PHP_CodeSniffer version number
      */
     if (!$this->skipversioncheck) {
         if (defined('PHP_CodeSniffer::VERSION')) {
             preg_match('/\\d\\.\\d\\.\\d/', PHP_CodeSniffer::VERSION, $version);
         } else {
             preg_match('/\\d\\.\\d\\.\\d/', shell_exec('phpcs --version'), $version);
         }
         if (version_compare($version[0], '1.2.2') < 0) {
             throw new BuildException('PhpCodeSnifferTask requires PHP_CodeSniffer version >= 1.2.2', $this->getLocation());
         }
     }
     if (!isset($this->file) and count($this->filesets) == 0) {
         throw new BuildException("Missing either a nested fileset or attribute 'file' set");
     }
     if (count($this->formatters) == 0) {
         // turn legacy format attribute into formatter
         $fmt = new PhpCodeSnifferTask_FormatterElement();
         $fmt->setType($this->format);
         $fmt->setUseFile(false);
         $this->formatters[] = $fmt;
     }
     $fileList = array();
     if (!isset($this->file)) {
         $project = $this->getProject();
         foreach ($this->filesets as $fs) {
             $ds = $fs->getDirectoryScanner($project);
             $files = $ds->getIncludedFiles();
             $dir = $fs->getDir($this->project)->getAbsolutePath();
             foreach ($files as $file) {
                 $fileList[] = $dir . DIRECTORY_SEPARATOR . $file;
             }
         }
     } else {
         $fileList[] = $this->file->getPath();
     }
     $cwd = getcwd();
     // Save command line arguments because it confuses PHPCS (version 1.3.0)
     $oldArgs = $_SERVER['argv'];
     $_SERVER['argv'] = array();
     $_SERVER['argc'] = 0;
     include_once 'phing/tasks/ext/phpcs/PhpCodeSnifferTask_Wrapper.php';
     $codeSniffer = new PhpCodeSnifferTask_Wrapper($this->verbosity, $this->tabWidth, $this->encoding);
     $codeSniffer->setAllowedFileExtensions($this->allowedFileExtensions);
     if ($this->allowedTypes) {
         PhpCodeSnifferTask_Wrapper::$allowedTypes = $this->allowedTypes;
     }
     if (is_array($this->ignorePatterns)) {
         $codeSniffer->setIgnorePatterns($this->ignorePatterns);
     }
     foreach ($this->configData as $configData) {
         $codeSniffer->setConfigData($configData->getName(), $configData->getValue(), true);
     }
     /*
      * Verifying if standard is installed only after setting config data.
      * Custom standard paths could be provided via installed_paths config parameter.
      */
     foreach ($this->standards as $standard) {
         if (PHP_CodeSniffer::isInstalledStandard($standard) === false) {
             // They didn't select a valid coding standard, so help them
             // out by letting them know which standards are installed.
             $installedStandards = PHP_CodeSniffer::getInstalledStandards();
             $numStandards = count($installedStandards);
             $errMsg = '';
             if ($numStandards === 0) {
                 $errMsg = 'No coding standards are installed.';
             } else {
                 $lastStandard = array_pop($installedStandards);
                 if ($numStandards === 1) {
                     $errMsg = 'The only coding standard installed is ' . $lastStandard;
                 } else {
                     $standardList = implode(', ', $installedStandards);
                     $standardList .= ' and ' . $lastStandard;
                     $errMsg = 'The installed coding standards are ' . $standardList;
                 }
             }
             throw new BuildException('ERROR: the "' . $standard . '" coding standard is not installed. ' . $errMsg, $this->getLocation());
         }
     }
     if (!$this->showWarnings) {
         $codeSniffer->cli->warningSeverity = 0;
     }
     // nasty integration hack
     $values = $codeSniffer->cli->getDefaults();
     $_SERVER['argv'] = array('t');
     $_SERVER['argc'] = 1;
     foreach ($this->formatters as $fe) {
         $output = $fe->getUseFile() ? $fe->getOutFile() : null;
         $_SERVER['argv'][] = '--report-' . $fe->getType() . '=' . $output;
         $_SERVER['argc']++;
     }
     $codeSniffer->process($fileList, $this->standards, $this->sniffs, $this->noSubdirectories);
     $_SERVER['argv'] = array();
     $_SERVER['argc'] = 0;
     $this->printErrorReport($codeSniffer);
     // generate the documentation
     if ($this->docGenerator !== '' && $this->docFile !== null) {
         ob_start();
         $codeSniffer->generateDocs($this->standards, $this->sniffs, $this->docGenerator);
         $output = ob_get_contents();
         ob_end_clean();
         // write to file
         $outputFile = $this->docFile->getPath();
         $check = file_put_contents($outputFile, $output);
         if (is_bool($check) && !$check) {
             throw new BuildException('Error writing doc to ' . $outputFile);
         }
     } elseif ($this->docGenerator !== '' && $this->docFile === null) {
         $codeSniffer->generateDocs($this->standards, $this->sniffs, $this->docGenerator);
     }
     if ($this->haltonerror && $codeSniffer->reporting->totalErrors > 0) {
         throw new BuildException('phpcodesniffer detected ' . $codeSniffer->reporting->totalErrors . ' error' . ($codeSniffer->reporting->totalErrors > 1 ? 's' : ''));
     }
     if ($this->haltonwarning && $codeSniffer->reporting->totalWarnings > 0) {
         throw new BuildException('phpcodesniffer detected ' . $codeSniffer->reporting->totalWarnings . ' warning' . ($codeSniffer->reporting->totalWarnings > 1 ? 's' : ''));
     }
     $_SERVER['argv'] = $oldArgs;
     $_SERVER['argc'] = count($oldArgs);
     chdir($cwd);
 }
 /**
  * Executes PHP code sniffer against PhingFile or a FileSet
  */
 public function main()
 {
     if (!class_exists('PHP_CodeSniffer')) {
         include_once 'PHP/CodeSniffer.php';
     }
     /**
      * Determine PHP_CodeSniffer version number
      */
     if (!$this->skipVersionCheck) {
         preg_match('/\\d\\.\\d\\.\\d/', shell_exec('phpcs --version'), $version);
         if (version_compare($version[0], '1.2.2') < 0) {
             throw new BuildException('PhpCodeSnifferTask requires PHP_CodeSniffer version >= 1.2.2', $this->getLocation());
         }
     }
     if (!isset($this->file) and count($this->filesets) == 0) {
         throw new BuildException("Missing either a nested fileset or attribute 'file' set");
     }
     if (count($this->formatters) == 0) {
         // turn legacy format attribute into formatter
         $fmt = new PhpCodeSnifferTask_FormatterElement();
         $fmt->setType($this->format);
         $fmt->setUseFile(false);
         $this->formatters[] = $fmt;
     }
     if (!isset($this->file)) {
         $fileList = array();
         $project = $this->getProject();
         foreach ($this->filesets as $fs) {
             $ds = $fs->getDirectoryScanner($project);
             $files = $ds->getIncludedFiles();
             $dir = $fs->getDir($this->project)->getAbsolutePath();
             foreach ($files as $file) {
                 $fileList[] = $dir . DIRECTORY_SEPARATOR . $file;
             }
         }
     }
     $cwd = getcwd();
     // Save command line arguments because it confuses PHPCS (version 1.3.0)
     $oldArgs = $_SERVER['argv'];
     $_SERVER['argv'] = array();
     $codeSniffer = new PHP_CodeSniffer($this->verbosity, $this->tabWidth);
     $codeSniffer->setAllowedFileExtensions($this->allowedFileExtensions);
     if (is_array($this->ignorePatterns)) {
         $codeSniffer->setIgnorePatterns($this->ignorePatterns);
     }
     foreach ($this->configData as $configData) {
         $codeSniffer->setConfigData($configData->getName(), $configData->getValue(), true);
     }
     if ($this->file instanceof PhingFile) {
         $codeSniffer->process($this->file->getPath(), $this->standard, $this->sniffs, $this->noSubdirectories);
     } else {
         $codeSniffer->process($fileList, $this->standard, $this->sniffs, $this->noSubdirectories);
     }
     // Restore command line arguments
     $_SERVER['argv'] = $oldArgs;
     chdir($cwd);
     $report = $this->printErrorReport($codeSniffer);
     // generate the documentation
     if ($this->docGenerator !== '' && $this->docFile !== null) {
         ob_start();
         $codeSniffer->generateDocs($this->standard, $this->sniffs, $this->docGenerator);
         $output = ob_get_contents();
         ob_end_clean();
         // write to file
         $outputFile = $this->docFile->getPath();
         $check = file_put_contents($outputFile, $output);
         if (is_bool($check) && !$check) {
             throw new BuildException('Error writing doc to ' . $outputFile);
         }
     } elseif ($this->docGenerator !== '' && $this->docFile === null) {
         $codeSniffer->generateDocs($this->standard, $this->sniffs, $this->docGenerator);
     }
     if ($this->haltonerror && $report['totals']['errors'] > 0) {
         throw new BuildException('phpcodesniffer detected ' . $report['totals']['errors'] . ' error' . ($report['totals']['errors'] > 1 ? 's' : ''));
     }
     if ($this->haltonwarning && $report['totals']['warnings'] > 0) {
         throw new BuildException('phpcodesniffer detected ' . $report['totals']['warnings'] . ' warning' . ($report['totals']['warnings'] > 1 ? 's' : ''));
     }
 }
 /**
  * Executes PHP code sniffer against PhingFile or a FileSet
  */
 public function main()
 {
     if (!class_exists('PHP_CodeSniffer')) {
         include_once 'PHP/CodeSniffer.php';
     }
     if (!isset($this->file) and count($this->filesets) == 0) {
         throw new BuildException("Missing either a nested fileset or attribute 'file' set");
     }
     if (count($this->formatters) == 0) {
         // turn legacy format attribute into formatter
         $fmt = new PhpCodeSnifferTask_FormatterElement();
         $fmt->setType($this->format);
         $fmt->setUseFile(false);
         $this->formatters[] = $fmt;
     }
     if (!isset($this->file)) {
         $fileList = array();
         $project = $this->getProject();
         foreach ($this->filesets as $fs) {
             $ds = $fs->getDirectoryScanner($project);
             $files = $ds->getIncludedFiles();
             $dir = $fs->getDir($this->project)->getAbsolutePath();
             foreach ($files as $file) {
                 $fileList[] = $dir . DIRECTORY_SEPARATOR . $file;
             }
         }
     }
     $codeSniffer = new PHP_CodeSniffer($this->verbosity, $this->tabWidth);
     $codeSniffer->setAllowedFileExtensions($this->allowedFileExtensions);
     if (is_array($this->ignorePatterns)) {
         $codeSniffer->setIgnorePatterns($this->ignorePatterns);
     }
     foreach ($this->configData as $configData) {
         $codeSniffer->setConfigData($configData->getName(), $configData->getValue(), true);
     }
     if ($this->file instanceof PhingFile) {
         $codeSniffer->process($this->file->getPath(), $this->standard, $this->sniffs, $this->noSubdirectories);
     } else {
         $codeSniffer->process($fileList, $this->standard, $this->sniffs, $this->noSubdirectories);
     }
     $report = $this->printErrorReport($codeSniffer);
     // generate the documentation
     if ($this->docGenerator !== '' && $this->docFile !== null) {
         ob_start();
         $codeSniffer->generateDocs($this->standard, $this->sniffs, $this->docGenerator);
         $output = ob_get_contents();
         ob_end_clean();
         // write to file
         $outputFile = $this->docFile->getPath();
         $check = file_put_contents($outputFile, $output);
         if (is_bool($check) && !$check) {
             throw new BuildException('Error writing doc to ' . $outputFile);
         }
     } elseif ($this->docGenerator !== '' && $this->docFile === null) {
         $codeSniffer->generateDocs($this->standard, $this->sniffs, $this->docGenerator);
     }
     if ($this->haltonerror && $report['totals']['errors'] > 0) {
         throw new BuildException('phpcodesniffer detected ' . $report['totals']['errors'] . ' error' . ($report['totals']['errors'] > 1 ? 's' : ''));
     }
     if ($this->haltonwarning && $report['totals']['warnings'] > 0) {
         throw new BuildException('phpcodesniffer detected ' . $report['totals']['warnings'] . ' warning' . ($report['totals']['warnings'] > 1 ? 's' : ''));
     }
 }
Example #4
0
 /**
  * Executes PHP code sniffer against PhingFile or a FileSet
  */
 public function main()
 {
     if (!isset($this->file) and count($this->filesets) == 0) {
         throw new BuildException("Missing either a nested fileset or attribute 'file' set");
     }
     if (count($this->formatters) == 0) {
         // turn legacy format attribute into formatter
         $fmt = new PhpCodeSnifferTask_FormatterElement();
         $fmt->setType($this->format);
         $fmt->setUseFile(false);
         $this->formatters[] = $fmt;
     }
     if (!isset($this->file)) {
         $fileList = array();
         $project = $this->getProject();
         foreach ($this->filesets as $fs) {
             $ds = $fs->getDirectoryScanner($project);
             $files = $ds->getIncludedFiles();
             $dir = $fs->getDir($this->project)->getAbsolutePath();
             foreach ($files as $file) {
                 $fileList[] = $dir . DIRECTORY_SEPARATOR . $file;
             }
         }
     }
     /* save current directory */
     $old_cwd = getcwd();
     require_once 'PHP/CodeSniffer.php';
     $codeSniffer = new PHP_CodeSniffer($this->verbosity, $this->tabWidth);
     $codeSniffer->setAllowedFileExtensions($this->allowedFileExtensions);
     if (is_array($this->ignorePatterns)) {
         $codeSniffer->setIgnorePatterns($this->ignorePatterns);
     }
     foreach ($this->configData as $configData) {
         $codeSniffer->setConfigData($configData->getName(), $configData->getValue(), true);
     }
     if ($this->file instanceof PhingFile) {
         $codeSniffer->process($this->file->getPath(), $this->standard, $this->sniffs, $this->noSubdirectories);
     } else {
         $codeSniffer->process($fileList, $this->standard, $this->sniffs, $this->noSubdirectories);
     }
     $this->output($codeSniffer);
     $report = $codeSniffer->prepareErrorReport(true);
     if ($this->haltonerror && $report['totals']['errors'] > 0) {
         throw new BuildException('phpcodesniffer detected ' . $report['totals']['errors'] . ' error' . ($report['totals']['errors'] > 1 ? 's' : ''));
     }
     if ($this->haltonwarning && $report['totals']['warnings'] > 0) {
         throw new BuildException('phpcodesniffer detected ' . $report['totals']['warnings'] . ' warning' . ($report['totals']['warnings'] > 1 ? 's' : ''));
     }
     /* reset current directory */
     chdir($old_cwd);
 }