Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Reporter.php

function Reporter::__construct

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.

Parameters

\PHP_CodeSniffer\Config $config The config data for the run.:

Return value

void

Throws

\PHP_CodeSniffer\Exceptions\DeepExitException If a custom report class could not be found.

\PHP_CodeSniffer\Exceptions\RuntimeException If a report class is incorrectly set up.

File

vendor/squizlabs/php_codesniffer/src/Reporter.php, line 98

Class

Reporter

Namespace

PHP_CodeSniffer

Code

public function __construct(Config $config) {
    $this->config = $config;
    foreach ($config->reports as $type => $output) {
        if ($output === null) {
            $output = $config->reportFile;
        }
        $reportClassName = '';
        if (strpos($type, '.') !== false) {
            // This is a path to a custom report class.
            $filename = realpath($type);
            if ($filename === false) {
                $error = "ERROR: Custom report \"{$type}\" not found" . PHP_EOL;
                throw new DeepExitException($error, 3);
            }
            $reportClassName = Autoload::loadFile($filename);
        }
        else {
            if (class_exists('PHP_CodeSniffer\\Reports\\' . ucfirst($type)) === true) {
                // PHPCS native report.
                $reportClassName = 'PHP_CodeSniffer\\Reports\\' . ucfirst($type);
            }
            else {
                if (class_exists($type) === true) {
                    // FQN of a custom report.
                    $reportClassName = $type;
                }
                else {
                    // OK, so not a FQN, try and find the report using the registered namespaces.
                    $registeredNamespaces = Autoload::getSearchPaths();
                    $trimmedType = ltrim($type, '\\');
                    foreach ($registeredNamespaces as $nsPrefix) {
                        if ($nsPrefix === '') {
                            continue;
                        }
                        if (class_exists($nsPrefix . '\\' . $trimmedType) === true) {
                            $reportClassName = $nsPrefix . '\\' . $trimmedType;
                            break;
                        }
                    }
                }
            }
        }
        
        //end if
        if ($reportClassName === '') {
            $error = "ERROR: Class file for report \"{$type}\" not found" . PHP_EOL;
            throw new DeepExitException($error, 3);
        }
        $reportClass = new $reportClassName();
        if ($reportClass instanceof Report === false) {
            throw new RuntimeException('Class "' . $reportClassName . '" must implement the "PHP_CodeSniffer\\Report" interface.');
        }
        $this->reports[$type] = [
            'output' => $output,
            'class' => $reportClass,
        ];
        if ($output === null) {
            // Using a temp file.
            // This needs to be set in the constructor so that all
            // child procs use the same report file when running in parallel.
            $this->tmpFiles[$type] = tempnam(sys_get_temp_dir(), 'phpcs');
            file_put_contents($this->tmpFiles[$type], '');
        }
        else {
            file_put_contents($output, '');
        }
    }
    
    //end foreach
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal