本文整理匯總了PHP中Symfony\CS\Tokenizer\Tokens::getPrevNonWhitespace方法的典型用法代碼示例。如果您正苦於以下問題:PHP Tokens::getPrevNonWhitespace方法的具體用法?PHP Tokens::getPrevNonWhitespace怎麽用?PHP Tokens::getPrevNonWhitespace使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\CS\Tokenizer\Tokens
的用法示例。
在下文中一共展示了Tokens::getPrevNonWhitespace方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_FUNCTION)) {
continue;
}
$startParenthesisIndex = $tokens->getNextTokenOfKind($index, array('('));
$endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startParenthesisIndex);
for ($iter = $endParenthesisIndex - 1; $iter > $startParenthesisIndex; --$iter) {
if (!$tokens[$iter]->isGivenKind(T_VARIABLE)) {
continue;
}
// skip ... before $variable for variadic parameter
if (defined('T_ELLIPSIS')) {
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter);
if ($tokens[$prevNonWhitespaceIndex]->isGivenKind(T_ELLIPSIS)) {
$iter = $prevNonWhitespaceIndex;
}
}
// skip & before $variable for parameter passed by reference
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($iter);
if ($tokens[$prevNonWhitespaceIndex]->equals('&')) {
$iter = $prevNonWhitespaceIndex;
}
if (!$tokens[$iter - 1]->equalsAny(array(array(T_WHITESPACE), array(T_COMMENT), array(T_DOC_COMMENT), '(', ','))) {
$tokens->insertAt($iter, new Token(array(T_WHITESPACE, ' ')));
}
}
}
}
示例2: fixContent
/**
* @param Tokens|Token[] $tokens
*
* @return void
*/
protected function fixContent(Tokens $tokens)
{
$wrongTokens = [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW];
foreach ($tokens as $index => $token) {
$tokenContent = strtolower($token->getContent());
if (strtolower($tokenContent) !== 'php_sapi_name') {
continue;
}
$openingBrace = $tokens->getNextMeaningfulToken($index);
if ($openingBrace === null || $tokens[$openingBrace]->getContent() !== '(') {
continue;
}
$closingBrace = $tokens->getNextMeaningfulToken($openingBrace);
if ($closingBrace === null || $tokens[$closingBrace]->getContent() !== ')') {
continue;
}
$prevIndex = $tokens->getPrevNonWhitespace($index);
if ($prevIndex === null || in_array($tokens[$prevIndex]->getId(), $wrongTokens, true)) {
continue;
}
$tokens[$index]->setContent('PHP_SAPI');
for ($i = $openingBrace; $i <= $closingBrace; ++$i) {
$tokens[$i]->clear();
}
}
}
示例3: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_NEW)) {
continue;
}
$nextIndex = $tokens->getNextTokenOfKind($index, array(':', ';', ',', '(', ')', '[', ']', array(CT_ARRAY_SQUARE_BRACE_OPEN), array(CT_ARRAY_SQUARE_BRACE_CLOSE), array(CT_BRACE_CLASS_INSTANTIATION_OPEN), array(CT_BRACE_CLASS_INSTANTIATION_CLOSE)));
$nextToken = $tokens[$nextIndex];
// entrance into array index syntax - need to look for exit
while ($nextToken->equals('[')) {
$nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $nextIndex) + 1;
$nextToken = $tokens[$nextIndex];
}
// new statement has a gap in it - advance to the next token
if ($nextToken->isGivenKind(T_WHITESPACE)) {
$nextIndex = $tokens->getNextNonWhitespace($nextIndex);
$nextToken = $tokens[$nextIndex];
}
// new statement with () - nothing to do
if ($nextToken->equals('(')) {
continue;
}
$meaningBeforeNextIndex = $tokens->getPrevNonWhitespace($nextIndex);
$tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')')));
}
}
示例4: fixContent
/**
* @param Tokens|Token[] $tokens
*
* @return void
*/
protected function fixContent(Tokens $tokens)
{
$wrongTokens = [T_FUNCTION, T_OBJECT_OPERATOR, T_NEW];
foreach ($tokens as $index => $token) {
$tokenContent = strtolower($token->getContent());
if (empty($tokenContent) || !isset(self::$matching[$tokenContent])) {
continue;
}
$prevIndex = $tokens->getPrevNonWhitespace($index);
if (in_array($tokens[$prevIndex]->getId(), $wrongTokens, true)) {
continue;
}
$openingBrace = $tokens->getNextMeaningfulToken($index);
if ($tokens[$openingBrace]->getContent() !== '(') {
continue;
}
$closingBrace = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openingBrace);
// Skip for non-trivial cases
for ($i = $openingBrace + 1; $i < $closingBrace; ++$i) {
if ($tokens[$i]->equals(',')) {
continue 2;
}
}
$cast = '(' . self::$matching[$tokenContent] . ')';
$tokens[$index]->setContent($cast);
$tokens[$openingBrace]->setContent('');
$tokens[$closingBrace]->setContent('');
}
}
示例5: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$functionyTokens = $this->getFunctionyTokenKinds();
$languageConstructionTokens = $this->getLanguageConstructionTokenKinds();
foreach ($tokens as $index => $token) {
// looking for start brace
if (!$token->equals('(')) {
continue;
}
// last non-whitespace token
$lastTokenIndex = $tokens->getPrevNonWhitespace($index);
if (null === $lastTokenIndex) {
continue;
}
// check for ternary operator
$endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
$nextNonWhiteSpace = $tokens->getNextMeaningfulToken($endParenthesisIndex);
if (!empty($nextNonWhiteSpace) && $tokens[$nextNonWhiteSpace]->equals('?') && $tokens[$lastTokenIndex]->isGivenKind($languageConstructionTokens)) {
continue;
}
// check if it is a function call
if ($tokens[$lastTokenIndex]->isGivenKind($functionyTokens)) {
$this->fixFunctionCall($tokens, $index);
}
}
}
示例6: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->isGivenKind(array(T_CASE, T_DEFAULT))) {
continue;
}
$ternariesCount = 0;
for ($colonIndex = $index + 1;; ++$colonIndex) {
// We have to skip ternary case for colons.
if ($tokens[$colonIndex]->equals('?')) {
++$ternariesCount;
}
if ($tokens[$colonIndex]->equals(':')) {
if (0 === $ternariesCount) {
break;
}
--$ternariesCount;
}
}
$valueIndex = $tokens->getPrevNonWhitespace($colonIndex);
if (2 + $valueIndex === $colonIndex) {
$tokens[$valueIndex + 1]->clear();
}
}
}
示例7: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_RETURN)) {
continue;
}
$prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)];
if (!$prevNonWhitespaceToken->equalsAny(array(';', '}'))) {
continue;
}
$prevToken = $tokens[$index - 1];
if ($prevToken->isWhitespace()) {
$parts = explode("\n", $prevToken->getContent());
$countParts = count($parts);
if (1 === $countParts) {
$prevToken->setContent(rtrim($prevToken->getContent(), " \t") . "\n\n");
} elseif (count($parts) <= 2) {
$prevToken->setContent("\n" . $prevToken->getContent());
}
} else {
$tokens->insertAt($index, new Token(array(T_WHITESPACE, "\n\n")));
++$index;
++$limit;
}
}
}
示例8: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
$token = $tokens[$index];
if (!$token->isGivenKind(T_LIST)) {
continue;
}
$openIndex = $tokens->getNextMeaningfulToken($index);
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);
$markIndex = null;
$prevIndex = $tokens->getPrevNonWhitespace($closeIndex);
while ($tokens[$prevIndex]->equals(',')) {
$markIndex = $prevIndex;
$prevIndex = $tokens->getPrevNonWhitespace($prevIndex);
}
if (null !== $markIndex) {
$tokens->clearRange($tokens->getPrevNonWhitespace($markIndex) + 1, $closeIndex - 1);
}
}
}
示例9: fixSpace
public function fixSpace(Tokens $tokens, $index)
{
if ($tokens[$index - 1]->isWhitespace()) {
$prevIndex = $tokens->getPrevNonWhitespace($index - 1);
if (!$tokens[$prevIndex]->equalsAny(array(',', array(T_END_HEREDOC)))) {
$tokens[$index - 1]->clear();
}
}
if (!$tokens[$index + 1]->isWhitespace()) {
$tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
}
}
示例10: fixSpace
/**
* Method to insert space after comma and remove space before comma.
*
* @param Tokens $tokens
* @param int $index
*/
public function fixSpace(Tokens $tokens, $index)
{
// remove space before comma if exist
if ($tokens[$index - 1]->isWhitespace()) {
$prevIndex = $tokens->getPrevNonWhitespace($index - 1);
if (!$tokens[$prevIndex]->equalsAny(array(',', array(T_END_HEREDOC)))) {
$tokens[$index - 1]->clear();
}
}
// add space after comma if not exist
if (!$tokens[$index + 1]->isWhitespace()) {
$tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
}
}
示例11: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
foreach ($tokens as $index => $token) {
if (!$token->equals('.')) {
continue;
}
if (!$tokens[$tokens->getPrevNonWhitespace($index)]->isGivenKind(T_LNUMBER)) {
$tokens->removeLeadingWhitespace($index, " \t");
}
if (!$tokens[$tokens->getNextNonWhitespace($index)]->isGivenKind(T_LNUMBER)) {
$tokens->removeTrailingWhitespace($index, " \t");
}
}
}
示例12: fixFunction
private function fixFunction(Tokens $tokens, $functionIndex, array $functionParams)
{
$startBraceIndex = $tokens->getNextTokenOfKind($functionIndex, array('('));
$endBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startBraceIndex);
$commaCounter = 0;
$sawParameter = false;
for ($index = $startBraceIndex + 1; $index < $endBraceIndex; ++$index) {
$token = $tokens[$index];
if (!$token->isWhitespace() && !$token->isComment()) {
$sawParameter = true;
}
if ($token->equals('(')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
continue;
}
if ($token->equals('[')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_SQUARE_BRACE, $index);
continue;
}
if ($token->equals(',')) {
++$commaCounter;
continue;
}
}
$functionParamsQuantity = count($functionParams);
$paramsQuantity = ($sawParameter ? 1 : 0) + $commaCounter;
if ($paramsQuantity === $functionParamsQuantity) {
return;
}
$tokensToInsert = array();
for ($i = $paramsQuantity; $i < $functionParamsQuantity; ++$i) {
// function call do not have all params that are required to set useStrict flag, exit from method!
if (!$functionParams[$i]) {
return;
}
$tokensToInsert[] = new Token(',');
$tokensToInsert[] = new Token(array(T_WHITESPACE, ' '));
if (!is_array($functionParams[$i])) {
$tokensToInsert[] = clone $functionParams[$i];
continue;
}
foreach ($functionParams[$i] as $param) {
$tokensToInsert[] = clone $param;
}
}
$beforeEndBraceIndex = $tokens->getPrevNonWhitespace($endBraceIndex, array());
$tokens->insertAt($beforeEndBraceIndex + 1, $tokensToInsert);
}
示例13: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
if (!$tokens->isMonolithicPhp()) {
return;
}
$closeTags = $tokens->findGivenKind(T_CLOSE_TAG);
if (empty($closeTags)) {
return;
}
list($index, $token) = each($closeTags);
$tokens->removeLeadingWhitespace($index);
$token->clear();
$prevIndex = $tokens->getPrevNonWhitespace($index);
$prevToken = $tokens[$prevIndex];
if (!$prevToken->equalsAny(array(';', '}'))) {
$tokens->insertAt($prevIndex + 1, new Token(';'));
}
}
示例14: fix
/**
* {@inheritdoc}
*/
public function fix(\SplFileInfo $file, Tokens $tokens)
{
$functionyTokens = $this->getFunctionyTokens();
foreach ($tokens as $index => $token) {
// looking for start brace
if (!$token->equals('(')) {
continue;
}
// last non-whitespace token
$lastTokenIndex = $tokens->getPrevNonWhitespace($index);
if (null === $lastTokenIndex) {
continue;
}
// check if it is a function call
if ($tokens[$lastTokenIndex]->isGivenKind($functionyTokens)) {
$this->fixFunctionCall($tokens, $index);
}
}
}
示例15: fixArray
/**
* Method to trim leading/trailing whitespace within single line arrays.
*
* @param Tokens $tokens
* @param int $index
*/
private static function fixArray(Tokens $tokens, $index)
{
$startIndex = $index;
if ($tokens[$startIndex]->isGivenKind(T_ARRAY)) {
$startIndex = $tokens->getNextMeaningfulToken($startIndex);
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
} else {
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $startIndex);
}
$nextToken = $tokens[$startIndex + 1];
if ($nextToken->isWhitespace(" \t")) {
$nextToken->clear();
}
$prevToken = $tokens[$endIndex - 1];
$prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($endIndex)];
if ($prevToken->isWhitespace(" \t") && !$prevNonWhitespaceToken->equals(',')) {
$prevToken->clear();
}
}