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('(', ';', array(T_CLOSE_TAG)));
if (!$tokens[$startParenthesisIndex]->equals('(')) {
continue;
}
$endParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startParenthesisIndex);
$startBraceIndex = $tokens->getNextTokenOfKind($endParenthesisIndex, array(';', '{'));
// fix single-line whitespace before {
// eg: `function foo(){}` => `function foo() {}`
// eg: `function foo() {}` => `function foo() {}`
if ($tokens[$startBraceIndex]->equals('{') && (!$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::T_USE_LAMBDA)) {
// fix whitespace after CT:T_USE_LAMBDA (we might add a token, so do this before determining start and end parenthesis)
$tokens->ensureWhitespaceAtIndex($afterParenthesisIndex + 1, 0, ' ');
$useStartParenthesisIndex = $tokens->getNextTokenOfKind($afterParenthesisIndex, array('('));
$useEndParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $useStartParenthesisIndex);
// remove single-line edge whitespaces inside use parentheses
$this->fixParenthesisInnerEdge($tokens, $useStartParenthesisIndex, $useEndParenthesisIndex);
// fix whitespace before CT::T_USE_LAMBDA
$tokens->ensureWhitespaceAtIndex($afterParenthesisIndex - 1, 1, ' ');
}
// remove single-line edge whitespaces inside parameters list parentheses
$this->fixParenthesisInnerEdge($tokens, $startParenthesisIndex, $endParenthesisIndex);
// remove whitespace before (
// eg: `function foo () {}` => `function foo() {}`
if (!$tokensAnalyzer->isLambda($index) && $tokens[$startParenthesisIndex - 1]->isWhitespace()) {
$tokens[$startParenthesisIndex - 1]->clear();
}
// fix whitespace after T_FUNCTION
// eg: `function foo() {}` => `function foo() {}`
$tokens->ensureWhitespaceAtIndex($index + 1, 0, ' ');
}
}