Skip to content

Commit

Permalink
Consistency fix to allow space after opening parenthesis in control s…
Browse files Browse the repository at this point in the history
…tructures

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
  • Loading branch information
michalbundyra committed Mar 7, 2019
1 parent 8c8c4ff commit 1a984dd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;

class ControlStructureSpacingSniff implements Sniff
{
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function getErrorList()
26 => 2,
27 => 2,
31 => 1,
41 => 2,
43 => 2,
51 => 2,
53 => 2,
];

}//end getErrorList()
Expand Down

0 comments on commit 1a984dd

Please sign in to comment.