Skip to content

Commit

Permalink
Merge pull request #277 from nyordanov/if-else-alternative
Browse files Browse the repository at this point in the history
Fix T_ELSE immediately followed by T_COLON
  • Loading branch information
westonruter committed Jan 2, 2015
2 parents e0b6dde + 0e97a27 commit 13e2142
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
54 changes: 52 additions & 2 deletions WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class WordPress_Sniffs_WhiteSpace_ControlStructureSpacingSniff implements PHP_Co
*/
public $blank_line_after_check = true;

/**
* Require for space before T_COLON when using the alternative syntax for control structures
*
* @var string one of 'required', 'forbidden', optional'
*/
public $space_before_colon = 'required';


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -83,7 +90,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)

$tokens = $phpcsFile->getTokens();

if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE
&& ! ( $tokens[$stackPtr]['code'] === T_ELSE && $tokens[($stackPtr + 1)]['code'] === T_COLON )
) {
$error = 'Space after opening control structure is required';
if (isset($phpcsFile->fixer) === true) {
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterStructureOpen');
Expand All @@ -104,9 +113,50 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$scopeOpener = $tokens[$stackPtr]['scope_opener'];
$scopeCloser = $tokens[$stackPtr]['scope_closer'];

// alternative syntax
if ( $tokens[$scopeOpener]['code'] === T_COLON ) {

if ( $this->space_before_colon === 'required') {

if ( $tokens[$scopeOpener - 1]['code'] !== T_WHITESPACE ) {
$error = 'Space between opening control structure and T_COLON is required';

if ( isset($phpcsFile->fixer) === true ) {
$fix = $phpcsFile->addFixableError($error, $scopeOpener, 'NoSpaceBetweenStructureColon');

if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContentBefore($scopeOpener, ' ');
$phpcsFile->fixer->endChangeset();
}
} else {
$phpcsFile->addError($error, $stackPtr, 'NoSpaceBetweenStructureColon');
}
}

} elseif ( $this->space_before_colon === 'forbidden' ) {

if ( $tokens[$scopeOpener - 1]['code'] === T_WHITESPACE ) {
$error = 'Extra space between opening control structure and T_COLON found';

if ( isset($phpcsFile->fixer) === true ) {
$fix = $phpcsFile->addFixableError( $error, $scopeOpener - 1, 'SpaceBetweenStructureColon' );

if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken( $scopeOpener - 1, '' );
$phpcsFile->fixer->endChangeset();
}
} else {
$phpcsFile->addError( $error, $stackPtr, 'SpaceBetweenStructureColon' );
}
}
}
}

$parenthesisOpener = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);

if (($stackPtr + 1) === $parenthesisOpener) {
if (($stackPtr + 1) === $parenthesisOpener && $tokens[$parenthesisOpener]['code'] !== T_COLON) {
// Checking this: $value = my_function[*](...).
$error = 'No space before opening parenthesis is prohibited';
if (isset($phpcsFile->fixer) === true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ if ( true ) {
}

}

if ( false ):
else:
endif;

if ( false ) :
else :
endif;
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ if ( true ) {

}
}

if ( false ) :
else :
endif;

if ( false ) :
else :
endif;
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function getErrorList()
17 => 1,
29 => 4,
37 => 1,
41 => 1,
42 => 1,
);

// Uncomment when "$blank_line_check" parameter will be "true" by default.
Expand Down

0 comments on commit 13e2142

Please sign in to comment.