/** * {@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, ' '))); } } } }
/** * 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; } $ifTokenIndex = $tokens->getNextMeaningfulToken($index); $beforeIfTokenIndex = $tokens->getPrevNonWhitespace($ifTokenIndex); // if next meaning token is not T_IF - continue searching, this is not the case for fixing if (!$tokens[$ifTokenIndex]->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 $tokens[$ifTokenIndex]->clear(); // 4. clear extra whitespace after T_IF in T_COMMENT,T_WHITESPACE?,T_IF,T_WHITESPACE sequence if ($tokens[$beforeIfTokenIndex]->isComment() && $tokens[$ifTokenIndex + 1]->isWhitespace()) { $tokens[$ifTokenIndex + 1]->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) { foreach ($tokens as $index => $token) { if (!$token->equalsAny(array('[', array(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)))) { continue; } if (in_array('inside', $this->configuration, true)) { if ($token->equals('[')) { $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_INDEX_SQUARE_BRACE, $index); } else { $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_INDEX_CURLY_BRACE, $index); } // remove space after opening `[` or `{` $this->removeWhitespaceToken($tokens[$index + 1]); // remove space before closing `]` or `}` $this->removeWhitespaceToken($tokens[$endIndex - 1]); } if (in_array('outside', $this->configuration, true)) { $prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($index); if ($tokens[$prevNonWhitespaceIndex]->isComment()) { continue; } $tokens->removeLeadingWhitespace($index); } } }
/** * 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; } $ifTokenIndex = $tokens->getNextMeaningfulToken($index); $beforeIfTokenIndex = $tokens->getPrevNonWhitespace($ifTokenIndex); // if next meaning token is not T_IF - continue searching, this is not the case for fixing if (!$tokens[$ifTokenIndex]->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 $tokens[$ifTokenIndex]->clear(); // 4. clear extra whitespace after T_IF in T_COMMENT,T_WHITESPACE?,T_IF,T_WHITESPACE sequence if ($tokens[$beforeIfTokenIndex]->isComment() && $tokens[$ifTokenIndex + 1]->isWhitespace()) { $tokens[$ifTokenIndex + 1]->clear(); } } }
/** * {@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 (null !== $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); } elseif ($tokens[$lastTokenIndex]->isGivenKind(T_STRING)) { // for real function calls or definitions $possibleDefinitionIndex = $tokens->getPrevMeaningfulToken($lastTokenIndex); if (!$tokens[$possibleDefinitionIndex]->isGivenKind(T_FUNCTION)) { $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]->equalsAny(array(':', ';'))) { if (0 === $ternariesCount) { break; } --$ternariesCount; } } $valueIndex = $tokens->getPrevNonWhitespace($colonIndex); if (2 + $valueIndex === $colonIndex) { $tokens[$valueIndex + 1]->clear(); } } }
/** * 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, ' '))); } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { $lineEnding = $this->whitespacesConfig->getLineEnding(); 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") . $lineEnding . $lineEnding); } elseif (count($parts) <= 2) { $prevToken->setContent($lineEnding . $prevToken->getContent()); } } else { $tokens->insertAt($index, new Token(array(T_WHITESPACE, $lineEnding . $lineEnding))); ++$index; ++$limit; } } }
/** * @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"); } }
/** * {@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); } } }
/** * {@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) { 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) { $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. } }
/** * 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, ' '))); } }
/** * @param Tokens $tokens * @param int $index * * @return string */ 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); }
/** * 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; }
/** * @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, ' '))); } }
/** * @param Tokens $tokens * @param int $classOpenIndex * @param array $classImplementsInfo */ private function fixClassyDefinitionImplements(Tokens $tokens, $classOpenIndex, array $classImplementsInfo) { $endIndex = $tokens->getPrevNonWhitespace($classOpenIndex); if ($this->config['singleLine'] || false === $classImplementsInfo['multiLine']) { $this->makeClassyDefinitionSingleLine($tokens, $classImplementsInfo['start'], $endIndex); } elseif ($this->config['singleItemSingleLine'] && 1 === $classImplementsInfo['numberOfImplements']) { $this->makeClassyDefinitionSingleLine($tokens, $classImplementsInfo['start'], $endIndex); } else { $this->makeClassyInheritancePartMultiLine($tokens, $classImplementsInfo['start'], $endIndex); } }
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->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) { $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_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); $tokens->insertAt($beforeEndBraceIndex + 1, $tokensToInsert); }