From 1a984dd75f65f224f33c67a5edc6e19585dcf000 Mon Sep 17 00:00:00 2001 From: webimpress Date: Thu, 7 Mar 2019 20:33:38 +0000 Subject: [PATCH] Consistency fix to allow space after opening parenthesis in control structures For the consistency with other sniffs we should allow spaces after opening parenthesis in control structures when the next content is a comment. Fixes #2411 --- .../ControlStructureSpacingSniff.php | 60 ++++++++++--------- .../ControlStructureSpacingUnitTest.inc | 10 ++++ .../ControlStructureSpacingUnitTest.inc.fixed | 10 ++++ .../ControlStructureSpacingUnitTest.php | 4 +- 4 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/Standards/PSR2/Sniffs/ControlStructures/ControlStructureSpacingSniff.php b/src/Standards/PSR2/Sniffs/ControlStructures/ControlStructureSpacingSniff.php index cc0872c59f..6ab3f5573d 100644 --- a/src/Standards/PSR2/Sniffs/ControlStructures/ControlStructureSpacingSniff.php +++ b/src/Standards/PSR2/Sniffs/ControlStructures/ControlStructureSpacingSniff.php @@ -11,6 +11,7 @@ use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Util\Tokens; class ControlStructureSpacingSniff implements Sniff { @@ -75,37 +76,42 @@ public function process(File $phpcsFile, $stackPtr) return; } - $parenOpener = $tokens[$stackPtr]['parenthesis_opener']; - $parenCloser = $tokens[$stackPtr]['parenthesis_closer']; - $spaceAfterOpen = 0; - if ($tokens[($parenOpener + 1)]['code'] === T_WHITESPACE) { - if (strpos($tokens[($parenOpener + 1)]['content'], $phpcsFile->eolChar) !== false) { - $spaceAfterOpen = 'newline'; - } else { - $spaceAfterOpen = $tokens[($parenOpener + 1)]['length']; + $parenOpener = $tokens[$stackPtr]['parenthesis_opener']; + $parenCloser = $tokens[$stackPtr]['parenthesis_closer']; + $nextContent = $phpcsFile->findNext(T_WHITESPACE, ($parenOpener + 1), null, true); + if (false === in_array($tokens[$nextContent]['code'], Tokens::$commentTokens, true)) { + $spaceAfterOpen = 0; + if ($tokens[($parenOpener + 1)]['code'] === T_WHITESPACE) { + if (strpos($tokens[($parenOpener + 1)]['content'], $phpcsFile->eolChar) !== false) { + $spaceAfterOpen = 'newline'; + } else { + $spaceAfterOpen = $tokens[($parenOpener + 1)]['length']; + } } - } - $phpcsFile->recordMetric($stackPtr, 'Spaces after control structure open parenthesis', $spaceAfterOpen); - - if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) { - $error = 'Expected %s spaces after opening bracket; %s found'; - $data = [ - $this->requiredSpacesAfterOpen, - $spaceAfterOpen, - ]; - $fix = $phpcsFile->addFixableError($error, ($parenOpener + 1), 'SpacingAfterOpenBrace', $data); - if ($fix === true) { - $padding = str_repeat(' ', $this->requiredSpacesAfterOpen); - if ($spaceAfterOpen === 0) { - $phpcsFile->fixer->addContent($parenOpener, $padding); - } else if ($spaceAfterOpen === 'newline') { - $phpcsFile->fixer->replaceToken(($parenOpener + 1), ''); - } else { - $phpcsFile->fixer->replaceToken(($parenOpener + 1), $padding); + $phpcsFile->recordMetric($stackPtr, 'Spaces after control structure open parenthesis', $spaceAfterOpen); + + if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) { + $error = 'Expected %s spaces after opening bracket; %s found'; + $data = [ + $this->requiredSpacesAfterOpen, + $spaceAfterOpen, + ]; + $fix = $phpcsFile->addFixableError($error, ($parenOpener + 1), 'SpacingAfterOpenBrace', $data); + if ($fix === true) { + $padding = str_repeat(' ', $this->requiredSpacesAfterOpen); + if ($spaceAfterOpen === 0) { + $phpcsFile->fixer->addContent($parenOpener, $padding); + } else { + if ($spaceAfterOpen === 'newline') { + $phpcsFile->fixer->replaceToken(($parenOpener + 1), ''); + } else { + $phpcsFile->fixer->replaceToken(($parenOpener + 1), $padding); + } + } } } - } + }//end if if ($tokens[$parenOpener]['line'] === $tokens[$parenCloser]['line']) { $spaceBeforeClose = 0; diff --git a/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc b/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc index b2b7efc49e..a894edd3d4 100644 --- a/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc +++ b/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc @@ -36,6 +36,16 @@ if ( $settingsUpdated = FALSE; } +if ( // comment + $something +) { +} + +while ( /* comment */ + true +) { +} + // phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesAfterOpen 1 // phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesBeforeClose 1 foreach ($something as $blah => $that) {} diff --git a/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed b/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed index a2b7ebb9f5..30093706d3 100644 --- a/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed @@ -35,6 +35,16 @@ if ($defaultPageDesign === 0 $settingsUpdated = FALSE; } +if ( // comment + $something +) { +} + +while ( /* comment */ + true +) { +} + // phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesAfterOpen 1 // phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesBeforeClose 1 foreach ( $something as $blah => $that ) {} diff --git a/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.php b/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.php index 7e576cdae6..cc3ef00218 100644 --- a/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.php +++ b/src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.php @@ -31,8 +31,8 @@ public function getErrorList() 26 => 2, 27 => 2, 31 => 1, - 41 => 2, - 43 => 2, + 51 => 2, + 53 => 2, ]; }//end getErrorList()