Skip to content

Commit

Permalink
TraitUseSpacingSniff: Fix for uses with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Aug 25, 2020
1 parent 6f45c01 commit a5dda73
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
22 changes: 20 additions & 2 deletions SlevomatCodingStandard/Sniffs/Classes/TraitUseSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SlevomatCodingStandard\Helpers\ClassHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function count;
use function in_array;
use function sprintf;
use function substr_count;
use const T_ANON_CLASS;
Expand Down Expand Up @@ -74,14 +76,22 @@ private function checkLinesBeforeFirstUse(File $phpcsFile, int $firstUsePointer)
{
$tokens = $phpcsFile->getTokens();

$useStartPointer = $firstUsePointer;

/** @var int $pointerBeforeFirstUse */
$pointerBeforeFirstUse = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $firstUsePointer - 1);

if (in_array($tokens[$pointerBeforeFirstUse]['code'], Tokens::$commentTokens, true)) {
$pointerBeforeFirstUse = TokenHelper::findPreviousEffective($phpcsFile, $pointerBeforeFirstUse - 1);
$useStartPointer = TokenHelper::findNext($phpcsFile, Tokens::$commentTokens, $pointerBeforeFirstUse + 1);
}

$isAtTheStartOfClass = $tokens[$pointerBeforeFirstUse]['code'] === T_OPEN_CURLY_BRACKET;

$whitespaceBeforeFirstUse = '';

if ($pointerBeforeFirstUse + 1 !== $firstUsePointer) {
$whitespaceBeforeFirstUse .= TokenHelper::getContent($phpcsFile, $pointerBeforeFirstUse + 1, $firstUsePointer - 1);
$whitespaceBeforeFirstUse .= TokenHelper::getContent($phpcsFile, $pointerBeforeFirstUse + 1, $useStartPointer - 1);
}

$requiredLinesCountBeforeFirstUse = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeFirstUse);
Expand All @@ -91,6 +101,7 @@ private function checkLinesBeforeFirstUse(File $phpcsFile, int $firstUsePointer)
) {
$requiredLinesCountBeforeFirstUse = SniffSettingsHelper::normalizeInteger($this->linesCountBeforeFirstUseWhenFirstInClass);
}

$actualLinesCountBeforeFirstUse = substr_count($whitespaceBeforeFirstUse, $phpcsFile->eolChar) - 1;

if ($actualLinesCountBeforeFirstUse === $requiredLinesCountBeforeFirstUse) {
Expand Down Expand Up @@ -206,7 +217,14 @@ private function checkLinesBetweenUses(File $phpcsFile, array $usePointers): voi
$previousUseEndPointer = $tokens[$previousUseEndPointer]['bracket_closer'];
}

$actualLinesCountAfterPreviousUse = $tokens[$usePointer]['line'] - $tokens[$previousUseEndPointer]['line'] - 1;
$useStartPointer = $usePointer;
$pointerBeforeUse = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $usePointer - 1);

if (in_array($tokens[$pointerBeforeUse]['code'], Tokens::$commentTokens, true)) {
$useStartPointer = TokenHelper::findNext($phpcsFile, Tokens::$commentTokens, TokenHelper::findPreviousEffective($phpcsFile, $pointerBeforeUse - 1) + 1);
}

$actualLinesCountAfterPreviousUse = $tokens[$useStartPointer]['line'] - $tokens[$previousUseEndPointer]['line'] - 1;

if ($actualLinesCountAfterPreviousUse === $requiredLinesCountBetweenUses) {
$previousUsePointer = $usePointer;
Expand Down
18 changes: 18 additions & 0 deletions tests/Sniffs/Classes/TraitUseSpacingSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ public function testOneUseDefaultSettingErrors(): void
self::assertAllFixedInFile($report);
}

public function testDefaultSettingWithCommentsNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingDefaultSettingsWithCommentsNoErrors.php');
self::assertNoSniffErrorInFile($report);
}

public function testDefaultSettingWithCommentsErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingDefaultSettingsWithCommentsErrors.php');

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

self::assertSniffError($report, 8, TraitUseSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_USE);
self::assertSniffError($report, 14, TraitUseSpacingSniff::CODE_INCORRECT_LINES_COUNT_BETWEEN_USES);

self::assertAllFixedInFile($report);
}

public function testModifiedSettingNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/traitUseSpacingModifiedSettingsNoErrors.php', [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class Whatever
{

// Comment
use A;
use B {
B::b as bb;
}
// Comment
use C;
use D;

public function __construct()
{

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

class Whatever
{


// Comment
use A;
use B {
B::b as bb;
}

// Comment
use C;
use D;

public function __construct()
{

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class Whatever
{

// Comment
use A;
use B {
B::b as bb;
}
// Comment
use C;
use D;

public function __construct()
{

}

}

0 comments on commit a5dda73

Please sign in to comment.