/** * Initialise the reporter. * * All reports specified in the config will be created and their * output file (or a temp file if none is specified) initialised by * clearing the current contents. * * @param \PHP_CodeSniffer\Config $config The config data for the run. * * @return void * @throws RuntimeException If a report is not available. */ public function __construct(Config $config) { $this->config = $config; foreach ($config->reports as $type => $output) { $type = ucfirst($type); if ($output === null) { $output = $config->reportFile; } if (strpos($type, '.') !== false) { // This is a path to a custom report class. $filename = realpath($type); if ($filename === false) { echo "ERROR: Custom report \"{$type}\" not found" . PHP_EOL; exit(3); } $reportClassName = Autoload::loadFile($filename); } else { $reportClassName = 'PHP_CodeSniffer\\Reports\\' . $type; } $reportClass = new $reportClassName(); if (false === $reportClass instanceof Report) { throw new RuntimeException('Class "' . $reportClassName . '" must implement the "PHP_CodeSniffer\\Report" interface.'); } $this->reports[$type] = array('output' => $output, 'class' => $reportClass); if ($output === null) { // Using a temp file. $this->tmpFiles[$type] = tempnam(sys_get_temp_dir(), 'phpcs'); file_put_contents($this->tmpFiles[$type], ''); } else { file_put_contents($output, ''); } } //end foreach }
/** * Loads and stores sniffs objects used for sniffing files. * * @param array $files Paths to the sniff files to register. * @param array $restrictions The sniff class names to restrict the allowed * listeners to. * @param array $exclusions 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 = Autoload::loadFile($file); // If they have specified a list of sniffs to restrict to, check // to see if this sniff is allowed. if (empty($restrictions) === false && isset($restrictions[strtolower($className)]) === false) { continue; } // If they have specified a list of sniffs to exclude, check // to see if this sniff is allowed. if (empty($exclusions) === false && isset($exclusions[strtolower($className)]) === true) { continue; } // 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; }
/** * Get the class name of the filter being used for the run. * * @return string */ private function getFilterClass() { $filterType = $this->config->filter; if ($filterType === null) { $filterClass = '\\PHP_CodeSniffer\\Filters\\Filter'; } else { if (strpos($filterType, '.') !== false) { // This is a path to a custom filter class. $filename = realpath($filterType); if ($filename === false) { echo "ERROR: Custom filter \"{$filterType}\" not found" . PHP_EOL; exit(3); } $filterClass = \PHP_CodeSniffer\Autoload::loadFile($filename); } else { $filterClass = '\\PHP_CodeSniffer\\Filters\\' . $filterType; } } return $filterClass; }