PHP_CodeSniffer_File::_createPositionMap PHP Method

_createPositionMap() private static method

Can also convert tabs into spaces. Each tab can represent between 1 and $width spaces, so this cannot be a straight string replace.
private static _createPositionMap ( array &$tokens, object $tokenizer, string $eolChar, string $encoding, integer $tabWidth ) : void
$tokens array The array of tokens to process.
$tokenizer object The tokenizer being used to process this file.
$eolChar string The EOL character to use for splitting strings.
$encoding string The charset of the sniffed file.
$tabWidth integer The number of spaces that each tab represents. Set to 0 to disable tab replacement.
return void
    private static function _createPositionMap(&$tokens, $tokenizer, $eolChar, $encoding, $tabWidth)
    {
        $currColumn = 1;
        $lineNumber = 1;
        $eolLen = strlen($eolChar) * -1;
        $tokenizerType = get_class($tokenizer);
        $ignoring = false;
        $inTests = defined('PHP_CODESNIFFER_IN_TESTS');
        $checkEncoding = false;
        if ($encoding !== 'iso-8859-1' && function_exists('iconv_strlen') === true) {
            $checkEncoding = true;
        }
        $tokensWithTabs = array(T_WHITESPACE => true, T_COMMENT => true, T_DOC_COMMENT => true, T_DOC_COMMENT_WHITESPACE => true, T_DOC_COMMENT_STRING => true, T_CONSTANT_ENCAPSED_STRING => true, T_DOUBLE_QUOTED_STRING => true, T_HEREDOC => true, T_NOWDOC => true, T_INLINE_HTML => true);
        $numTokens = count($tokens);
        for ($i = 0; $i < $numTokens; $i++) {
            $tokens[$i]['line'] = $lineNumber;
            $tokens[$i]['column'] = $currColumn;
            if ($tokenizerType === 'PHP_CodeSniffer_Tokenizers_PHP' && isset(PHP_CodeSniffer_Tokens::$knownLengths[$tokens[$i]['code']]) === true) {
                // There are no tabs in the tokens we know the length of.
                $length = PHP_CodeSniffer_Tokens::$knownLengths[$tokens[$i]['code']];
                $currColumn += $length;
            } else {
                if ($tabWidth === 0 || isset($tokensWithTabs[$tokens[$i]['code']]) === false || strpos($tokens[$i]['content'], "\t") === false) {
                    // There are no tabs in this content, or we aren't replacing them.
                    if ($checkEncoding === true) {
                        // Not using the default encoding, so take a bit more care.
                        $length = @iconv_strlen($tokens[$i]['content'], $encoding);
                        if ($length === false) {
                            // String contained invalid characters, so revert to default.
                            $length = strlen($tokens[$i]['content']);
                        }
                    } else {
                        $length = strlen($tokens[$i]['content']);
                    }
                    $currColumn += $length;
                } else {
                    if (str_replace("\t", '', $tokens[$i]['content']) === '') {
                        // String only contains tabs, so we can shortcut the process.
                        $numTabs = strlen($tokens[$i]['content']);
                        $newContent = '';
                        $firstTabSize = $tabWidth - $currColumn % $tabWidth + 1;
                        $length = $firstTabSize + $tabWidth * ($numTabs - 1);
                        $currColumn += $length;
                        $newContent = str_repeat(' ', $length);
                    } else {
                        // We need to determine the length of each tab.
                        $tabs = explode("\t", $tokens[$i]['content']);
                        $numTabs = count($tabs) - 1;
                        $tabNum = 0;
                        $newContent = '';
                        $length = 0;
                        foreach ($tabs as $content) {
                            if ($content !== '') {
                                $newContent .= $content;
                                if ($checkEncoding === true) {
                                    // Not using the default encoding, so take a bit more care.
                                    $contentLength = @iconv_strlen($content, $encoding);
                                    if ($contentLength === false) {
                                        // String contained invalid characters, so revert to default.
                                        $contentLength = strlen($content);
                                    }
                                } else {
                                    $contentLength = strlen($content);
                                }
                                $currColumn += $contentLength;
                                $length += $contentLength;
                            }
                            // The last piece of content does not have a tab after it.
                            if ($tabNum === $numTabs) {
                                break;
                            }
                            // Process the tab that comes after the content.
                            $lastCurrColumn = $currColumn;
                            $tabNum++;
                            // Move the pointer to the next tab stop.
                            if ($currColumn % $tabWidth === 0) {
                                // This is the first tab, and we are already at a
                                // tab stop, so this tab counts as a single space.
                                $currColumn++;
                            } else {
                                $currColumn++;
                                while ($currColumn % $tabWidth !== 0) {
                                    $currColumn++;
                                }
                                $currColumn++;
                            }
                            $length += $currColumn - $lastCurrColumn;
                            $newContent .= str_repeat(' ', $currColumn - $lastCurrColumn);
                        }
                        //end foreach
                    }
                    //end if
                    $tokens[$i]['orig_content'] = $tokens[$i]['content'];
                    $tokens[$i]['content'] = $newContent;
                }
            }
            //end if
            $tokens[$i]['length'] = $length;
            if (isset(PHP_CodeSniffer_Tokens::$knownLengths[$tokens[$i]['code']]) === false && strpos($tokens[$i]['content'], $eolChar) !== false) {
                $lineNumber++;
                $currColumn = 1;
                // Newline chars are not counted in the token length.
                $tokens[$i]['length'] += $eolLen;
            }
            if ($tokens[$i]['code'] === T_COMMENT || $tokens[$i]['code'] === T_DOC_COMMENT_TAG || $inTests === true && $tokens[$i]['code'] === T_INLINE_HTML) {
                if (strpos($tokens[$i]['content'], '@codingStandards') !== false) {
                    if ($ignoring === false && strpos($tokens[$i]['content'], '@codingStandardsIgnoreStart') !== false) {
                        $ignoring = true;
                    } else {
                        if ($ignoring === true && strpos($tokens[$i]['content'], '@codingStandardsIgnoreEnd') !== false) {
                            $ignoring = false;
                            // Ignore this comment too.
                            self::$_ignoredLines[$tokens[$i]['line']] = true;
                        } else {
                            if ($ignoring === false && strpos($tokens[$i]['content'], '@codingStandardsIgnoreLine') !== false) {
                                self::$_ignoredLines[$tokens[$i]['line'] + 1] = true;
                                // Ignore this comment too.
                                self::$_ignoredLines[$tokens[$i]['line']] = true;
                            }
                        }
                    }
                }
            }
            //end if
            if ($ignoring === true) {
                self::$_ignoredLines[$tokens[$i]['line']] = true;
            }
        }
        //end for
    }