Skip to content

Commit

Permalink
ConstantSpacingSniff and PropertySpacingSniff: Fixed false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Feb 1, 2020
1 parent caa2482 commit bb9db1a
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 9 deletions.
Expand Up @@ -38,6 +38,8 @@ abstract class AbstractPropertyAndConstantSpacing implements Sniff
/** @var int */
public $maxLinesCountBeforeWithoutComment = 1;

abstract protected function isNextMemberValid(File $phpcsFile, int $pointer): bool;

abstract protected function addError(File $phpcsFile, int $pointer, int $min, int $max, int $found): bool;

/**
Expand Down Expand Up @@ -68,6 +70,10 @@ public function process(File $phpcsFile, $pointer): int
$types = [T_COMMENT, T_DOC_COMMENT_OPEN_TAG, T_CONST, T_VAR, T_PUBLIC, T_PROTECTED, T_PRIVATE];
$nextPointer = TokenHelper::findNext($phpcsFile, $types, $firstOnLinePointer);

if (!$this->isNextMemberValid($phpcsFile, $nextPointer)) {
return $nextPointer;
}

$linesBetween = $tokens[$nextPointer]['line'] - $tokens[$nextSemicolon]['line'] - 1;
if (in_array($tokens[$nextPointer]['code'], [T_DOC_COMMENT_OPEN_TAG, T_COMMENT], true)) {
$minExpectedLines = SniffSettingsHelper::normalizeInteger($this->minLinesCountBeforeWithComment);
Expand Down
11 changes: 11 additions & 0 deletions SlevomatCodingStandard/Sniffs/Classes/ConstantSpacingSniff.php
Expand Up @@ -3,8 +3,12 @@
namespace SlevomatCodingStandard\Sniffs\Classes;

use PHP_CodeSniffer\Files\File;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function sprintf;
use const T_CONST;
use const T_FUNCTION;
use const T_STRING;
use const T_VARIABLE;

class ConstantSpacingSniff extends AbstractPropertyAndConstantSpacing
{
Expand All @@ -19,6 +23,13 @@ public function register(): array
return [T_CONST];
}

protected function isNextMemberValid(File $phpcsFile, int $pointer): bool
{
$nextPointer = TokenHelper::findNext($phpcsFile, [T_FUNCTION, T_STRING, T_VARIABLE], $pointer + 1);

return $phpcsFile->getTokens()[$nextPointer]['code'] === T_STRING;
}

protected function addError(File $phpcsFile, int $pointer, int $min, int $max, int $found): bool
{
$message = 'Expected %d to %d blank lines after constant, found %d.';
Expand Down
11 changes: 11 additions & 0 deletions SlevomatCodingStandard/Sniffs/Classes/PropertySpacingSniff.php
Expand Up @@ -3,11 +3,15 @@
namespace SlevomatCodingStandard\Sniffs\Classes;

use PHP_CodeSniffer\Files\File;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function sprintf;
use const T_FUNCTION;
use const T_PRIVATE;
use const T_PROTECTED;
use const T_PUBLIC;
use const T_STRING;
use const T_VAR;
use const T_VARIABLE;

class PropertySpacingSniff extends AbstractPropertyAndConstantSpacing
{
Expand All @@ -22,6 +26,13 @@ public function register(): array
return [T_VAR, T_PUBLIC, T_PROTECTED, T_PRIVATE];
}

protected function isNextMemberValid(File $phpcsFile, int $pointer): bool
{
$nextPointer = TokenHelper::findNext($phpcsFile, [T_FUNCTION, T_STRING, T_VARIABLE], $pointer + 1);

return $phpcsFile->getTokens()[$nextPointer]['code'] === T_VARIABLE;
}

protected function addError(File $phpcsFile, int $pointer, int $min, int $max, int $found): bool
{
$message = 'Expected %d to %d blank lines after property, found %d.';
Expand Down
3 changes: 2 additions & 1 deletion tests/Sniffs/Classes/ConstantSpacingSniffTest.php
Expand Up @@ -16,10 +16,11 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/constantSpacingSniffErrors.php');

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

self::assertSniffError($report, 5, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);
self::assertSniffError($report, 22, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);
self::assertSniffError($report, 26, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);

self::assertAllFixedInFile($report);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Sniffs/Classes/PropertySpacingSniffTest.php
Expand Up @@ -16,7 +16,7 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/propertySpacingSniffErrors.php');

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

self::assertSniffError($report, 5, PropertySpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_PROPERTY);
self::assertSniffError($report, 7, PropertySpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_PROPERTY);
Expand All @@ -28,6 +28,7 @@ public function testErrors(): void
self::assertSniffError($report, 39, PropertySpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_PROPERTY);
self::assertSniffError($report, 43, PropertySpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_PROPERTY);
self::assertSniffError($report, 49, PropertySpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_PROPERTY);
self::assertSniffError($report, 53, PropertySpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_PROPERTY);

self::assertAllFixedInFile($report);
}
Expand Down
Expand Up @@ -25,9 +25,7 @@ abstract class Bar {
/** @var string */
const Bar = 'foo';




const WHATEVER = 'whatever';



Expand Down
2 changes: 1 addition & 1 deletion tests/Sniffs/Classes/data/constantSpacingSniffErrors.php
Expand Up @@ -27,7 +27,7 @@ abstract class Bar {




const WHATEVER = 'whatever';



Expand Down
3 changes: 3 additions & 0 deletions tests/Sniffs/Classes/data/constantSpacingSniffNoErrors.php
Expand Up @@ -37,4 +37,7 @@ class Foobar {
];

private const FOO = 3;


private $property;
}
Expand Up @@ -49,8 +49,7 @@ abstract class Bar {
/** @var int */
private $lvl = 9001;



private $noComment = 'noComment';


/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Sniffs/Classes/data/propertySpacingSniffErrors.php
Expand Up @@ -53,7 +53,7 @@ abstract class Bar {
private $lvl = 9001;



private $noComment = 'noComment';


/**
Expand Down
3 changes: 3 additions & 0 deletions tests/Sniffs/Classes/data/propertySpacingSniffNoErrors.php
Expand Up @@ -8,6 +8,9 @@ abstract class Foo {
protected $bar = 'foo';
private $lvl = 9001;


private const CONSTANT = true;

public static abstract function wow();
private function such()
{
Expand Down

0 comments on commit bb9db1a

Please sign in to comment.