PHP_CodeSniffer::registerSniffs PHP Method

registerSniffs() public method

Loads and stores sniffs objects used for sniffing files.
public registerSniffs ( array $files, array $restrictions, array $exclusions ) : void
$files array Paths to the sniff files to register.
$restrictions array The sniff class names to restrict the allowed listeners to.
$exclusions array The sniff class names to exclude from the listeners list.
return void
    public function registerSniffs($files, $restrictions, $exclusions)
    {
        $listeners = array();
        foreach ($files as $file) {
            // Work out where the position of /StandardName/Sniffs/... is
            // so we can determine what the class will be called.
            $sniffPos = strrpos($file, DIRECTORY_SEPARATOR . 'Sniffs' . DIRECTORY_SEPARATOR);
            if ($sniffPos === false) {
                continue;
            }
            $slashPos = strrpos(substr($file, 0, $sniffPos), DIRECTORY_SEPARATOR);
            if ($slashPos === false) {
                continue;
            }
            $className = substr($file, $slashPos + 1);
            if (substr_count($className, DIRECTORY_SEPARATOR) !== 3) {
                throw new PHP_CodeSniffer_Exception("Sniff file {$className} is not valid; sniff files must be located in a .../StandardName/Sniffs/CategoryName/ directory");
            }
            $className = substr($className, 0, -4);
            $className = str_replace(DIRECTORY_SEPARATOR, '_', $className);
            // If they have specified a list of sniffs to restrict to, check
            // to see if this sniff is allowed.
            if (empty($restrictions) === false && in_array(strtolower($className), $restrictions) === false) {
                continue;
            }
            // If they have specified a list of sniffs to exclude, check
            // to see if this sniff is allowed.
            if (empty($exclusions) === false && in_array(strtolower($className), $exclusions) === true) {
                continue;
            }
            include_once $file;
            // Support the use of PHP namespaces. If the class name we included
            // contains namespace separators instead of underscores, use this as the
            // class name from now on.
            $classNameNS = str_replace('_', '\\', $className);
            if (class_exists($classNameNS, false) === true) {
                $className = $classNameNS;
            }
            // Skip abstract classes.
            $reflection = new ReflectionClass($className);
            if ($reflection->isAbstract() === true) {
                continue;
            }
            $listeners[$className] = $className;
            if (PHP_CODESNIFFER_VERBOSITY > 2) {
                echo "Registered {$className}" . PHP_EOL;
            }
        }
        //end foreach
        $this->sniffs = $listeners;
    }

Usage Example

 protected static function analyze(array $sniffs, $files)
 {
     $sniffs = array_map(static function ($sniff) {
         $sniff = __DIR__ . '/../../../../src/' . $sniff . '.php';
         static::assertFileExists($sniff, 'Sniff does not exist');
         return $sniff;
     }, $sniffs);
     $files = array_map(static function ($file) {
         static::assertFileExists($file, 'Source file does not exists');
         return $file;
     }, (array) $files);
     $codeSniffer = new PHP_CodeSniffer();
     $codeSniffer->registerSniffs($sniffs, []);
     $codeSniffer->populateTokenListeners();
     $report = [];
     foreach ($files as $file) {
         $phpcsFile = $codeSniffer->processFile($file);
         $report[$file] = [];
         $report[$file]['numWarnings'] = $phpcsFile->getWarningCount();
         $report[$file]['warnings'] = $phpcsFile->getWarnings();
         $report[$file]['numErrors'] = $phpcsFile->getErrorCount();
         $report[$file]['errors'] = $phpcsFile->getErrors();
     }
     return $report;
 }
All Usage Examples Of PHP_CodeSniffer::registerSniffs