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

Breadcrumb

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

function Fixer::replaceToken

Replace the entire contents of a token.

Parameters

int $stackPtr The position of the token in the token stack.:

string $content The new content of the token.:

Return value

bool If the change was accepted.

7 calls to Fixer::replaceToken()
Fixer::addContent in vendor/squizlabs/php_codesniffer/src/Fixer.php
Adds content to the end of a token's current content.
Fixer::addContentBefore in vendor/squizlabs/php_codesniffer/src/Fixer.php
Adds content to the start of a token's current content.
Fixer::addNewline in vendor/squizlabs/php_codesniffer/src/Fixer.php
Adds a newline to end of a token's content.
Fixer::addNewlineBefore in vendor/squizlabs/php_codesniffer/src/Fixer.php
Adds a newline to the start of a token's content.
Fixer::changeCodeBlockIndent in vendor/squizlabs/php_codesniffer/src/Fixer.php
Adjust the indent of a code block.

... See full list

File

vendor/squizlabs/php_codesniffer/src/Fixer.php, line 514

Class

Fixer

Namespace

PHP_CodeSniffer

Code

public function replaceToken($stackPtr, $content) {
    if ($this->inConflict === true) {
        return false;
    }
    if ($this->inChangeset === false && isset($this->fixedTokens[$stackPtr]) === true) {
        $indent = "\t";
        if (empty($this->changeset) === false) {
            $indent .= "\t";
        }
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
            @ob_end_clean();
            echo "{$indent}* token {$stackPtr} has already been modified, skipping *" . PHP_EOL;
            ob_start();
        }
        return false;
    }
    if (PHP_CODESNIFFER_VERBOSITY > 1) {
        $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
        if ($bt[1]['class'] === 'PHP_CodeSniffer\\Fixer') {
            $sniff = $bt[2]['class'];
            $line = $bt[1]['line'];
        }
        else {
            $sniff = $bt[1]['class'];
            $line = $bt[0]['line'];
        }
        $sniff = Common::getSniffCode($sniff);
        $tokens = $this->currentFile
            ->getTokens();
        $type = $tokens[$stackPtr]['type'];
        $tokenLine = $tokens[$stackPtr]['line'];
        $oldContent = Common::prepareForOutput($this->tokens[$stackPtr]);
        $newContent = Common::prepareForOutput($content);
        if (trim($this->tokens[$stackPtr]) === '' && isset($this->tokens[$stackPtr + 1]) === true) {
            // Add some context for whitespace only changes.
            $append = Common::prepareForOutput($this->tokens[$stackPtr + 1]);
            $oldContent .= $append;
            $newContent .= $append;
        }
    }
    
    //end if
    if ($this->inChangeset === true) {
        $this->changeset[$stackPtr] = $content;
        if (PHP_CODESNIFFER_VERBOSITY > 1) {
            @ob_end_clean();
            echo "\t\tQ: {$sniff}:{$line} replaced token {$stackPtr} ({$type} on line {$tokenLine}) \"{$oldContent}\" => \"{$newContent}\"" . PHP_EOL;
            ob_start();
        }
        return true;
    }
    if (isset($this->oldTokenValues[$stackPtr]) === false) {
        $this->oldTokenValues[$stackPtr] = [
            'curr' => $content,
            'prev' => $this->tokens[$stackPtr],
            'loop' => $this->loops,
        ];
    }
    else {
        if ($this->oldTokenValues[$stackPtr]['prev'] === $content && $this->oldTokenValues[$stackPtr]['loop'] === $this->loops - 1) {
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                $indent = "\t";
                if (empty($this->changeset) === false) {
                    $indent .= "\t";
                }
                $loop = $this->oldTokenValues[$stackPtr]['loop'];
                @ob_end_clean();
                echo "{$indent}**** {$sniff}:{$line} has possible conflict with another sniff on loop {$loop}; caused by the following change ****" . PHP_EOL;
                echo "{$indent}**** replaced token {$stackPtr} ({$type} on line {$tokenLine}) \"{$oldContent}\" => \"{$newContent}\" ****" . PHP_EOL;
            }
            if ($this->oldTokenValues[$stackPtr]['loop'] >= $this->loops - 1) {
                $this->inConflict = true;
                if (PHP_CODESNIFFER_VERBOSITY > 1) {
                    echo "{$indent}**** ignoring all changes until next loop ****" . PHP_EOL;
                }
            }
            if (PHP_CODESNIFFER_VERBOSITY > 1) {
                ob_start();
            }
            return false;
        }
        
        //end if
        $this->oldTokenValues[$stackPtr]['prev'] = $this->oldTokenValues[$stackPtr]['curr'];
        $this->oldTokenValues[$stackPtr]['curr'] = $content;
        $this->oldTokenValues[$stackPtr]['loop'] = $this->loops;
    }
    
    //end if
    $this->fixedTokens[$stackPtr] = $this->tokens[$stackPtr];
    $this->tokens[$stackPtr] = $content;
    $this->numFixes++;
    if (PHP_CODESNIFFER_VERBOSITY > 1) {
        $indent = "\t";
        if (empty($this->changeset) === false) {
            $indent .= "\tA: ";
        }
        if (ob_get_level() > 0) {
            ob_end_clean();
        }
        echo "{$indent}{$sniff}:{$line} replaced token {$stackPtr} ({$type} on line {$tokenLine}) \"{$oldContent}\" => \"{$newContent}\"" . PHP_EOL;
        ob_start();
    }
    return true;
}

API Navigation

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