getNextNonWhitespace() public method

This method is shorthand for getNonWhitespaceSibling method.
public getNextNonWhitespace ( integer $index, null | string $whitespaces = null ) : integer | null
$index integer token index
$whitespaces null | string whitespaces characters for Token::isWhitespace
return integer | null
Exemplo n.º 1
0
 /**
  * Replace all `else if` (T_ELSE T_IF) with `elseif` (T_ELSEIF).
  *
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(T_ELSE)) {
             continue;
         }
         $nextIndex = $tokens->getNextNonWhitespace($index);
         $nextToken = $tokens[$nextIndex];
         // if next meaning token is not T_IF - continue searching, this is not the case for fixing
         if (!$nextToken->isGivenKind(T_IF)) {
             continue;
         }
         // now we have T_ELSE following by T_IF so we could fix this
         // 1. clear whitespaces between T_ELSE and T_IF
         $tokens[$index + 1]->clear();
         // 2. change token from T_ELSE into T_ELSEIF
         $tokens->overrideAt($index, array(T_ELSEIF, 'elseif'));
         // 3. clear succeeding T_IF
         $nextToken->clear();
     }
     // handle `T_ELSE T_WHITESPACE T_IF` treated as single `T_ELSEIF` by HHVM
     // see https://github.com/facebook/hhvm/issues/4796
     if (defined('HHVM_VERSION')) {
         foreach ($tokens->findGivenKind(T_ELSEIF) as $token) {
             $token->setContent('elseif');
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = $tokens->count() - 3; $index > 0; --$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(T_NEW)) {
             continue;
         }
         $nextIndex = $tokens->getNextTokenOfKind($index, array(':', ';', ',', '(', ')', '[', ']', array(T_CLOSE_TAG), 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->isWhitespace()) {
             $nextIndex = $tokens->getNextNonWhitespace($nextIndex);
             $nextToken = $tokens[$nextIndex];
         }
         // new statement with () - nothing to do
         if ($nextToken->equals('(')) {
             continue;
         }
         $meaningBeforeNextIndex = $tokens->getPrevMeaningfulToken($nextIndex);
         $tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')')));
     }
 }
 /**
  * Fix the line breaks per group.
  *
  * For each use token reach the nearest ; and ensure every
  * token after has one \n before next non empty token (next line).
  * It skips the first pass from the bottom.
  *
  * @param Tokens $tokens
  * @param array  $uses
  */
 private function fixLineBreaksPerImportGroup(Tokens $tokens, array $uses)
 {
     foreach ($uses as $index) {
         $endIndex = $tokens->getNextTokenOfKind($index, array(';'));
         $afterSemicolonIndex = $tokens->getNextNonWhitespace($endIndex);
         if (null !== $afterSemicolonIndex && !$tokens[$afterSemicolonIndex]->isGivenKind(T_USE)) {
             continue;
         }
         $nextToken = $tokens[$endIndex + 1];
         if ($nextToken->isWhitespace()) {
             $nextToken->setContent(preg_replace('/\\n{2,}/', "\n", $nextToken->getContent()));
         }
     }
 }
 /**
  * {@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");
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     static $forbiddenSuccessors = array(T_DOC_COMMENT, T_COMMENT, T_WHITESPACE, T_RETURN, T_THROW, T_GOTO, T_CONTINUE, T_BREAK);
     foreach ($tokens as $index => $token) {
         if (!$token->isGivenKind(T_DOC_COMMENT)) {
             continue;
         }
         // get the next non-whitespace token inc comments, provided
         // that there is whitespace between it and the current token
         $next = $tokens->getNextNonWhitespace($index);
         if ($index + 2 === $next && false === $tokens[$next]->isGivenKind($forbiddenSuccessors)) {
             $this->fixWhitespace($tokens[$index + 1]);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($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);
         $startBraceIndex = $tokens->getNextTokenOfKind($endParenthesisIndex, array(';', '{'));
         $startBraceToken = $tokens[$startBraceIndex];
         if ($startBraceToken->equals('{')) {
             // fix single-line whitespace before {
             // eg: `function foo(){}` => `function foo() {}`
             // eg: `function foo()   {}` => `function foo() {}`
             if (!$tokens[$startBraceIndex - 1]->isWhitespace() || $tokens[$startBraceIndex - 1]->isWhitespace($this->singleLineWhitespaceOptions)) {
                 $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, ' ');
             }
         }
         $afterParenthesisIndex = $tokens->getNextNonWhitespace($endParenthesisIndex);
         $afterParenthesisToken = $tokens[$afterParenthesisIndex];
         if ($afterParenthesisToken->isGivenKind(CT_USE_LAMBDA)) {
             $useStartParenthesisIndex = $tokens->getNextTokenOfKind($afterParenthesisIndex, array('('));
             $useEndParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $useStartParenthesisIndex);
             // fix whitespace after CT_USE_LAMBDA
             $tokens->ensureWhitespaceAtIndex($afterParenthesisIndex + 1, 0, ' ');
             // remove single-line edge whitespaces inside use parentheses
             $this->fixParenthesisInnerEdge($tokens, $useStartParenthesisIndex, $useEndParenthesisIndex);
             // fix whitespace before CT_USE_LAMBDA
             $tokens->ensureWhitespaceAtIndex($afterParenthesisIndex - 1, 1, ' ');
         }
         // remove single-line edge whitespaces inside parameters list parentheses
         $this->fixParenthesisInnerEdge($tokens, $startParenthesisIndex, $endParenthesisIndex);
         if (!$tokensAnalyzer->isLambda($index)) {
             // remove whitespace before (
             // eg: `function foo () {}` => `function foo() {}`
             if ($tokens[$startParenthesisIndex - 1]->isWhitespace()) {
                 $tokens[$startParenthesisIndex - 1]->clear();
             }
         }
         // fix whitespace after T_FUNCTION
         // eg: `function     foo() {}` => `function foo() {}`
         $tokens->ensureWhitespaceAtIndex($index + 1, 0, ' ');
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $foundNamespace = $tokens->findGivenKind(T_NAMESPACE);
     if (empty($foundNamespace)) {
         return;
     }
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     $firstNamespaceIdx = key($foundNamespace);
     $usesIdxs = $tokensAnalyzer->getImportUseIndexes();
     foreach ($usesIdxs as $idx) {
         if ($idx < $firstNamespaceIdx) {
             continue;
         }
         $nextTokenIdx = $tokens->getNextNonWhitespace($idx);
         $nextToken = $tokens[$nextTokenIdx];
         if ($nextToken->isGivenKind(T_NS_SEPARATOR)) {
             $nextToken->clear();
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     for ($index = $tokens->count() - 1; $index >= 0; --$index) {
         if (!$tokensAnalyzer->isBinaryOperator($index)) {
             continue;
         }
         // skip `declare(foo ==bar)`
         $prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($index);
         if ($tokens[$prevMeaningfulIndex]->isGivenKind(T_STRING)) {
             $prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($prevMeaningfulIndex);
             if ($tokens[$prevMeaningfulIndex]->equals('(')) {
                 $prevMeaningfulIndex = $tokens->getPrevMeaningfulToken($prevMeaningfulIndex);
                 if ($tokens[$prevMeaningfulIndex]->isGivenKind(T_DECLARE)) {
                     continue;
                 }
             }
         }
         // fix white space after operator
         if ($tokens[$index + 1]->isWhitespace()) {
             $content = $tokens[$index + 1]->getContent();
             if (' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getNextNonWhitespace($index + 1)]->isComment()) {
                 $tokens[$index + 1]->setContent(' ');
             }
         } else {
             $tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
         }
         // fix white space before operator
         if ($tokens[$index - 1]->isWhitespace()) {
             $content = $tokens[$index - 1]->getContent();
             if (' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment()) {
                 $tokens[$index - 1]->setContent(' ');
             }
         } else {
             $tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
         }
         --$index;
         // skip check for binary operator on the whitespace token that is fixed.
     }
 }
 private function fixWhiteSpaceAroundOperator(Tokens $tokens, $index)
 {
     // do not change the alignment of `=>` or `=`, see `(un)align_double_arrow`, `(un)align_equals`
     $preserveAlignment = $tokens[$index]->isGivenKind(T_DOUBLE_ARROW) || $tokens[$index]->equals('=');
     // fix white space after operator
     if ($tokens[$index + 1]->isWhitespace()) {
         $content = $tokens[$index + 1]->getContent();
         if (!$preserveAlignment && ' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getNextNonWhitespace($index + 1)]->isComment()) {
             $tokens[$index + 1]->setContent(' ');
         }
     } else {
         $tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
     }
     // fix white space before operator
     if ($tokens[$index - 1]->isWhitespace()) {
         $content = $tokens[$index - 1]->getContent();
         if (!$preserveAlignment && ' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment()) {
             $tokens[$index - 1]->setContent(' ');
         }
     } else {
         $tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
     }
 }
 /**
  * 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];
     $nextNonWhitespaceIndex = $tokens->getNextNonWhitespace($startIndex);
     $nextNonWhitespaceToken = $tokens[$nextNonWhitespaceIndex];
     $tokenAfterNextNonWhitespaceToken = $tokens[$nextNonWhitespaceIndex + 1];
     $prevToken = $tokens[$endIndex - 1];
     $prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($endIndex);
     $prevNonWhitespaceToken = $tokens[$prevNonWhitespaceIndex];
     if ($nextToken->isWhitespace(" \t") && (!$nextNonWhitespaceToken->isComment() || $nextNonWhitespaceIndex === $prevNonWhitespaceIndex || $tokenAfterNextNonWhitespaceToken->isWhitespace(" \t") || '/*' === substr($nextNonWhitespaceToken->getContent(), 0, 2))) {
         $nextToken->clear();
     }
     if ($prevToken->isWhitespace(" \t") && !$prevNonWhitespaceToken->equals(',')) {
         $prevToken->clear();
     }
 }
Exemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     static $nextTokenKinds = null;
     if (null === $nextTokenKinds) {
         $nextTokenKinds = array('?', ';', ',', '(', ')', '[', ']', ':', '<', '>', '+', '-', '*', '/', '%', '&', '^', '|', array(T_IS_SMALLER_OR_EQUAL), array(T_IS_GREATER_OR_EQUAL), array(T_IS_EQUAL), array(T_IS_NOT_EQUAL), array(T_IS_IDENTICAL), array(T_IS_NOT_IDENTICAL), array(T_CLOSE_TAG), array(T_LOGICAL_AND), array(T_LOGICAL_OR), array(T_LOGICAL_XOR), array(T_BOOLEAN_AND), array(T_BOOLEAN_OR), array(T_INC), array(T_DEC), array(T_SL), array(T_SR), array(T_INSTANCEOF), array(T_AS), array(T_DOUBLE_ARROW), array(CT_ARRAY_SQUARE_BRACE_OPEN), array(CT_ARRAY_SQUARE_BRACE_CLOSE), array(CT_BRACE_CLASS_INSTANTIATION_OPEN), array(CT_BRACE_CLASS_INSTANTIATION_CLOSE));
         if (defined('T_POW')) {
             $nextTokenKinds[] = array(T_POW);
         }
         if (defined('T_SPACESHIP')) {
             $nextTokenKinds[] = array(T_SPACESHIP);
         }
     }
     for ($index = $tokens->count() - 3; $index > 0; --$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(T_NEW)) {
             continue;
         }
         $nextIndex = $tokens->getNextTokenOfKind($index, $nextTokenKinds);
         $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->isWhitespace()) {
             $nextIndex = $tokens->getNextNonWhitespace($nextIndex);
             $nextToken = $tokens[$nextIndex];
         }
         // new statement with () - nothing to do
         if ($nextToken->equals('(')) {
             continue;
         }
         $meaningBeforeNextIndex = $tokens->getPrevMeaningfulToken($nextIndex);
         $tokens->insertAt($meaningBeforeNextIndex + 1, array(new Token('('), new Token(')')));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $ternaryLevel = 0;
     foreach ($tokens as $index => $token) {
         if ($token->isArray()) {
             continue;
         }
         if ($token->equals('?')) {
             ++$ternaryLevel;
             $nextNonWhitespaceIndex = $tokens->getNextNonWhitespace($index);
             $nextNonWhitespaceToken = $tokens[$nextNonWhitespaceIndex];
             if ($nextNonWhitespaceToken->equals(':')) {
                 // for `$a ?: $b` remove spaces between `?` and `:`
                 if ($tokens[$index + 1]->isWhitespace()) {
                     $tokens[$index + 1]->clear();
                 }
             } else {
                 // for `$a ? $b : $c` ensure space after `?`
                 $this->ensureWhitespaceExistence($tokens, $index + 1, true);
             }
             // for `$a ? $b : $c` ensure space before `?`
             $this->ensureWhitespaceExistence($tokens, $index - 1, false);
             continue;
         }
         if ($ternaryLevel && $token->equals(':')) {
             // for `$a ? $b : $c` ensure space after `:`
             $this->ensureWhitespaceExistence($tokens, $index + 1, true);
             $prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)];
             if (!$prevNonWhitespaceToken->equals('?')) {
                 // for `$a ? $b : $c` ensure space before `:`
                 $this->ensureWhitespaceExistence($tokens, $index - 1, false);
             }
             --$ternaryLevel;
         }
     }
 }
Exemplo n.º 13
0
 /**
  * @param Tokens $tokens
  * @param int    $structureTokenIndex
  *
  * @return int
  */
 private function findParenthesisEnd(Tokens $tokens, $structureTokenIndex)
 {
     $nextIndex = $tokens->getNextNonWhitespace($structureTokenIndex);
     $nextToken = $tokens[$nextIndex];
     // return if next token is not opening parenthesis
     if (!$nextToken->equals('(')) {
         return $structureTokenIndex;
     }
     return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextIndex);
 }
