From 516711daacd6852ac1ea3868e3fdd5e3abc60174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Mon, 14 Nov 2022 10:15:03 +0100 Subject: [PATCH] SlevomatCodingStandard.Classes.ConstantSpacing: Added support for traits with constants --- .../Sniffs/Classes/ConstantSpacingSniff.php | 3 ++- tests/Sniffs/Classes/ConstantSpacingSniffTest.php | 3 ++- .../Classes/data/constantSpacingErrors.fixed.php | 11 ++++++++++- tests/Sniffs/Classes/data/constantSpacingErrors.php | 12 +++++++++++- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/SlevomatCodingStandard/Sniffs/Classes/ConstantSpacingSniff.php b/SlevomatCodingStandard/Sniffs/Classes/ConstantSpacingSniff.php index 663a39071..46d999132 100644 --- a/SlevomatCodingStandard/Sniffs/Classes/ConstantSpacingSniff.php +++ b/SlevomatCodingStandard/Sniffs/Classes/ConstantSpacingSniff.php @@ -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; @@ -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; } diff --git a/tests/Sniffs/Classes/ConstantSpacingSniffTest.php b/tests/Sniffs/Classes/ConstantSpacingSniffTest.php index 8052f34b3..8407445f3 100644 --- a/tests/Sniffs/Classes/ConstantSpacingSniffTest.php +++ b/tests/Sniffs/Classes/ConstantSpacingSniffTest.php @@ -17,7 +17,7 @@ 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); @@ -25,6 +25,7 @@ public function testErrors(): void 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); } diff --git a/tests/Sniffs/Classes/data/constantSpacingErrors.fixed.php b/tests/Sniffs/Classes/data/constantSpacingErrors.fixed.php index 0a29671dc..cd1017169 100644 --- a/tests/Sniffs/Classes/data/constantSpacingErrors.fixed.php +++ b/tests/Sniffs/Classes/data/constantSpacingErrors.fixed.php @@ -1,4 +1,4 @@ -= 8.1 += 8.2 abstract class Foo { /** @var string */ @@ -45,3 +45,12 @@ private function such() { } } + +trait Boo +{ + + const BOO = 'boo'; + + const BOOO = 'booo'; + +} diff --git a/tests/Sniffs/Classes/data/constantSpacingErrors.php b/tests/Sniffs/Classes/data/constantSpacingErrors.php index d3d1ac969..e3c116976 100644 --- a/tests/Sniffs/Classes/data/constantSpacingErrors.php +++ b/tests/Sniffs/Classes/data/constantSpacingErrors.php @@ -1,4 +1,4 @@ -= 8.1 += 8.2 abstract class Foo { /** @var string */ @@ -50,3 +50,13 @@ private function such() { } } + +trait Boo +{ + + const BOO = 'boo'; + + + const BOOO = 'booo'; + +}