private function correctLineBreaks(Tokens $tokens, $startIndex, $endIndex, $reqLineCount = 2)
{
$lineEnding = $this->whitespacesConfig->getLineEnding();
++$startIndex;
$numbOfWhiteTokens = $endIndex - $startIndex;
if (0 === $numbOfWhiteTokens) {
$tokens->insertAt($startIndex, new Token(array(T_WHITESPACE, str_repeat($lineEnding, $reqLineCount))));
return;
}
$lineBreakCount = $this->getLineBreakCount($tokens, $startIndex, $endIndex);
if ($reqLineCount === $lineBreakCount) {
return;
}
if ($lineBreakCount < $reqLineCount) {
$tokens[$startIndex]->setContent(str_repeat($lineEnding, $reqLineCount - $lineBreakCount) . $tokens[$startIndex]->getContent());
return;
}
// $lineCount = > $reqLineCount : check the one Token case first since this one will be true most of the time
if (1 === $numbOfWhiteTokens) {
$tokens[$startIndex]->setContent(preg_replace('/\\r\\n|\\n/', '', $tokens[$startIndex]->getContent(), $lineBreakCount - $reqLineCount));
return;
}
// $numbOfWhiteTokens = > 1
$toReplaceCount = $lineBreakCount - $reqLineCount;
for ($i = $startIndex; $i < $endIndex && $toReplaceCount > 0; ++$i) {
$tokenLineCount = substr_count($tokens[$i]->getContent(), "\n");
if ($tokenLineCount > 0) {
$tokens[$i]->setContent(preg_replace('/\\r\\n|\\n/', '', $tokens[$i]->getContent(), min($toReplaceCount, $tokenLineCount)));
$toReplaceCount -= $tokenLineCount;
}
}
}