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

Breadcrumb

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

class LocalFile

Hierarchy

  • class \PHP_CodeSniffer\Files\File
    • class \PHP_CodeSniffer\Files\LocalFile extends \PHP_CodeSniffer\Files\File

Expanded class hierarchy of LocalFile

1 file declares its use of LocalFile
TestCase.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/TestCase.php

File

vendor/squizlabs/php_codesniffer/src/Files/LocalFile.php, line 17

Namespace

PHP_CodeSniffer\Files
View source
class LocalFile extends File {
    
    /**
     * Creates a LocalFile object and sets the content.
     *
     * @param string                   $path    The absolute path to the file.
     * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run.
     * @param \PHP_CodeSniffer\Config  $config  The config data for the run.
     *
     * @return void
     */
    public function __construct($path, Ruleset $ruleset, Config $config) {
        $this->path = trim($path);
        if (Common::isReadable($this->path) === false) {
            parent::__construct($this->path, $ruleset, $config);
            $error = 'Error opening file; file no longer exists or you do not have access to read the file';
            $this->addMessage(true, $error, 1, 1, 'Internal.LocalFile', [], 5, false);
            $this->ignored = true;
            return;
        }
        // Before we go and spend time tokenizing this file, just check
        // to see if there is a tag up top to indicate that the whole
        // file should be ignored. It must be on one of the first two lines.
        if ($config->annotations === true) {
            $handle = fopen($this->path, 'r');
            if ($handle !== false) {
                $firstContent = fgets($handle);
                $firstContent .= fgets($handle);
                fclose($handle);
                if (strpos($firstContent, '@codingStandardsIgnoreFile') !== false || stripos($firstContent, 'phpcs:ignorefile') !== false) {
                    // We are ignoring the whole file.
                    $this->ignored = true;
                    return;
                }
            }
        }
        $this->reloadContent();
        parent::__construct($this->path, $ruleset, $config);
    }
    
    //end __construct()
    
    /**
     * Loads the latest version of the file's content from the file system.
     *
     * @return void
     */
    public function reloadContent() {
        $this->setContent(file_get_contents($this->path));
    }
    
    //end reloadContent()
    
