isLambda() публичный Метод

Check if the function under given index is a lambda.
public isLambda ( integer $index ) : boolean
$index integer
Результат boolean
Пример #1
0
 /**
  * Replace occurrences of the name of the classy element by "self" (if possible).
  *
  * @param Tokens $tokens
  * @param string $name
  * @param int    $startIndex
  * @param int    $endIndex
  */
 private function replaceNameOccurrences(Tokens $tokens, $name, $startIndex, $endIndex)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     for ($i = $startIndex; $i < $endIndex; ++$i) {
         $token = $tokens[$i];
         // skip lambda functions (PHP < 5.4 compatibility)
         if ($token->isGivenKind(T_FUNCTION) && $tokensAnalyzer->isLambda($i)) {
             $i = $tokens->getNextTokenOfKind($i, array('{'));
             $i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $i);
             continue;
         }
         if (!$token->equals(array(T_STRING, $name), false)) {
             continue;
         }
         $prevToken = $tokens[$tokens->getPrevMeaningfulToken($i)];
         $nextToken = $tokens[$tokens->getNextMeaningfulToken($i)];
         // skip tokens that are part of a fully qualified name
         if ($prevToken->isGivenKind(T_NS_SEPARATOR) || $nextToken->isGivenKind(T_NS_SEPARATOR)) {
             continue;
         }
         if ($prevToken->isGivenKind(array(T_INSTANCEOF, T_NEW)) || $nextToken->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM)) {
             $token->setContent('self');
         }
     }
 }
