Strip whitespace in content tokens surrounding a given token

@param  ref $tokens Reference to the tokens array
@param  int $position
@return void
499255
private function stripWhitespace(&$tokens, $position)
    {
        switch ($tokens[$position][0]) {
            case self::TOKEN_PLACEHOLDER:
            case self::TOKEN_SECTION:
            case self::TOKEN_SECTION_INVERT:
                // Analyze first token of section, and strip leading newlines
                $sectionTokens = $tokens[$position][1]['content'];
                if (0 === count($sectionTokens)) {
                    break;
                }
                $token = $sectionTokens[0];
                if ($token[0] === self::TOKEN_CONTENT) {
                    $content = preg_replace('/^\s*?(\r\n?|\n)/s', '', $token[1]);
                    $token = [
                        self::TOKEN_CONTENT,
                        $content,
                        'original_content' => $token[1],
                    ];
                    $sectionTokens[0] = $token;
                    $tokens[$position][1]['content'] = $sectionTokens;
                    break;
                }
                break;
            default:
                break;
        }

        // Analyze preceding token; if content token, and ending with a newline
        // and optionally whitespace, trim the whitespace.
        if (($position - 1) > -1) {
            $previous = $tokens[$position - 1];
            $type = $previous[0];
            if ($type === self::TOKEN_CONTENT) {
                $content = preg_replace('/(\r\n?|\n)\s+$/s', '$1', $previous[1]);
                $previous = [
                    self::TOKEN_CONTENT,
                    $content,
                    'original_content' => $previous[1],
                ];
                $tokens[$position - 1] = $previous;
            }
        }

        // Analyze next token. If it is a content token, and begins with optional
        // whitespace, followed by a newline, trim this whitespace.
        if (isset($tokens[$position + 1])) {
            $next = $tokens[$position + 1];
            $type = $next[0];
            if ($type === self::TOKEN_CONTENT) {
                $content = preg_replace('/^\s*?(\r\n?|\n)/s', '', $next[1]);
                $next = [
                    self::TOKEN_CONTENT,
                    $content,
                    'original_content' => $next[1],
                ];
                $tokens[$position + 1] = $next;
            }
        }
    }
1002146
protected function handleEntryState($content, $position, TokenList $tokenList) {
        $lang = trim($content, " `\t\n\r\0\x0B");
        $tokenList->addToken($this->name . self::OPEN, $position)
            ->set('lang', $lang);
    }
1370948
protected function handleUnmatchedState($content, $position, TokenList $tokenList) {
        if (empty($content)) {
            return;
        }

        $contentTokenList = $this->parseContent($content);

        $startRule = $this->set->getStartRule();

        for ($i = 0; $i < count($contentTokenList); $i++) {
            if ($contentTokenList->getTokenAt($i)->getName() === $this->name) {
                $token = $contentTokenList->getTokenAt($i);
                $contentTokenList->removeTokenAt($i);
                $contentTokenList->insertTokenAt($startRule->getName(), $token->getPosition(), $i)
                    ->set('content', $token->get('content', ''));
            }
        }

        $tokenList->merge($contentTokenList);

    }
1072698
protected function getNamespace($key, array $tokens)
    {
        $namespace = null;
        $tokenCount = count($tokens);
        for ($i = $key; $i < $tokenCount; $i++) {
            if ($this->isPartOfNamespace($tokens[$i])) {
                $namespace .= $tokens[$i][1];
            } elseif ($tokens[$i] == ';') {
                return $namespace;
            }
        }
    }
227794
private function validateUCFirstDocComment(int $position, array $token): void
    {
        $commentText = $token['content'];

        if (ucfirst($commentText) !== $commentText && $this->isPrevLineEmpty($position)) {
            $isFixing = $this->file->addFixableWarning(
                self::MESSAGE_DOC_COMMENT_UC_FIRST,
                $position,
                static::CODE_DOC_COMMENT_UC_FIRST
            );

            if ($isFixing) {
                $this->fixDocCommentUcFirst($position, $token);
            }
        }
    }
703210
public static function offset(array $tokens, $index)
    {
        if (count($tokens) < 1) {
            return 0;
        }

        if (null === $index) {
            $index = count($tokens) - 1;
        }

        $source = '';

        for ($i = 0; $i < $index; ++$i) {
            $source .= $tokens[$i]->content();
        }

        return strlen($source);
    }
968484
public static function tokenPreviousCharacterIsWhiteSpace($token, &$originalTokens)
    {
        return (isset($originalTokens[$token['i'] - 1])
            && $originalTokens[$token['i'] - 1][Tokenizer::TOKEN_TYPE] !== Tokenizer::TOKEN_TYPE_WHITESPACE);
    }
119945
private function matchNamePositionForEndOfFunctionCall(Tokens $tokens, int $position): ?int
    {
        try {
            $blockStart = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $position);
        } catch (Throwable $throwable) {
            // not a block start
            return null;
        }

        $previousTokenPosition = $blockStart - 1;
        $possibleMethodNameToken = $tokens[$previousTokenPosition];

        // not a "methodCall()"
        if (! $possibleMethodNameToken->isGivenKind(T_STRING)) {
            return null;
        }

        // starts with small letter?
        $methodOrFunctionName = $possibleMethodNameToken->getContent();
        if (! ctype_lower($methodOrFunctionName[0])) {
            return null;
        }

        // is "someCall()"? we don't care, there are no arguments
        if ($tokens[$blockStart + 1]->equals(')')) {
            return null;
        }

        return $previousTokenPosition;
    }
