/** * 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 = $this->getFilesToParse(); $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']++; } if ($this->cache) { require_once 'phing/tasks/ext/phpcs/Reports_PhingRemoveFromCache.php'; PHP_CodeSniffer_Reports_PhingRemoveFromCache::setCache($this->cache); // add a fake report to remove from cache $_SERVER['argv'][] = '--report-phingRemoveFromCache='; $_SERVER['argc']++; } $codeSniffer->process($fileList, $this->standards, $this->sniffs, $this->noSubdirectories); $_SERVER['argv'] = array(); $_SERVER['argc'] = 0; if ($this->cache) { PHP_CodeSniffer_Reports_PhingRemoveFromCache::setCache(null); $this->cache->commit(); } $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); }