Пример #2
0
 /**
  * @dataProvider provideIsLambdaCases
  */
 public function testIsLambda($source, array $expected)
 {
     $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
     foreach ($expected as $index => $isLambda) {
         $this->assertSame($isLambda, $tokensAnalyzer->isLambda($index));
     }
 }
 private function fixAfterToken($index)
 {
     for ($i = $index - 1; $i > 0; --$i) {
         if ($this->tokens[$i]->isGivenKind(T_FUNCTION) && $this->tokensAnalyzer->isLambda($i)) {
             return;
         }
         if ($this->tokens[$i]->isGivenKind(T_CLASS) && $this->tokensAnalyzer->isAnonymousClass($i)) {
             return;
         }
         if ($this->tokens[$i]->isWhitespace() && false !== strpos($this->tokens[$i]->getContent(), "\n")) {
             break;
         }
     }
     $this->removeEmptyLinesAfterLineWithTokenAt($index);
 }
 /**
  * @param Tokens         $tokens
  * @param TokensAnalyzer $tokensAnalyzer
  * @param int            $classStart
  * @param int            $classEnd
  */
 private function fixClass(Tokens $tokens, TokensAnalyzer $tokensAnalyzer, $classStart, $classEnd)
 {
     for ($index = $classEnd; $index > $classStart; --$index) {
         if (!$tokens[$index]->isGivenKind(T_FUNCTION) || $tokensAnalyzer->isLambda($index)) {
             continue;
         }
         $attributes = $tokensAnalyzer->getMethodAttributes($index);
         if (true === $attributes['abstract']) {
             $methodEnd = $tokens->getNextTokenOfKind($index, array(';'));
         } else {
             $methodStart = $tokens->getNextTokenOfKind($index, array('{'));
             $methodEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $methodStart, true);
         }
         $this->fixSpaceBelowMethod($tokens, $classEnd, $methodEnd);
         $this->fixSpaceAboveMethod($tokens, $classStart, $index);
     }
 }
 /**
  * {@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, ' ');
     }
 }
Пример #6
0
 private function fixLambdas(Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     for ($index = $tokens->count() - 1; 0 <= $index; --$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(T_FUNCTION) || !$tokensAnalyzer->isLambda($index)) {
             continue;
         }
         $nextIndex = $tokens->getNextTokenOfKind($index, array('{'));
         $tokens->ensureWhitespaceAtIndex($nextIndex - 1, 1, ' ');
     }
 }
Пример #7
0
 private function fixIndents(Tokens $tokens)
 {
     $classyTokens = Token::getClassyTokenKinds();
     $classyAndFunctionTokens = array_merge(array(T_FUNCTION), $classyTokens);
     $controlTokens = $this->getControlTokens();
     $indentTokens = array_filter(array_merge($classyAndFunctionTokens, $controlTokens), function ($item) {
         return T_SWITCH !== $item;
     });
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     for ($index = 0, $limit = count($tokens); $index < $limit; ++$index) {
         $token = $tokens[$index];
         // if token is not a structure element - continue
         if (!$token->isGivenKind($indentTokens)) {
             continue;
         }
         // do not change indent for `while` in `do ... while ...`
         if ($token->isGivenKind(T_WHILE) && $tokensAnalyzer->isWhilePartOfDoWhile($index)) {
             continue;
         }
         // do not change import of functions
         if ($token->isGivenKind(T_FUNCTION) && $tokens[$tokens->getPrevMeaningfulToken($index)]->isGivenKind(T_USE)) {
             continue;
         }
         if ($token->isGivenKind($classyAndFunctionTokens)) {
             $startBraceIndex = $tokens->getNextTokenOfKind($index, array(';', '{'));
             $startBraceToken = $tokens[$startBraceIndex];
         } else {
             $parenthesisEndIndex = $this->findParenthesisEnd($tokens, $index);
             $startBraceIndex = $tokens->getNextNonWhitespace($parenthesisEndIndex);
             $startBraceToken = $tokens[$startBraceIndex];
         }
         // structure without braces block - nothing to do, e.g. do { } while (true);
         if (!$startBraceToken->equals('{')) {
             continue;
         }
         $endBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startBraceIndex);
         $indent = $this->detectIndent($tokens, $index);
         // fix indent near closing brace
         $tokens->ensureWhitespaceAtIndex($endBraceIndex - 1, 1, "\n" . $indent);
         // fix indent between braces
         $lastCommaIndex = $tokens->getPrevTokenOfKind($endBraceIndex - 1, array(';', '}'));
         $nestLevel = 1;
         for ($nestIndex = $lastCommaIndex; $nestIndex >= $startBraceIndex; --$nestIndex) {
             $nestToken = $tokens[$nestIndex];
             if ($nestToken->equals(')')) {
                 $nestIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nestIndex, false);
                 continue;
             }
             if (1 === $nestLevel && $nestToken->equalsAny(array(';', '}'))) {
                 $nextNonWhitespaceNestIndex = $tokens->getNextNonWhitespace($nestIndex);
                 $nextNonWhitespaceNestToken = $tokens[$nextNonWhitespaceNestIndex];
                 if (!$nextNonWhitespaceNestToken->isComment() && !($nestToken->equals('}') && $nextNonWhitespaceNestToken->equalsAny(array(';', ',', ']', array(CT_ARRAY_SQUARE_BRACE_CLOSE)))) && !($nestToken->equals('}') && $nextNonWhitespaceNestToken->equals('(')) && !($nestToken->equals('}') && $tokens[$nestIndex - 1]->equalsAny(array('"', "'", array(T_CONSTANT_ENCAPSED_STRING))))) {
                     if ($nextNonWhitespaceNestToken->isGivenKind($this->getControlContinuationTokens()) || $nextNonWhitespaceNestToken->isGivenKind(T_CLOSE_TAG) || $nextNonWhitespaceNestToken->isGivenKind(T_WHILE) && $tokensAnalyzer->isWhilePartOfDoWhile($nextNonWhitespaceNestIndex)) {
                         $whitespace = ' ';
                     } else {
                         $nextToken = $tokens[$nestIndex + 1];
                         $nextWhitespace = '';
                         if ($nextToken->isWhitespace()) {
                             $nextWhitespace = rtrim($nextToken->getContent(), " \t");
                             if (strlen($nextWhitespace) && "\n" === $nextWhitespace[strlen($nextWhitespace) - 1]) {
                                 $nextWhitespace = substr($nextWhitespace, 0, -1);
                             }
                         }
                         $whitespace = $nextWhitespace . "\n" . $indent;
                         if (!$nextNonWhitespaceNestToken->equals('}')) {
                             $whitespace .= '    ';
                         }
                     }
                     $tokens->ensureWhitespaceAtIndex($nestIndex + 1, 0, $whitespace);
                 }
             }
             if ($nestToken->equals('}')) {
                 ++$nestLevel;
                 continue;
             }
             if ($nestToken->equals('{')) {
                 --$nestLevel;
                 continue;
             }
         }
         // fix indent near opening brace
         if (isset($tokens[$startBraceIndex + 2]) && $tokens[$startBraceIndex + 2]->equals('}')) {
             $tokens->ensureWhitespaceAtIndex($startBraceIndex + 1, 0, "\n" . $indent);
         } else {
             $nextToken = $tokens[$startBraceIndex + 1];
             $nextNonWhitespaceToken = $tokens[$tokens->getNextNonWhitespace($startBraceIndex)];
             // set indent only if it is not a case, when comment is following { in same line
             if (!$nextNonWhitespaceToken->isComment() || !($nextToken->isWhitespace() && $nextToken->isWhitespace(" \t")) && substr_count($nextToken->getContent(), "\n") === 1) {
                 $tokens->ensureWhitespaceAtIndex($startBraceIndex + 1, 0, "\n" . $indent . '    ');
             }
         }
         if ($token->isGivenKind($classyTokens) && !$tokensAnalyzer->isAnonymousClass($index)) {
             $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, "\n" . $indent);
         } elseif ($token->isGivenKind(T_FUNCTION) && !$tokensAnalyzer->isLambda($index)) {
             $closingParenthesisIndex = $tokens->getPrevTokenOfKind($startBraceIndex, array(')'));
             if (null === $closingParenthesisIndex) {
                 continue;
             }
             $prevToken = $tokens[$closingParenthesisIndex - 1];
             if ($prevToken->isWhitespace() && false !== strpos($prevToken->getContent(), "\n")) {
                 if (!$tokens[$startBraceIndex - 2]->isComment()) {
                     $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, ' ');
                 }
             } else {
                 $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, "\n" . $indent);
             }
         } else {
             $tokens->ensureWhitespaceAtIndex($startBraceIndex - 1, 1, ' ');
         }
         // reset loop limit due to collection change
         $limit = count($tokens);
     }
 }