PHP_CodeSniffer_CLI::explainStandard PHP Method

explainStandard() public method

Prints a report showing the sniffs contained in a standard.
public explainStandard ( string $standard ) : void
$standard string 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();
    }

Usage Example

 /**
  * List all available standards
  *
  * @return void
  */
 public function standards()
 {
     $this->out('Current standard: ' . $this->standard, 2);
     $standards = $this->_standards();
     $this->out('The installed coding standards are:');
     $this->out(implode(', ', $standards));
     $standard = $this->standard;
     if (!empty($this->args[0])) {
         $standard = $this->args[0];
     }
     if (!in_array($standard, $standards)) {
         $this->error('Invalid standard');
     }
     $phpcs = new PHP_CodeSniffer_CLI();
     $phpcs->explainStandard($standard);
 }