/**
  * {@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, ' ')));
             }
         }
     }
 }
 /**
  * @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();
         }
     }
 }
 /**
  * {@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(')')));
     }
 }
 /**
  * @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('');
     }
 }
 /**
  * {@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);
         }
     }
 }
 /**
  * {@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();
         }
     }
 }
Esempio n. 7
0
 /**
  * {@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;
         }
     }
 }
 /**
  * {@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);
         }
     }
 }
 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, ' ')));
     }
 }
 /**
  * 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, ' ')));
     }
 }
 /**
  * {@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");
         }
     }
 }
Esempio n. 12
0
 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);
 }
Esempio n. 13
0
 /**
  * {@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(';'));
     }
 }
 /**
  * {@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);
         }
     }
 }
 /**
  * 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();
     }
 }
 /**
  * @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;
         }
         $next = $tokens->getNextMeaningfulToken($index);
         if ($tokens[$next]->getContent() !== '(') {
             continue;
         }
         $tokens[$index]->setContent(self::$matching[$tokenContent]);
     }
 }
 /**
  * Method to trim leading/trailing whitespace within single line arrays.
  *
  * @param Tokens $tokens
  * @param int    $index
  */
 private static function fixArray(Tokens $tokens, $index)
 {
     static $whitespaceOptions = array('whitespaces' => " \t");
     $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_SQUARE_BRACE, $startIndex);
     }
     $nextToken = $tokens[$startIndex + 1];
     if ($nextToken->isWhitespace($whitespaceOptions)) {
         $nextToken->clear();
     }
     $prevToken = $tokens[$endIndex - 1];
     $prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($endIndex)];
     if ($prevToken->isWhitespace($whitespaceOptions) && !$prevNonWhitespaceToken->equals(',') && !($prevNonWhitespaceToken->isComment() && $prevNonWhitespaceToken->getContent() !== rtrim($prevNonWhitespaceToken->getContent()))) {
         $prevToken->clear();
     }
 }
