本文整理汇总了PHP中PHP_CodeSniffer::getSniffs方法的典型用法代码示例。如果您正苦于以下问题:PHP PHP_CodeSniffer::getSniffs方法的具体用法?PHP PHP_CodeSniffer::getSniffs怎么用?PHP PHP_CodeSniffer::getSniffs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHP_CodeSniffer
的用法示例。
在下文中一共展示了PHP_CodeSniffer::getSniffs方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getStandardFiles
/**
* 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 string[]
*/
protected function getStandardFiles()
{
$phpcs = new PHP_CodeSniffer();
$phpcs->process(array(), $this->_standard);
$sniffs = $phpcs->getSniffs();
$standardFiles = array();
foreach ($sniffs as $className => $sniffClass) {
$object = new ReflectionObject($sniffClass);
$sniff = $object->getFilename();
if (empty($this->_sniffs) === false) {
// We are limiting the docs to certain sniffs only, so filter
// out any unwanted sniffs.
$parts = explode('_', $className);
$sniffName = $parts[0] . '.' . $parts[2] . '.' . substr($parts[3], 0, -5);
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;
}
示例2: printErrorReport
/**
* Prints the error report.
*
* @param PHP_CodeSniffer $phpcs The PHP_CodeSniffer object containing
* the errors.
*/
protected function printErrorReport($phpcs)
{
if ($this->showSniffs) {
$sniffs = $phpcs->getSniffs();
$sniffStr = '';
foreach ($sniffs as $sniff) {
if (is_string($sniff)) {
$sniffStr .= '- ' . $sniff . PHP_EOL;
} else {
$sniffStr .= '- ' . get_class($sniff) . PHP_EOL;
}
}
$this->log('The list of used sniffs (#' . count($sniffs) . '): ' . PHP_EOL . $sniffStr, Project::MSG_INFO);
}
// process output
$reporting = $phpcs->reporting;
foreach ($this->formatters as $fe) {
$reportFile = null;
if ($fe->getUseFile()) {
$reportFile = $fe->getOutfile();
//ob_start();
}
// Crude check, but they broke backwards compatibility
// with a minor version release.
if (PHP_CodeSniffer::VERSION >= '2.2.0') {
$cliValues = array('colors' => false);
$reporting->printReport($fe->getType(), $this->showSources, $cliValues, $reportFile, $this->reportWidth);
} else {
$reporting->printReport($fe->getType(), $this->showSources, $reportFile, $this->reportWidth);
}
// reporting class uses ob_end_flush(), but we don't want
// an output if we use a file
if ($fe->getUseFile()) {
//ob_end_clean();
}
}
}
示例3: explainStandard
/**
* Prints a report showing the sniffs contained in a standard.
*
* @param string $standard The standard to validate.
*
* @return void
*/
public function explainStandard($standard)
{
$phpcs = new PHP_CodeSniffer();
$phpcs->process(array(), $standard);
$sniffs = $phpcs->getSniffs();
$sniffs = array_keys($sniffs);
sort($sniffs);
ob_start();
$lastStandard = '';
$lastCount = '';
$sniffCount = count($sniffs);
$sniffs[] = '___';
echo PHP_EOL . "The {$standard} standard contains {$sniffCount} sniffs" . PHP_EOL;
ob_start();
foreach ($sniffs as $sniff) {
$parts = explode('_', str_replace('\\', '_', $sniff));
if ($lastStandard === '') {
$lastStandard = $parts[0];
}
if ($parts[0] !== $lastStandard) {
$sniffList = ob_get_contents();
ob_end_clean();
echo PHP_EOL . $lastStandard . ' (' . $lastCount . ' sniffs)' . PHP_EOL;
echo str_repeat('-', strlen($lastStandard . $lastCount) + 10);
echo PHP_EOL;
echo $sniffList;
$lastStandard = $parts[0];
$lastCount = 0;
ob_start();
}
echo ' ' . $parts[0] . '.' . $parts[2] . '.' . substr($parts[3], 0, -5) . PHP_EOL;
$lastCount++;
}
//end foreach
ob_end_clean();
}
示例4: printErrorReport
/**
* Prints the error report.
*
* @param PHP_CodeSniffer $phpcs The PHP_CodeSniffer object containing
* the errors.
*/
protected function printErrorReport($phpcs)
{
if ($this->showSniffs) {
$sniffs = $phpcs->getSniffs();
$sniffStr = '';
foreach ($sniffs as $sniff) {
if (is_string($sniff)) {
$sniffStr .= '- ' . $sniff . PHP_EOL;
} else {
$sniffStr .= '- ' . get_class($sniff) . PHP_EOL;
}
}
$this->log('The list of used sniffs (#' . count($sniffs) . '): ' . PHP_EOL . $sniffStr, Project::MSG_INFO);
}
// process output
$reporting = $phpcs->reporting;
foreach ($this->formatters as $fe) {
$reportFile = null;
if ($fe->getUseFile()) {
$reportFile = $fe->getOutfile();
//ob_start();
}
$reporting->printReport($fe->getType(), $this->showSources, $reportFile, $this->reportWidth);
// reporting class uses ob_end_flush(), but we don't want
// an output if we use a file
if ($fe->getUseFile()) {
//ob_end_clean();
}
}
}
示例5: start
/**
* Starts the stack traversal and tells listeners when tokens are found.
*
* @param string $contents The contents to parse. If NULL, the content
* is taken from the file system.
*
* @return void
*/
public function start($contents = null)
{
$this->_errors = array();
$this->_warnings = array();
$this->_errorCount = 0;
$this->_warningCount = 0;
$this->_fixableCount = 0;
// Reset the ignored lines because lines numbers may have changed
// if we are fixing this file.
self::$_ignoredLines = array();
try {
$this->eolChar = self::detectLineEndings($this->_file, $contents);
} catch (PHP_CodeSniffer_Exception $e) {
$this->addWarning($e->getMessage(), null, 'Internal.DetectLineEndings');
return;
}
// If this is standard input, see if a filename was passed in as well.
// This is done by including: phpcs_input_file: [file path]
// as the first line of content.
if ($this->_file === 'STDIN' && $contents !== null) {
if (substr($contents, 0, 17) === 'phpcs_input_file:') {
$eolPos = strpos($contents, $this->eolChar);
$filename = trim(substr($contents, 17, $eolPos - 17));
$contents = substr($contents, $eolPos + strlen($this->eolChar));
$this->_file = $filename;
}
}
$this->_parse($contents);
$this->fixer->startFile($this);
if (PHP_CODESNIFFER_VERBOSITY > 2) {
echo "\t*** START TOKEN PROCESSING ***" . PHP_EOL;
}
$foundCode = false;
$listeners = $this->phpcs->getSniffs();
$listenerIgnoreTo = array();
$inTests = defined('PHP_CODESNIFFER_IN_TESTS');
// Foreach of the listeners that have registered to listen for this
// token, get them to process it.
foreach ($this->_tokens as $stackPtr => $token) {
// Check for ignored lines.
if ($token['code'] === T_COMMENT || $token['code'] === T_DOC_COMMENT_TAG || $inTests === true && $token['code'] === T_INLINE_HTML) {
if (strpos($token['content'], '@codingStandards') !== false) {
if (strpos($token['content'], '@codingStandardsIgnoreFile') !== false) {
// Ignoring the whole file, just a little late.
$this->_errors = array();
$this->_warnings = array();
$this->_errorCount = 0;
$this->_warningCount = 0;
$this->_fixableCount = 0;
return;
} else {
if (strpos($token['content'], '@codingStandardsChangeSetting') !== false) {
$start = strpos($token['content'], '@codingStandardsChangeSetting');
$comment = substr($token['content'], $start + 30);
$parts = explode(' ', $comment);
$sniffParts = explode('.', $parts[0]);
$listenerClass = $sniffParts[0] . '_Sniffs_' . $sniffParts[1] . '_' . $sniffParts[2] . 'Sniff';
$this->phpcs->setSniffProperty($listenerClass, $parts[1], $parts[2]);
}
}
//end if
}
//end if
}
//end if
if (PHP_CODESNIFFER_VERBOSITY > 2) {
$type = $token['type'];
$content = PHP_CodeSniffer::prepareForOutput($token['content']);
echo "\t\tProcess token {$stackPtr}: {$type} => {$content}" . PHP_EOL;
}
if ($token['code'] !== T_INLINE_HTML) {
$foundCode = true;
}
if (isset($this->_listeners[$token['code']]) === false) {
continue;
}
foreach ($this->_listeners[$token['code']] as $listenerData) {
if (isset($this->_ignoredListeners[$listenerData['class']]) === true || isset($listenerIgnoreTo[$listenerData['class']]) === true && $listenerIgnoreTo[$listenerData['class']] > $stackPtr) {
// This sniff is ignoring past this token, or the whole file.
continue;
}
// Make sure this sniff supports the tokenizer
// we are currently using.
$class = $listenerData['class'];
if (isset($listenerData['tokenizers'][$this->tokenizerType]) === false) {
continue;
}
// If the file path matches one of our ignore patterns, skip it.
// While there is support for a type of each pattern
// (absolute or relative) we don't actually support it here.
foreach ($listenerData['ignore'] as $pattern) {
// We assume a / directory separator, as do the exclude rules
//.........这里部分代码省略.........
示例6: printErrorReport
/**
* Prints the error report.
*
* @param PHP_CodeSniffer $phpcs The PHP_CodeSniffer object containing
* the errors.
*
* @return int The number of error and warning messages shown.
*/
protected function printErrorReport($phpcs)
{
if ($this->showSniffs) {
$sniffs = $phpcs->getSniffs();
$sniffStr = '';
foreach ($sniffs as $sniff) {
$sniffStr .= '- ' . $sniff . PHP_EOL;
}
$this->log('The list of used sniffs (#' . count($sniffs) . '): ' . PHP_EOL . $sniffStr, Project::MSG_INFO);
}
$filesViolations = $phpcs->getFilesErrors();
$reporting = new PHP_CodeSniffer_Reporting();
$report = $reporting->prepare($filesViolations, $this->showWarnings);
// process output
foreach ($this->formatters as $fe) {
switch ($fe->getType()) {
case 'default':
// default format goes to logs, no buffering
$this->outputCustomFormat($report);
$fe->setUseFile(false);
break;
default:
$reportFile = null;
if ($fe->getUseFile()) {
$reportFile = $fe->getOutfile();
ob_start();
}
// Determine number of parameters required to
// ensure backwards compatibility
$rm = new ReflectionMethod('PHP_CodeSniffer_Reporting', 'printReport');
if ($rm->getNumberOfParameters() == 5) {
$reporting->printReport($fe->getType(), $filesViolations, $this->showSources, $reportFile, $this->reportWidth);
} else {
$reporting->printReport($fe->getType(), $filesViolations, $this->showWarnings, $this->showSources, $reportFile, $this->reportWidth);
}
// reporting class uses ob_end_flush(), but we don't want
// an output if we use a file
if ($fe->getUseFile()) {
ob_end_clean();
}
break;
}
}
return $report;
}
示例7: output
/**
* Outputs the results
* @param PHP_CodeSniffer $codeSniffer
*/
protected function output($codeSniffer)
{
if ($this->showSniffs) {
$sniffs = $codeSniffer->getSniffs();
$sniffStr = '';
foreach ($sniffs as $sniff) {
$sniffStr .= '- ' . $sniff . PHP_EOL;
}
$this->log('The list of used sniffs (#' . count($sniffs) . '): ' . PHP_EOL . $sniffStr, Project::MSG_INFO);
}
switch ($this->outputFormat) {
case 'default':
$this->outputCustomFormat($codeSniffer);
break;
case 'xml':
$codeSniffer->printXMLErrorReport($this->showWarnings);
break;
case 'checkstyle':
$codeSniffer->printCheckstyleErrorReport($this->showWarnings);
break;
case 'csv':
$codeSniffer->printCSVErrorReport($this->showWarnings);
break;
case 'report':
$codeSniffer->printErrorReport($this->showWarnings);
break;
case 'summary':
$codeSniffer->printErrorReportSummary($this->showWarnings);
break;
case 'doc':
$codeSniffer->generateDocs($this->standard, $this->sniffs);
break;
default:
$this->log('Unknown output format "' . $this->outputFormat . '"', Project::MSG_INFO);
break;
}
}
示例8: sniffList
public function sniffList()
{
if (!class_exists('PHP_CodeSniffer')) {
$composerInstall = dirname(dirname(dirname(__FILE__))) . '/vendor/squizlabs/php_codesniffer/CodeSniffer.php';
if (file_exists($composerInstall)) {
require_once $composerInstall;
} else {
require_once 'PHP/CodeSniffer.php';
}
}
$phpcs = new PHP_CodeSniffer();
$phpcs->process(array(), $this->codingStandardName);
$sniffs = $phpcs->getSniffs();
$sniffs = array_keys($sniffs);
sort($sniffs);
$sniffList = [];
foreach ($sniffs as $sniff) {
$parts = explode('_', str_replace('\\', '_', $sniff));
$sniffList[] = "{$parts[0]}.{$parts[2]}." . substr($parts[3], 0, -5);
}
return $sniffList;
}
示例9: printErrorReport
/**
* Prints the error report.
*
* @param PHP_CodeSniffer $phpcs The PHP_CodeSniffer object containing
* the errors.
*
* @return int The number of error and warning messages shown.
*/
protected function printErrorReport($phpcs)
{
if ($this->showSniffs) {
$sniffs = $phpcs->getSniffs();
$sniffStr = '';
foreach ($sniffs as $sniff) {
$sniffStr .= '- ' . $sniff . PHP_EOL;
}
$this->log('The list of used sniffs (#' . count($sniffs) . '): ' . PHP_EOL . $sniffStr, Project::MSG_INFO);
}
$filesViolations = $phpcs->getFilesErrors();
$reporting = new PHP_CodeSniffer_Reporting();
$report = $reporting->prepare($filesViolations, $this->showWarnings);
// process output
foreach ($this->formatters as $fe) {
switch ($fe->getType()) {
case 'default':
// default format goes to logs, no buffering
$this->outputCustomFormat($report);
$fe->setUseFile(false);
break;
default:
$reportFile = '';
if ($fe->getUseFile()) {
$reportFile = $fe->getOutfile()->getPath();
ob_start();
}
$reporting->printReport($fe->getType(), $filesViolations, $this->showWarnings, $this->showSources, $reportFile, $this->reportWidth);
// reporting class uses ob_end_flush(), but we don't want
// an output if we use a file
if ($fe->getUseFile()) {
ob_end_clean();
}
break;
}
}
return $report;
}
示例10: output
/**
* Outputs the results
* @param PHP_CodeSniffer $codeSniffer
*/
protected function output($codeSniffer)
{
if ($this->showSniffs) {
$sniffs = $codeSniffer->getSniffs();
$sniffStr = '';
foreach ($sniffs as $sniff) {
$sniffStr .= '- ' . $sniff . PHP_EOL;
}
$this->log('The list of used sniffs (#' . count($sniffs) . '): ' . PHP_EOL . $sniffStr, Project::MSG_INFO);
}
// process output
foreach ($this->formatters as $fe) {
$output = '';
switch ($fe->getType()) {
case 'default':
// default format goes to logs, no buffering
$this->outputCustomFormat($codeSniffer);
$fe->setUseFile(false);
break;
case 'xml':
ob_start();
$codeSniffer->printXMLErrorReport($this->showWarnings);
$output = ob_get_contents();
ob_end_clean();
break;
case 'checkstyle':
ob_start();
$codeSniffer->printCheckstyleErrorReport($this->showWarnings);
$output = ob_get_contents();
ob_end_clean();
break;
case 'csv':
ob_start();
$codeSniffer->printCSVErrorReport($this->showWarnings);
$output = ob_get_contents();
ob_end_clean();
break;
case 'report':
ob_start();
$codeSniffer->printErrorReport($this->showWarnings);
$output = ob_get_contents();
ob_end_clean();
break;
case 'summary':
ob_start();
$codeSniffer->printErrorReportSummary($this->showWarnings);
$output = ob_get_contents();
ob_end_clean();
break;
case 'doc':
ob_start();
$codeSniffer->generateDocs($this->standard, $this->sniffs);
$output = ob_get_contents();
ob_end_clean();
break;
default:
$this->log('Unknown output format "' . $fe->getType() . '"', Project::MSG_INFO);
continue;
//skip to next formatter in list
break;
}
//end switch
if (!$fe->getUseFile()) {
// output raw to console
echo $output;
} else {
// write to file
$outputFile = $fe->getOutfile();
$check = file_put_contents($outputFile, $output);
if (is_bool($check) && !$check) {
throw new BuildException('Error writing output to ' . $outputFile);
}
}
}
//end foreach
}
示例11: _sniffs
/**
* CodeSnifferShell::_sniffs()
*
* @return array
*/
protected function _sniffs($standard)
{
include_once 'PHP/CodeSniffer.php';
$phpcs = new PHP_CodeSniffer();
$phpcs->process([], $standard);
$sniffs = $phpcs->getSniffs();
$sniffs = array_keys($sniffs);
sort($sniffs);
$result = [];
foreach ($sniffs as $sniff) {
$result[] = $this->_formatSniff($sniff);
}
return $result;
}