Skip to content

Commit

Permalink
EarlyExitSniff: Improved fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Feb 5, 2020
1 parent a741c09 commit a05d520
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
9 changes: 0 additions & 9 deletions SlevomatCodingStandard/Helpers/IndentationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use const T_END_NOWDOC;
use const T_START_HEREDOC;
use const T_START_NOWDOC;
use const T_WHITESPACE;

/**
* @internal
Expand All @@ -25,14 +24,6 @@ class IndentationHelper

public static function getIndentation(File $phpcsFile, int $pointer): string
{
$tokens = $phpcsFile->getTokens();

$nonWhitespacePointer = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $pointer - 1);

if ($tokens[$nonWhitespacePointer]['line'] === $tokens[$pointer]['line']) {
return '';
}

$firstPointerOnLine = TokenHelper::findFirstTokenOnLine($phpcsFile, $pointer);

return TokenHelper::getContent($phpcsFile, $firstPointerOnLine, $pointer - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private function processElse(File $phpcsFile, int $elsePointer): void
}

$elseCodePointers = $this->getScopeCodePointers($phpcsFile, $elsePointer);
$afterIfCode = IndentationHelper::fixIndentation($phpcsFile, $elseCodePointers, IndentationHelper::addIndentation(IndentationHelper::getIndentation($phpcsFile, $previousConditionPointer)));
$afterIfCode = IndentationHelper::fixIndentation($phpcsFile, $elseCodePointers, IndentationHelper::getIndentation($phpcsFile, $ifPointer));

$phpcsFile->fixer->beginChangeset();

Expand Down
6 changes: 3 additions & 3 deletions tests/Sniffs/ControlStructures/EarlyExitSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/earlyExitErrors.php');

self::assertSame(60, $report->getErrorCount());
self::assertSame(62, $report->getErrorCount());

foreach ([6, 15, 24, 33, 42, 50, 58, 66, 74, 82, 90, 98, 108, 149, 157, 165, 191, 199, 207, 376] as $line) {
self::assertSniffError($report, $line, EarlyExitSniff::CODE_EARLY_EXIT_NOT_USED, 'Use early exit instead of else.');
Expand All @@ -27,11 +27,11 @@ public function testErrors(): void
self::assertSniffError($report, $line, EarlyExitSniff::CODE_EARLY_EXIT_NOT_USED, 'Use early exit to reduce code nesting.');
}

foreach ([173, 182, 328, 353, 398, 440, 462] as $line) {
foreach ([173, 182, 328, 353, 398, 440, 462, 475] as $line) {
self::assertSniffError($report, $line, EarlyExitSniff::CODE_USELESS_ELSE, 'Remove useless else to reduce code nesting.');
}

foreach ([322, 324, 326, 336, 338, 340, 351, 396, 406, 436, 460] as $line) {
foreach ([322, 324, 326, 336, 338, 340, 351, 396, 406, 436, 460, 473] as $line) {
self::assertSniffError($report, $line, EarlyExitSniff::CODE_USELESS_ELSEIF, 'Use if instead of elseif.');
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Sniffs/ControlStructures/data/earlyExitErrors.fixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,19 @@ function moreInlineComments()
// Comment
return 3;
}

class Whatever
{
public function moreInlineCommentsWithMoreIndentation(): int
{
if (true) { // Comment
return 1;
}

if (true) { // Comment
return 2;
}
// Comment
return 3;
}
}
14 changes: 14 additions & 0 deletions tests/Sniffs/ControlStructures/data/earlyExitErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,17 @@ function moreInlineComments()
return 3;
}
}

class Whatever
{
public function moreInlineCommentsWithMoreIndentation(): int
{
if (true) { // Comment
return 1;
} elseif (true) { // Comment
return 2;
} else { // Comment
return 3;
}
}
}

0 comments on commit a05d520

Please sign in to comment.