Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Mar 25, 2023
1 parent f026751 commit 8f11e0f
Showing 1 changed file with 7 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use SlevomatCodingStandard\Helpers\IndentationHelper;
use SlevomatCodingStandard\Helpers\TernaryOperatorHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function strlen;
use function substr;
use const T_INLINE_ELSE;
use const T_INLINE_THEN;
use const T_WHITESPACE;
Expand Down Expand Up @@ -57,14 +55,19 @@ public function process(File $phpcsFile, $inlineThenPointer): void
}

$inlineElsePointer = TernaryOperatorHelper::getElsePointer($phpcsFile, $inlineThenPointer);
$endOfLineBeforeInlineThenPointer = TokenHelper::findLastTokenOnPreviousLine($phpcsFile, $inlineThenPointer);

$indentation = $this->getIndentation($phpcsFile, $endOfLineBeforeInlineThenPointer);
$pointerBeforeInlineThen = TokenHelper::findPreviousEffective($phpcsFile, $inlineThenPointer - 1);
$pointerAfterInlineThen = TokenHelper::findNextExcluding($phpcsFile, [T_WHITESPACE], $inlineThenPointer + 1);
$pointerBeforeInlineElse = TokenHelper::findPreviousEffective($phpcsFile, $inlineElsePointer - 1);
$pointerAfterInlineElse = TokenHelper::findNextExcluding($phpcsFile, [T_WHITESPACE], $inlineElsePointer + 1);

$indentation = IndentationHelper::addIndentation(
IndentationHelper::getIndentation(
$phpcsFile,
TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $inlineThenPointer)
)
);

$phpcsFile->fixer->beginChangeset();

FixerHelper::removeBetween($phpcsFile, $pointerBeforeInlineThen, $inlineThenPointer);
Expand All @@ -82,21 +85,4 @@ public function process(File $phpcsFile, $inlineThenPointer): void
$phpcsFile->fixer->endChangeset();
}

private function getIndentation(File $phpcsFile, int $endOfLinePointer): string
{
$pointerAfterWhitespace = TokenHelper::findNextNonWhitespace($phpcsFile, $endOfLinePointer + 1);
$actualIndentation = TokenHelper::getContent($phpcsFile, $endOfLinePointer + 1, $pointerAfterWhitespace - 1);

if (strlen($actualIndentation) !== 0) {
return $actualIndentation . (
substr($actualIndentation, -1) === IndentationHelper::TAB_INDENT
? IndentationHelper::TAB_INDENT
: IndentationHelper::SPACES_INDENT
);
}

$tabPointer = TokenHelper::findPreviousContent($phpcsFile, T_WHITESPACE, IndentationHelper::TAB_INDENT, $endOfLinePointer - 1);
return $tabPointer !== null ? IndentationHelper::TAB_INDENT : IndentationHelper::SPACES_INDENT;
}

}

0 comments on commit 8f11e0f

Please sign in to comment.