Skip to content

Commit

Permalink
SlevomatCodingStandard.Classes.ConstantSpacing: Added support for tra…
Browse files Browse the repository at this point in the history
…its with constants
  • Loading branch information
kukulich committed Nov 14, 2022
1 parent 46e6607 commit 516711d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
Expand Up @@ -14,6 +14,7 @@
use const T_ENUM;
use const T_FUNCTION;
use const T_INTERFACE;
use const T_TRAIT;
use const T_USE;
use const T_VARIABLE;

Expand Down Expand Up @@ -44,7 +45,7 @@ public function process(File $phpcsFile, $constantPointer): int

/** @var int $classPointer */
$classPointer = array_keys($tokens[$constantPointer]['conditions'])[count($tokens[$constantPointer]['conditions']) - 1];
if (!in_array($tokens[$classPointer]['code'], [T_CLASS, T_INTERFACE, T_ANON_CLASS, T_ENUM], true)) {
if (!in_array($tokens[$classPointer]['code'], [T_CLASS, T_INTERFACE, T_ANON_CLASS, T_ENUM, T_TRAIT], true)) {
return $constantPointer;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Sniffs/Classes/ConstantSpacingSniffTest.php
Expand Up @@ -17,14 +17,15 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/constantSpacingErrors.php');

self::assertSame(6, $report->getErrorCount());
self::assertSame(7, $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::assertSniffError($report, 30, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);
self::assertSniffError($report, 33, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);
self::assertSniffError($report, 36, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);
self::assertSniffError($report, 57, ConstantSpacingSniff::CODE_INCORRECT_COUNT_OF_BLANK_LINES_AFTER_CONSTANT);

self::assertAllFixedInFile($report);
}
Expand Down
11 changes: 10 additions & 1 deletion tests/Sniffs/Classes/data/constantSpacingErrors.fixed.php
@@ -1,4 +1,4 @@
<?php // lint >= 8.1
<?php // lint >= 8.2

abstract class Foo {
/** @var string */
Expand Down Expand Up @@ -45,3 +45,12 @@ private function such()
{
}
}

trait Boo
{

const BOO = 'boo';

const BOOO = 'booo';

}
12 changes: 11 additions & 1 deletion tests/Sniffs/Classes/data/constantSpacingErrors.php
@@ -1,4 +1,4 @@
<?php // lint >= 8.1
<?php // lint >= 8.2

abstract class Foo {
/** @var string */
Expand Down Expand Up @@ -50,3 +50,13 @@ private function such()
{
}
}

trait Boo
{

const BOO = 'boo';


const BOOO = 'booo';

}

0 comments on commit 516711d

Please sign in to comment.