Exemplo n.º 14
0
 /**
  * @param Tokens $tokens
  * @param int    $classEnd
  * @param int    $methodEnd
  */
 private function fixSpaceBelowMethod(Tokens $tokens, $classEnd, $methodEnd)
 {
     $nextNotWhite = $tokens->getNextNonWhitespace($methodEnd);
     $this->correctLineBreaks($tokens, $methodEnd, $nextNotWhite, $nextNotWhite === $classEnd ? 1 : 2);
 }
 /**
  * Find the index where the header comment must be inserted.
  *
  * @param Tokens $tokens
  *
  * @return int
  */
 private function findHeaderCommentInsertionIndex(Tokens $tokens)
 {
     $index = $tokens->getNextNonWhitespace(0);
     if (null === $index) {
         // empty file, insert at the end
         $index = $tokens->getSize();
     }
     return $index;
 }
 /**
  * @param Tokens $tokens
  * @param int    $index
  */
 private function fixWhiteSpaceAroundOperator(Tokens $tokens, $index)
 {
     if ($tokens[$index]->isGivenKind(T_DOUBLE_ARROW)) {
         if (true === $this->configuration['align_double_arrow']) {
             if (!isset($this->alignFixerHelpers['align_double_arrow'])) {
                 $this->alignFixerHelpers['align_double_arrow'] = new AlignDoubleArrowFixerHelper();
             }
             return;
         } elseif (null === $this->configuration['align_double_arrow']) {
             return;
             // configured not to touch the whitespace around the operator
         }
     } elseif ($tokens[$index]->equals('=')) {
         if (true === $this->configuration['align_equals']) {
             if (!isset($this->alignFixerHelpers['align_equals'])) {
                 $this->alignFixerHelpers['align_equals'] = new AlignEqualsFixerHelper();
             }
             return;
         } elseif (null === $this->configuration['align_equals']) {
             return;
             // configured not to touch the whitespace around the operator
         }
     }
     // fix white space after operator
     if ($tokens[$index + 1]->isWhitespace()) {
         $content = $tokens[$index + 1]->getContent();
         if (' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getNextNonWhitespace($index + 1)]->isComment()) {
             $tokens[$index + 1]->setContent(' ');
         }
     } else {
         $tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
     }
     // fix white space before operator
     if ($tokens[$index - 1]->isWhitespace()) {
         $content = $tokens[$index - 1]->getContent();
         if (' ' !== $content && false === strpos($content, "\n") && !$tokens[$tokens->getPrevNonWhitespace($index - 1)]->isComment()) {
             $tokens[$index - 1]->setContent(' ');
         }
     } else {
         $tokens->insertAt($index, new Token(array(T_WHITESPACE, ' ')));
     }
 }