Esempio n. 18
0
 private function findIncludies(Tokens $tokens)
 {
     static $includyTokenKinds = array(T_REQUIRE, T_REQUIRE_ONCE, T_INCLUDE, T_INCLUDE_ONCE);
     $includies = array();
     foreach ($tokens->findGivenKind($includyTokenKinds) as $includyTokens) {
         foreach ($includyTokens as $index => $token) {
             $includy = array('begin' => $index, 'braces' => null, 'end' => $tokens->getNextTokenOfKind($index, array(';')));
             // Don't remove when the statement is wrapped. include is also legal as function parameter
             // but requires being wrapped then
             if (!$tokens[$tokens->getPrevNonWhitespace($index)]->equals('(')) {
                 $nextTokenIndex = $tokens->getNextNonWhitespace($index);
                 $nextToken = $tokens[$nextTokenIndex];
                 if ($nextToken->equals('(')) {
                     $includy['braces'] = array('open' => $nextTokenIndex, 'close' => $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextTokenIndex));
                 }
             }
             $includies[] = $includy;
         }
     }
     return $includies;
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $limit = $tokens->count();
     for ($index = 0; $index < $limit; ++$index) {
         $token = $tokens[$index];
         // skip T_FOR parenthesis to ignore duplicated `;` like `for ($i = 1; ; ++$i) {...}`
         if ($token->isGivenKind(T_FOR)) {
             $index = $tokens->getNextMeaningfulToken($index);
             $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
             continue;
         }
         if (!$token->equals(';')) {
             continue;
         }
         $prevIndex = $tokens->getPrevNonWhitespace($index);
         if (!$tokens[$prevIndex]->equals(';')) {
             continue;
         }
         $tokens->removeLeadingWhitespace($index);
         $token->clear();
     }
 }
 /**
  * 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();
     }
 }
 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 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();
         }
     }
     $nextToken = $tokens[$index + 1];
     // Two cases for fix space after comma (exclude multiline comments)
     //  1) multiple spaces after comma
     //  2) no space after comma
     if ($nextToken->isWhitespace()) {
         if ($this->isCommentLastLineToken($tokens, $index + 2)) {
             return;
         }
         $newContent = ltrim($nextToken->getContent(), " \t");
         if ('' === $newContent) {
             $newContent = ' ';
         }
         $nextToken->setContent($newContent);
         return;
     }
     if (!$this->isCommentLastLineToken($tokens, $index + 1)) {
         $tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, ' ')));
     }
 }
Esempio n. 23
0
 private function detectIndent(Tokens $tokens, $index)
 {
     static $goBackTokens = array(T_ABSTRACT, T_FINAL, T_PUBLIC, T_PROTECTED, T_PRIVATE, T_STATIC);
     $token = $tokens[$index];
     if ($token->isGivenKind($goBackTokens) || $token->isClassy() || $token->isGivenKind(T_FUNCTION)) {
         $prevIndex = $tokens->getPrevNonWhitespace($index);
         $prevToken = $tokens[$prevIndex];
         if ($prevToken->isGivenKind($goBackTokens)) {
             return $this->detectIndent($tokens, $prevIndex);
         }
     }
     $prevIndex = $index - 1;
     $prevToken = $tokens[$prevIndex];
     if ($prevToken->equals('}')) {
         return $this->detectIndent($tokens, $prevIndex);
     }
     // if can not detect indent:
     if (!$prevToken->isWhitespace()) {
         return '';
     }
     $explodedContent = explode("\n", $prevToken->getContent());
     // proper decect indent for code: `    } else {`
     if (1 === count($explodedContent)) {
         if ($tokens[$index - 2]->equals('}')) {
             return $this->detectIndent($tokens, $index - 2);
         }
     }
     return end($explodedContent);
 }
 /**
  * @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 ($tokenContent !== self::STRING_MATCH) {
             continue;
         }
         $prevIndex = $tokens->getPrevNonWhitespace($index);
         if (in_array($tokens[$prevIndex]->getId(), $wrongTokens, true)) {
             continue;
         }
         $nextIndex = $tokens->getNextMeaningfulToken($index);
         if ($tokens[$nextIndex]->getContent() !== '(') {
             continue;
         }
         $lastIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $nextIndex);
         $needsBrackets = false;
         if ($tokens[$prevIndex]->isCast() || $tokens[$prevIndex]->isGivenKind([T_IS_NOT_EQUAL, T_IS_EQUAL, T_IS_IDENTICAL, T_IS_NOT_IDENTICAL])) {
             $needsBrackets = true;
         }
         $endBraceIndex = $tokens->getNextTokenOfKind($nextIndex, [')']);
         $nextEndBraceIndex = $tokens->getNextMeaningfulToken($endBraceIndex);
         if ($tokens[$nextEndBraceIndex]->isGivenKind([T_IS_NOT_EQUAL, T_IS_EQUAL, T_IS_IDENTICAL, T_IS_NOT_IDENTICAL])) {
             $needsBrackets = true;
         }
         // Special fix: true/false === is_null() => !==/=== null
         if ($this->isFixableComparison($tokens, $prevIndex, $nextEndBraceIndex)) {
             $needsBrackets = false;
         }
         $negated = false;
         if ($tokens[$prevIndex]->getContent() === '!') {
             $negated = true;
         }
         $replacement = '';
         for ($i = $nextIndex + 1; $i < $lastIndex; ++$i) {
             // We should only change trivial cases to avoid changing code behavior
             if (!$tokens[$i]->isGivenKind([T_VARIABLE, T_OBJECT_OPERATOR, T_STRING, T_CONST, T_DOUBLE_COLON, T_CONSTANT_ENCAPSED_STRING, T_LNUMBER])) {
                 continue 2;
             }
             $replacement .= $tokens[$i]->getContent();
         }
         if ($this->startIndex !== null) {
             $index = $this->startIndex;
             $this->endIndex = $lastIndex;
             $negated = $tokens[$this->startIndex]->getContent() === 'false' ? true : false;
             $needsBrackets = false;
         }
         if ($this->endIndex !== null) {
             $lastIndex = $this->endIndex;
             if ($this->startIndex !== null) {
                 $token = $tokens[$this->startIndex];
             } else {
                 $token = $tokens[$this->endIndex];
             }
             $negated = $token->getContent() === 'false' ? true : false;
             $needsBrackets = false;
         }
         $replacement .= ' ' . ($negated ? '!' : '=') . '== null';
         if ($needsBrackets) {
             $replacement = '(' . $replacement . ')';
         }
         $offset = 0;
         if ($negated && $this->startIndex === null && $this->endIndex === null) {
             $offset = -($index - $prevIndex);
         }
         $index += $offset;
         for ($i = $index; $i < $lastIndex; ++$i) {
             $tokens[$i]->clear();
         }
         $tokens[$lastIndex]->setContent($replacement);
     }
 }
 /**
  * Method to remove incorrect default values.
  *
  * @param Tokens $tokens
  * @param int $index
  * @param int $endIndex
  *
  * @return void
  */
 protected function fixDefaultValues(Tokens $tokens, $index, $endIndex)
 {
     $hasDefaultValue = false;
     for ($i = $endIndex; $i > $index; $i--) {
         if ($tokens[$i]->equals('=')) {
             $hasDefaultValue = true;
             break;
         }
     }
     if (!$hasDefaultValue) {
         $this->defaultValueForbidden = true;
         return;
     }
     if ($hasDefaultValue && !$this->defaultValueForbidden) {
         return;
     }
     $positionOfEqualSign = $i;
     // Remove wrong default value
     for ($i = $positionOfEqualSign; $i <= $endIndex; $i++) {
         if ($tokens[$i]->equals(',')) {
             break;
         }
         $tokens[$i]->clear();
     }
     // Also remove whitespace then before the equal sign
     $prevIndex = $tokens->getPrevNonWhitespace($positionOfEqualSign);
     for ($i = $positionOfEqualSign - 1; $i > $prevIndex; $i--) {
         $tokens[$i]->clear();
     }
 }
 /**
  * Returns an array with `implements` data.
  *
  * Returns array:
  * * int  'breakAt'            index of the Token of type T_IMPLEMENTS for the definition, or 0
  * * int  'numberOfInterfaces'
  * * bool 'multiLine'
  *
  * @param Tokens $tokens
  * @param int    $start
  * @param int    $classyOpen
  *
  * @return array
  */
 private function getMultiLineInfo(Tokens $tokens, $start, $classyOpen)
 {
     $implementsInfo = array('breakAt' => 0, 'numberOfInterfaces' => 0, 'multiLine' => false);
     $breakAtToken = $tokens->findGivenKind($tokens[$start]->isGivenKind(T_INTERFACE) ? T_EXTENDS : T_IMPLEMENTS, $start, $classyOpen);
     if (count($breakAtToken) < 1) {
         return $implementsInfo;
     }
     $implementsInfo['breakAt'] = key($breakAtToken);
     $classyOpen = $tokens->getPrevNonWhitespace($classyOpen);
     for ($j = $implementsInfo['breakAt'] + 1; $j < $classyOpen; ++$j) {
         if ($tokens[$j]->isGivenKind(T_STRING)) {
             ++$implementsInfo['numberOfInterfaces'];
             continue;
         }
         if (!$implementsInfo['multiLine'] && ($tokens[$j]->isWhitespace() || $tokens[$j]->isComment()) && false !== strpos($tokens[$j]->getContent(), "\n")) {
             $implementsInfo['multiLine'] = true;
         }
     }
     return $implementsInfo;
 }