getSniffs() public method

Gets the array of PHP_CodeSniffer_Sniff's.
public getSniffs ( ) : PHP_CodeSniffer_Sniff[]
return PHP_CodeSniffer_Sniff[]
Esempio n. 1
0
 /**
  * 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;
 }
 /**
  * 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();
         }
     }
 }
Esempio n. 3
0
 /**
  * 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();
 }
 /**
  * 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();
         }
     }
 }
Esempio n. 5
0
 /**
  * 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
                 // most developers write, so we need a special case for any system
                 // that is different.
                 if (DIRECTORY_SEPARATOR === '\\') {
                     $pattern = str_replace('/', '\\\\', $pattern);
                 }
                 $pattern = '`' . $pattern . '`i';
                 if (preg_match($pattern, $this->_file) === 1) {
                     $this->_ignoredListeners[$class] = true;
                     continue 2;
                 }
             }
             $this->_activeListener = $class;
             if (PHP_CODESNIFFER_VERBOSITY > 2) {
                 $startTime = microtime(true);
                 echo "\t\t\tProcessing " . $this->_activeListener . '... ';
             }
             $ignoreTo = $listeners[$class]->process($this, $stackPtr);
             if ($ignoreTo !== null) {
                 $listenerIgnoreTo[$this->_activeListener] = $ignoreTo;
             }
             if (PHP_CODESNIFFER_VERBOSITY > 2) {
                 $timeTaken = microtime(true) - $startTime;
                 if (isset($this->_listenerTimes[$this->_activeListener]) === false) {
                     $this->_listenerTimes[$this->_activeListener] = 0;
                 }
                 $this->_listenerTimes[$this->_activeListener] += $timeTaken;
                 $timeTaken = round($timeTaken, 4);
                 echo "DONE in {$timeTaken} seconds" . PHP_EOL;
             }
             $this->_activeListener = '';
         }
         //end foreach
     }
     //end foreach
     if ($this->_recordErrors === false) {
         $this->_errors = array();
         $this->_warnings = array();
     }
     // If short open tags are off but the file being checked uses
     // short open tags, the whole content will be inline HTML
     // and nothing will be checked. So try and handle this case.
     if ($foundCode === false && $this->tokenizerType === 'PHP') {
         $shortTags = (bool) ini_get('short_open_tag');
         if ($shortTags === false) {
             $error = 'No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them.';
             $this->addWarning($error, null, 'Internal.NoCodeFound');
         }
     }
     if (PHP_CODESNIFFER_VERBOSITY > 2) {
         echo "\t*** END TOKEN PROCESSING ***" . PHP_EOL;
         echo "\t*** START SNIFF PROCESSING REPORT ***" . PHP_EOL;
         asort($this->_listenerTimes, SORT_NUMERIC);
         $this->_listenerTimes = array_reverse($this->_listenerTimes, true);
         foreach ($this->_listenerTimes as $listener => $timeTaken) {
             echo "\t{$listener}: " . round($timeTaken, 4) . ' secs' . PHP_EOL;
         }
         echo "\t*** END SNIFF PROCESSING REPORT ***" . PHP_EOL;
     }
 }
 /**
  * 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;
 }
Esempio n. 7
0
 /**
  * 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;
     }
 }
Esempio n. 8
0
 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;
 }
 /**
  * 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;
 }
Esempio n. 10
0
 /**
  * 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
 }
 /**
  * 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;
 }