/** * Returns a list of paths to XML standard files for all sniffs in a standard. * * Any sniffs that do not have an XML standard file are obviously not included * in the returned array. If documentation is only being generated for some * sniffs (ie. $this->_sniffs is not empty) then all others sniffs will * be filtered from the results as well. * * @return array(string) */ protected function getStandardFiles() { $phpcs = new PHP_CodeSniffer(); $sniffs = $phpcs->processRuleset($this->_standard); $standardFiles = array(); foreach ($sniffs as $sniff) { if (empty($this->_sniffs) === false) { // We are limiting the docs to certain sniffs only, so filter // out any unwanted sniffs. $sniffName = substr($sniff, strrpos($sniff, '/') + 1); $sniffName = substr($sniffName, 0, -9); if (in_array($sniffName, $this->_sniffs) === false) { continue; } } $standardFile = str_replace(DIRECTORY_SEPARATOR . 'Sniffs' . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR . 'Docs' . DIRECTORY_SEPARATOR, $sniff); $standardFile = str_replace('Sniff.php', 'Standard.xml', $standardFile); if (is_file($standardFile) === true) { $standardFiles[] = $standardFile; } } //end foreach return $standardFiles; }
private function process($files, $interactive = true) { $standards = 'Webasyst'; if (PHP_CodeSniffer::isInstalledStandard($standards) === false) { $this->tracef('WARNING: %s standard not found, will used PSR2. Some rules are differ', $standards); $standards = 'PSR2'; } if (is_array($standards) === false) { $standards = array($standards); } $phpcs = new PHP_CodeSniffer(0, 4, 'UTF-8', $interactive); // Set file extensions if they were specified. Otherwise, // let PHP_CodeSniffer decide on the defaults. if (true) { $extensions = array('php', 'js', 'css'); $phpcs->setAllowedFileExtensions($extensions); } if (is_array($files) === false) { $files = array($files); } // Reset the members. // Ensure this option is enabled or else line endings will not always // be detected properly for files created on a Mac with the /r line ending. ini_set('auto_detect_line_endings', true); $sniffs = array(); foreach ($standards as $standard) { $installed = $phpcs->getInstalledStandardPath($standard); if ($installed !== null) { $standard = $installed; } else { if (is_dir($standard) === true && is_file(realpath($standard . '/ruleset.xml')) === true) { $standard = realpath($standard . '/ruleset.xml'); } } $sniffs = array_merge($sniffs, $phpcs->processRuleset($standard)); } //end foreach $sniffRestrictions = array(); $phpcs->registerSniffs($sniffs, $sniffRestrictions); $phpcs->populateTokenListeners(); // The SVN pre-commit calls process() to init the sniffs // and ruleset so there may not be any files to process. // But this has to come after that initial setup. //define('PHP_CODESNIFFER_IN_TESTS',true); $_SERVER['argc'] = 0; $errors_count = 0; foreach ($files as $file) { $phpcsFile = $phpcs->processFile($file); // Show progress information. if ($phpcsFile !== null) { $count = $phpcsFile->getErrorCount() + $phpcsFile->getWarningCount(); if (!$interactive && $count) { $report = array('ERROR' => $phpcsFile->getErrors(), 'WARNING' => $phpcsFile->getWarnings()); $this->tracef("\nFILE: %s", str_replace($this->path . '/', '', $file)); $this->trace(str_repeat('-', 80)); foreach ($report as $type => $errors) { foreach ($errors as $line => $line_errors) { foreach ($line_errors as $column => $errors) { foreach ($errors as $error) { $this->tracef('%4d | %s | %s', $line, $type, $error['message']); } } } } $this->trace(str_repeat('-', 80)); } $errors_count += $count; } } return $errors_count; }