    /**
     * Processes the file.
     *
     * @return void
     */
    public function process() {
        if ($this->ignored === true) {
            return;
        }
        if ($this->configCache['cache'] === false) {
            parent::process();
            return;
        }
        $hash = md5_file($this->path);
        $hash .= fileperms($this->path);
        $cache = Cache::get($this->path);
        if ($cache !== false && $cache['hash'] === $hash) {
            // We can't filter metrics, so just load all of them.
            $this->metrics = $cache['metrics'];
            if ($this->configCache['recordErrors'] === true) {
                // Replay the cached errors and warnings to filter out the ones
                // we don't need for this specific run.
                $this->configCache['cache'] = false;
                $this->replayErrors($cache['errors'], $cache['warnings']);
                $this->configCache['cache'] = true;
            }
            else {
                $this->errorCount = $cache['errorCount'];
                $this->warningCount = $cache['warningCount'];
                $this->fixableCount = $cache['fixableCount'];
            }
            if (PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_CBF === true && empty($this->config->files) === false) {
                echo "[loaded from cache]... ";
            }
            $this->numTokens = $cache['numTokens'];
            $this->fromCache = true;
            return;
        }
        
        //end if
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
            echo PHP_EOL;
        }
        parent::process();
        $cache = [
            'hash' => $hash,
            'errors' => $this->errors,
            'warnings' => $this->warnings,
            'metrics' => $this->metrics,
            'errorCount' => $this->errorCount,
            'warningCount' => $this->warningCount,
            'fixableCount' => $this->fixableCount,
            'numTokens' => $this->numTokens,
        ];
        Cache::set($this->path, $cache);
        // During caching, we don't filter out errors in any way, so
        // we need to do that manually now by replaying them.
        if ($this->configCache['recordErrors'] === true) {
            $this->configCache['cache'] = false;
            $this->replayErrors($this->errors, $this->warnings);
            $this->configCache['cache'] = true;
        }
    }
    
    //end process()
    
    /**
     * Clears and replays error and warnings for the file.
     *
     * Replaying errors and warnings allows for filtering rules to be changed
     * and then errors and warnings to be reapplied with the new rules. This is
     * particularly useful while caching.
     *
     * @param array $errors   The list of errors to replay.
     * @param array $warnings The list of warnings to replay.
     *
     * @return void
     */
    private function replayErrors($errors, $warnings) {
        $this->errors = [];
        $this->warnings = [];
        $this->errorCount = 0;
        $this->warningCount = 0;
        $this->fixableCount = 0;
        $this->replayingErrors = true;
        foreach ($errors as $line => $lineErrors) {
            foreach ($lineErrors as $column => $colErrors) {
                foreach ($colErrors as $error) {
                    $this->activeListener = $error['listener'];
                    $this->addMessage(true, $error['message'], $line, $column, $error['source'], [], $error['severity'], $error['fixable']);
                }
            }
        }
        foreach ($warnings as $line => $lineErrors) {
            foreach ($lineErrors as $column => $colErrors) {
                foreach ($colErrors as $error) {
                    $this->activeListener = $error['listener'];
                    $this->addMessage(false, $error['message'], $line, $column, $error['source'], [], $error['severity'], $error['fixable']);
                }
            }
        }
        $this->replayingErrors = false;
    }
    
    //end replayErrors()

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
File::$activeListener protected property The class name of the sniff currently processing the file.
File::$config public property The config data for the run.
File::$configCache protected property A cache of often used config settings to improve performance.
File::$content protected property The content of the file.
File::$eolChar public property The EOL character this file uses.
File::$errorCount protected property The total number of errors raised.
File::$errors protected property The errors raised from sniffs.
File::$fixableCount protected property The total number of errors and warnings that can be fixed.
File::$fixedCount protected property The total number of errors and warnings that were fixed.
File::$fixer public property The Fixer object to control fixing errors.
File::$fromCache public property Was the file loaded from cache?
File::$ignored public property If TRUE, the entire file is being ignored.
File::$ignoredCodes protected property An array of message codes that are being ignored.
File::$ignoredListeners protected property An array of sniffs that are being ignored.
File::$listeners protected property An array of sniffs listening to this file's processing.
File::$listenerTimes protected property An array of sniffs being processed and how long they took.
File::$metrics protected property The metrics recorded by sniffs.
File::$metricTokens private property The metrics recorded for each token.
File::$numTokens public property The number of tokens in this file.
File::$path public property The absolute path to the file associated with this object.
File::$replayingErrors protected property TRUE if errors are being replayed from the cache.
File::$ruleset public property The ruleset used for the run.
File::$tokenizer public property The tokenizer being used for this file.
File::$tokenizerType public property The name of the tokenizer being used for this file.
File::$tokens protected property The tokens stack map.
File::$warningCount protected property The total number of warnings raised.
File::$warnings protected property The warnings raised from sniffs.
File::addError public function Records an error against a specific token in the file.
File::addErrorOnLine public function Records an error against a specific line in the file.
File::addFixableError public function Records a fixable error against a specific token in the file.
File::addFixableWarning public function Records a fixable warning against a specific token in the file.
File::addMessage protected function Adds an error to the error stack.
File::addWarning public function Records a warning against a specific token in the file.
File::addWarningOnLine public function Records a warning against a specific line in the file.
File::cleanUp public function Remove vars stored in this file that are no longer required.
File::disableCaching public function Disables caching of this file.
File::findEndOfStatement public function Returns the position of the last non-whitespace token in a statement.
File::findExtendedClassName public function Returns the name of the class that the specified class extends.
(works for classes, anonymous classes and interfaces)
File::findFirstOnLine public function Returns the position of the first token on a line, matching given type.
File::findImplementedInterfaceNames public function Returns the names of the interfaces that the specified class or enum implements.
File::findNext public function Returns the position of the next specified token(s).
File::findPrevious public function Returns the position of the previous specified token(s).
File::findStartOfStatement public function Returns the position of the first non-whitespace token in a statement.
File::getClassProperties public function Returns the visibility and implementation properties of a class.
File::getCondition public function Return the position of the condition for the passed token.
File::getDeclarationName public function Returns the declaration name for classes, interfaces, traits, enums, and functions.
File::getErrorCount public function Returns the number of errors raised.
File::getErrors public function Returns the errors raised from processing this file.
File::getFilename public function Returns the absolute filename of this file.
File::getFixableCount public function Returns the number of fixable errors/warnings raised.
File::getFixedCount public function Returns the number of fixed errors/warnings.
File::getIgnoredLines public function Returns the list of ignored lines.
File::getListenerTimes public function Returns the time taken processing this file for each invoked sniff.
File::getMemberProperties public function Returns the visibility and implementation properties of a class member var.
File::getMethodParameters public function Returns the method parameters for the specified function token.
File::getMethodProperties public function Returns the visibility and implementation properties of a method.
File::getMetrics public function Returns the metrics found while processing this file.
File::getTokens public function Returns the token stack for this file.
File::getTokensAsString public function Returns the content of the tokens from the specified start position in
the token stack for the specified length.
File::getWarningCount public function Returns the number of warnings raised.
File::getWarnings public function Returns the warnings raised from processing this file.
File::hasCondition public function Determine if the passed token has a condition of one of the passed types.
File::isReference public function Determine if the passed token is a reference operator.
File::parse public function Tokenizes the file and prepares it for the test run.
File::recordMetric public function Record a metric about the file being examined.
File::setContent public function Set the content of the file.
LocalFile::process public function Processes the file. Overrides File::process
LocalFile::reloadContent public function Loads the latest version of the file's content from the file system. Overrides File::reloadContent
LocalFile::replayErrors private function Clears and replays error and warnings for the file.
LocalFile::__construct public function Creates a LocalFile object and sets the content. Overrides File::__construct
RSS feed
Powered by Drupal