33369
public static function isPrecedingCurrentTokenOfTokenTypeWhiteSpace($originalTokens, $token)
    {
        return isset($originalTokens[$token['i'] - 1])
        && $originalTokens[$token['i'] - 1][Tokenizer::TOKEN_TYPE] !== Tokenizer::TOKEN_TYPE_WHITESPACE;
    }
1164306
protected function isShortArraySyntax(array $tokens, Node\Expr\Array_ $array)
    {
        $i = $array->getAttribute('startTokenPos');
        return is_string($tokens[$i]);
    }
525298
private static function getNthTokenWithoutWhitespace($tokens, $i) {
		$j = 0;
		foreach ($tokens as $token) {
			if ($token['token'] == 'T_WHITESPACE') {
				continue;
			}
			if ($i == $j) {
				return $token;
			}
			$j++;
		}
		return null;
	}
824843
protected function whitespaceInInlineElements($contents, PlaceholderContainer $placeholderContainer)
    {
        $elementsRegex = $this->getInlineElementsRegex();

        return preg_replace_callback(
            '/
                (
                    <('.$elementsRegex.')   # Match an inline element
                    (?:(?!<\/\2>).*)        # Match everything except its end tag
                    <\/\2>                  # Match the end tag
                    \s+
                )
                <('.$elementsRegex.')       # Match starting tag
            /xis',
            function ($match) use ($placeholderContainer) {
                return $this->replaceWhitespacesInInlineElements($match[1], $placeholderContainer).'<'.$match[3];
            }, $contents);
    }
70224
private function checkLinesBeforeFirstUse(int $firstUsePos): void
    {
        $posBeforeFirstUse = TokenHelper::findPreviousExcluding($this->getFile(), T_WHITESPACE, $firstUsePos - 1);
        $realLinesBeforeUse = $this->getRealLinesBeforeFirstUse($firstUsePos, $posBeforeFirstUse);

        if ($realLinesBeforeUse !== self::LINES_BEFORE_FIRST_USE) {
            $fix = $this->getFile()->addFixableError(
                self::MESSAGE_INCORRECT_LINES_COUNT_BEFORE_FIRST_USE,
                $firstUsePos,
                self::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_USE,
                [
                    self::LINES_BEFORE_FIRST_USE,
                    $realLinesBeforeUse
                ]
            );

            if ($fix) {
                $this->fixLinesBeforeFirstUse($firstUsePos, $posBeforeFirstUse, self::LINES_BEFORE_FIRST_USE);
            }
        }
    }
46725
private function fixSpacesBelow(Tokens $tokens, int $classEnd, int $propertyEnd): void
	{
		$nextNotWhitePosition = $tokens->getNextNonWhitespace($propertyEnd);
		if ($nextNotWhitePosition === null) {
			return;
		}

		$classAttributesSeparationFixer = new ClassAttributesSeparationFixer();
		$classAttributesSeparationFixer->setWhitespacesConfig($this->whitespacesFixerConfig);

		$arguments = [
			$tokens,
			$propertyEnd,
			$nextNotWhitePosition,
			$nextNotWhitePosition === $classEnd ? $this->configuration[self::SPACE_COUNT_OPTION] : $this->configuration[self::SPACE_COUNT_OPTION] + 1,
		];

		(new PrivatesCaller())->callPrivateMethod(
			$classAttributesSeparationFixer,
			'correctLineBreaks',
			...$arguments
		);
	}
990167
private function _processMinus($token) {
		if (!$this->_inFuncCall) {
			$this->_checkWhiteSpaceBefore($token->text);
		}
		// We allow some '-' signs to skip the the space afterwards for negative numbers
		// float number or integer
		if (!($this->tokenizer->checkNextToken(T_LNUMBER) || $this->tokenizer->checkNextToken(T_DNUMBER))) {
			$this->_checkWhiteSpaceAfter($token->text);
		}
	}
328769
protected function handleSingleState($content, $position, TokenList $tokenList) {
        preg_match('/!\\[((?:\\\\.|[^]\\\\])+)\\][\\t ]*\\[((?:\\\\.|[^]\\\\])*)\\]/', $content, $matches);
        $reference = strtolower(empty($matches[2]) ? $matches[1] : $matches[2]);

        $referenceTokens = $tokenList->getTokensByCallback(function (Token $token) use ($reference) {
            return $token->getName() === ReferenceRule::NAME && $token->get('reference', null) === $reference;
        });

        if (($referenceToken = reset($referenceTokens)) !== false) {
            $referenceToken->set('used', true);
            $reference = $referenceToken;
        }

        $tokenList->addToken($this->name, $position)
            ->set('match', $content)
            ->set('alt', $matches[1])
            ->set('reference', $reference);
    }
303676
public void skipWhitespace(List<Token> tokens) {
		while (pos < input.length()
				&& (input.charAt(pos) == '\n' || input.charAt(pos) == '\t')) {
			pos++;
		}
	}