getInstalledStandardPath() public static method

Coding standards are directories located in the CodeSniffer/Standards directory. Valid coding standards include a ruleset.xml file.
public static getInstalledStandardPath ( string $standard ) : string | null
$standard string The name of the coding standard.
return string | null
示例#1
0
 /**
  * Add all sniff unit tests into a test suite.
  *
  * Sniff unit tests are found by recursing through the 'Tests' directory
  * of each installed coding standard.
  *
  * @return PHPUnit_Framework_TestSuite
  */
 public static function suite()
 {
     $suite = new PHPUnit_Framework_TestSuite('PHP CodeSniffer Standards');
     $baseDir = pathinfo(getcwd() . "/Ongr", PATHINFO_DIRNAME);
     \PHP_CodeSniffer::setConfigData('installed_paths', $baseDir);
     $path = pathinfo(\PHP_CodeSniffer::getInstalledStandardPath('Ongr'), PATHINFO_DIRNAME);
     $testsDir = $path . DIRECTORY_SEPARATOR . 'Tests' . DIRECTORY_SEPARATOR . 'Unit';
     $directoryIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($testsDir));
     /** @var \SplFileInfo $fileinfo */
     foreach ($directoryIterator as $file) {
         // Skip hidden and extension must be php.
         if ($file->getFilename()[0] === '.' || pathinfo($file, PATHINFO_EXTENSION) !== 'php') {
             continue;
         }
         $className = str_replace([$baseDir . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], ['', '\\'], substr($file, 0, -4));
         $suite->addTest(new $className('getErrorList'));
     }
     return $suite;
 }
示例#2
0
 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;
 }