Exemplo n.º 17
0
 /**
  * @param Tokens $tokens
  * @param int    $index  index of concatenation '.' token
  */
 private function fixConcatenationToNoSpace(Tokens $tokens, $index)
 {
     if (!$tokens[$tokens->getPrevNonWhitespace($index)]->isGivenKind(T_LNUMBER)) {
         $tokens->removeLeadingWhitespace($index, " \t");
     }
     if (!$tokens[$tokens->getNextNonWhitespace($index)]->isGivenKind(array(T_LNUMBER, T_COMMENT, T_DOC_COMMENT))) {
         $tokens->removeTrailingWhitespace($index, " \t");
     }
 }
 /**
  * Find the header comment index.
  *
  * @param Tokens $tokens
  * @param int    $headerNewIndex
  *
  * @return int|null
  */
 private function findHeaderCommentCurrentIndex(Tokens $tokens, $headerNewIndex)
 {
     $index = $tokens->getNextNonWhitespace($headerNewIndex);
     return null === $index || !$tokens[$index]->isComment() ? null : $index;
 }
 /**
  * @param array<string, string> $map
  * @param Tokens                $tokens
  * @param int                   $index
  * @param string                $method
  *
  * @return int|null
  */
 private function fixAssert(array $map, Tokens $tokens, $index, $method)
 {
     $sequence = $tokens->findSequence(array(array(T_VARIABLE, '$this'), array(T_OBJECT_OPERATOR, '->'), array(T_STRING, $method), '('), $index);
     if (null === $sequence) {
         return;
     }
     $sequenceIndexes = array_keys($sequence);
     $sequenceIndexes[4] = $tokens->getNextMeaningfulToken($sequenceIndexes[3]);
     $firstParameterToken = $tokens[$sequenceIndexes[4]];
     if (!$firstParameterToken->isNativeConstant()) {
         return;
     }
     $sequenceIndexes[5] = $tokens->getNextMeaningfulToken($sequenceIndexes[4]);
     // return if first method argument is an expression, not value
     if (!$tokens[$sequenceIndexes[5]]->equals(',')) {
         return;
     }
     $tokens[$sequenceIndexes[2]]->setContent($map[$firstParameterToken->getContent()]);
     $tokens->clearRange($sequenceIndexes[4], $tokens->getNextNonWhitespace($sequenceIndexes[5]) - 1);
     return $sequenceIndexes[5];
 }