From 4d4c38c096315b92d39a1bbb3f7a1334212d30e1 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Mon, 28 Oct 2019 08:33:01 +1100 Subject: [PATCH] Fixed bug #2673 : PSR12.Traits.UseDeclaration does not allow comments or blank lines between use statements --- package.xml | 1 + .../Sniffs/Traits/UseDeclarationSniff.php | 6 +++--- .../Tests/Traits/UseDeclarationUnitTest.inc | 18 +++++++++++++++++ .../Traits/UseDeclarationUnitTest.inc.fixed | 20 +++++++++++++++++++ .../Tests/Traits/UseDeclarationUnitTest.php | 3 +-- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/package.xml b/package.xml index f82882f3a9..9fd487278b 100644 --- a/package.xml +++ b/package.xml @@ -40,6 +40,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - Fixed bug #2663 : Generic.NamingConventions.ConstructorName complains about old constructor in interfaces - Fixed bug #2664 : PSR12.Files.OpenTag incorrectly identifies PHP file with only an opening tag - Fixed bug #2665 : PSR12.Files.ImportStatement should not apply to traits + - Fixed bug #2673 : PSR12.Traits.UseDeclaration does not allow comments or blank lines between use statements diff --git a/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php b/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php index 093b20d61e..b6fb250784 100644 --- a/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php +++ b/src/Standards/PSR12/Sniffs/Traits/UseDeclarationSniff.php @@ -135,10 +135,10 @@ public function process(File $phpcsFile, $stackPtr) }//end if }//end if } else { - // Make sure this use statement immediately follows the previous one. + // Make sure this use statement is not on the same line as the previous one. $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); - if ($prev !== false && $tokens[$prev]['line'] !== ($tokens[$stackPtr]['line'] - 1)) { - $error = 'Each imported trait must be on the line after the previous import'; + if ($prev !== false && $tokens[$prev]['line'] === $tokens[$stackPtr]['line']) { + $error = 'Each imported trait must be on it\'s own line'; $prevNonWs = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); if ($prevNonWs !== $prev) { $phpcsFile->addError($error, $stackPtr, 'SpacingBeforeImport'); diff --git a/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc b/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc index dbb7e32ff4..2e42b1c150 100644 --- a/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc +++ b/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc @@ -131,3 +131,21 @@ class Foo implements Bar use Baz; } + +class ClassName +{ + /** + * DocBlockContent + */ + use FirstTrait; + + /** + * DocBlockContent + */ + use SecondTrait; + + /** + * DocBlockContent + */ + use ThirdTrait; +} diff --git a/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc.fixed b/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc.fixed index ecdd7f0775..98b9b06773 100644 --- a/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.inc.fixed @@ -77,7 +77,9 @@ class ClassName9 class ClassName10 { use TransactionTrait; + use PermissionAwareTrait; + use FirstTrait; use SecondTrait; use ThirdTrait; @@ -124,3 +126,21 @@ class Foo implements Bar use Baz; } + +class ClassName +{ + /** + * DocBlockContent + */ + use FirstTrait; + + /** + * DocBlockContent + */ + use SecondTrait; + + /** + * DocBlockContent + */ + use ThirdTrait; +} diff --git a/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php b/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php index ee0dd1f8bb..fd28cceb64 100644 --- a/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php +++ b/src/Standards/PSR12/Tests/Traits/UseDeclarationUnitTest.php @@ -38,8 +38,7 @@ public function getErrorList() 71 => 1, 73 => 2, 76 => 1, - 84 => 1, - 86 => 3, + 86 => 2, 103 => 1, 112 => 1, 122 => 1,