PHP_CodeSniffer_Fixer::replaceToken PHP Method

replaceToken() public method

Replace the entire contents of a token.
public replaceToken ( integer $stackPtr, string $content ) : boolean
$stackPtr integer The position of the token in the token stack.
$content string The new content of the token.
return boolean If the change was accepted.
    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();
            if ($bt[1]['class'] === 'PHP_CodeSniffer_Fixer') {
                $sniff = $bt[2]['class'];
                $line = $bt[1]['line'];
            } else {
                $sniff = $bt[1]['class'];
                $line = $bt[0]['line'];
            }
            $tokens = $this->_currentFile->getTokens();
            $type = $tokens[$stackPtr]['type'];
            $oldContent = PHP_CodeSniffer::prepareForOutput($this->_tokens[$stackPtr]);
            $newContent = PHP_CodeSniffer::prepareForOutput($content);
            if (trim($this->_tokens[$stackPtr]) === '' && isset($this->_tokens[$stackPtr + 1]) === true) {
                // Add some context for whitespace only changes.
                $append = PHP_CodeSniffer::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 {$line}) replaced token {$stackPtr} ({$type}) \"{$oldContent}\" => \"{$newContent}\"" . PHP_EOL;
                ob_start();
            }
            return true;
        }
        if (isset($this->_oldTokenValues[$stackPtr]) === false) {
            $this->_oldTokenValues[$stackPtr] = array('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 {$line}) has possible conflict with another sniff on loop {$loop}; caused by the following change ****" . PHP_EOL;
                    echo "{$indent}**** replaced token {$stackPtr} ({$type}) \"{$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: ";
            }
            @ob_end_clean();
            echo "{$indent}{$sniff} (line {$line}) replaced token {$stackPtr} ({$type}) \"{$oldContent}\" => \"{$newContent}\"" . PHP_EOL;
            ob_start();
        }
        return true;
    }