From 69087ae8c666b05a385367a33762bf2104120f76 Mon Sep 17 00:00:00 2001 From: Nikolay Yordanov Date: Mon, 1 Dec 2014 19:03:14 +0000 Subject: [PATCH 1/6] Fix T_ELSE immediately followed by T_COLON Curently else: throws two errors: 1) Space after opening control structure is required (the Coding Standards handbook doesn't say anything on this) 2) No space before opening parenthesis is prohibited (this is just wrong - there is no paranthesis on this line) This commit prevents the sniffer from throwing these two errors. --- .../Sniffs/WhiteSpace/ControlStructureSpacingSniff.php | 6 ++++-- .../Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc | 4 ++++ .../WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed | 4 ++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php index ca34eeecc9..69c4e16aea 100644 --- a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php @@ -83,7 +83,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'); @@ -106,7 +108,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) $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) { diff --git a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc index 70fbc70fb6..2c035296df 100644 --- a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc +++ b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc @@ -37,3 +37,7 @@ if ( true ) { } } + +if ( false ): +else: +endif; diff --git a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed index f0f31ee664..c637688e5f 100644 --- a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed +++ b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed @@ -35,3 +35,7 @@ if ( true ) { } } + +if ( false ): +else: +endif; From ac62ce6b55bc1bc3cff3d6b3a9f80909342f2419 Mon Sep 17 00:00:00 2001 From: Nikolay Yordanov Date: Mon, 8 Dec 2014 11:23:34 +0000 Subject: [PATCH 2/6] Add a property to control whether a space is required between a control structure and T_COLON --- .../ControlStructureSpacingSniff.php | 53 ++++++++++++++++++- .../ControlStructureSpacingUnitTest.inc.fixed | 4 +- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php index 69c4e16aea..eb450b4df4 100644 --- a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php @@ -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 boolean + */ + public $space_before_colon_required = true; + /** * Returns an array of tokens this test wants to listen for. @@ -78,8 +85,9 @@ public function register() */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { - $this->blank_line_check = (bool) $this->blank_line_check; - $this->blank_line_after_check = (bool) $this->blank_line_after_check; + $this->blank_line_check = (bool) $this->blank_line_check; + $this->blank_line_after_check = (bool) $this->blank_line_after_check; + $this->space_before_colon_check = (bool) $this->space_before_colon_check; $tokens = $phpcsFile->getTokens(); @@ -106,6 +114,47 @@ 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_check ) { + + 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'); + } + } + + } else { + + 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 && $tokens[$parenthesisOpener]['code'] !== T_COLON) { diff --git a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed index c637688e5f..1020f06369 100644 --- a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed +++ b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed @@ -36,6 +36,6 @@ if ( true ) { } } -if ( false ): -else: +if ( false ) : +else : endif; From cee8e71dc8e2092f3ebc42fe9662403b12ddfffc Mon Sep 17 00:00:00 2001 From: Nikolay Yordanov Date: Mon, 8 Dec 2014 11:29:27 +0000 Subject: [PATCH 3/6] WordPress_Sniffs_WhiteSpace_ControlStructureSpacingSniff::$space_before_colon_check is actually WordPress_Sniffs_WhiteSpace_ControlStructureSpacingSniff::$space_before_colon_required --- .../Sniffs/WhiteSpace/ControlStructureSpacingSniff.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php index eb450b4df4..9b41a528d9 100644 --- a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php @@ -85,9 +85,9 @@ public function register() */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { - $this->blank_line_check = (bool) $this->blank_line_check; - $this->blank_line_after_check = (bool) $this->blank_line_after_check; - $this->space_before_colon_check = (bool) $this->space_before_colon_check; + $this->blank_line_check = (bool) $this->blank_line_check; + $this->blank_line_after_check = (bool) $this->blank_line_after_check; + $this->space_before_colon_required = (bool) $this->space_before_colon_required; $tokens = $phpcsFile->getTokens(); @@ -117,7 +117,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // alternative syntax if ( $tokens[$scopeOpener]['code'] === T_COLON ) { - if ( $this->space_before_colon_check ) { + if ( $this->space_before_colon_required ) { if ( $tokens[$scopeOpener - 1]['code'] !== T_WHITESPACE ) { $error = 'Space between opening control structure and T_COLON is required'; From 5afd52c323e71ecde4fe9ab48491b6651dbc9565 Mon Sep 17 00:00:00 2001 From: Nikolay Yordanov Date: Mon, 8 Dec 2014 11:51:38 +0000 Subject: [PATCH 4/6] add tests for control structure followed by T_COLON --- .../Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc | 4 ++++ .../WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed | 4 ++++ .../Tests/WhiteSpace/ControlStructureSpacingUnitTest.php | 2 ++ 3 files changed, 10 insertions(+) diff --git a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc index 2c035296df..957a3f6767 100644 --- a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc +++ b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc @@ -41,3 +41,7 @@ if ( true ) { if ( false ): else: endif; + +if ( false ) : +else : +endif; diff --git a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed index 1020f06369..56168f5d31 100644 --- a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed +++ b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.inc.fixed @@ -39,3 +39,7 @@ if ( true ) { if ( false ) : else : endif; + +if ( false ) : +else : +endif; diff --git a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php index 9e879729f5..672b688fed 100644 --- a/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php +++ b/WordPress/Tests/WhiteSpace/ControlStructureSpacingUnitTest.php @@ -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. From 68400716124d4f346c989697bd5c0b426f56410a Mon Sep 17 00:00:00 2001 From: Nikolay Yordanov Date: Thu, 11 Dec 2014 10:52:57 +0000 Subject: [PATCH 5/6] Ability to set the space between a control structure and T_COLON as "required", "forbidden" or "optional" --- .../Sniffs/WhiteSpace/ControlStructureSpacingSniff.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php index 9b41a528d9..10fe87ce4e 100644 --- a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php @@ -48,9 +48,9 @@ class WordPress_Sniffs_WhiteSpace_ControlStructureSpacingSniff implements PHP_Co /** * Require for space before T_COLON when using the alternative syntax for control structures * - * @var boolean + * @var string one of 'required', 'forbidden', optional' */ - public $space_before_colon_required = true; + public $space_before_colon = 'required'; /** @@ -117,7 +117,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) // alternative syntax if ( $tokens[$scopeOpener]['code'] === T_COLON ) { - if ( $this->space_before_colon_required ) { + if ( $this->space_before_colon === 'required') { if ( $tokens[$scopeOpener - 1]['code'] !== T_WHITESPACE ) { $error = 'Space between opening control structure and T_COLON is required'; @@ -135,7 +135,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } } - } else { + } elseif ( $this->space_before_colon === 'forbidden' ) { if ( $tokens[$scopeOpener - 1]['code'] === T_WHITESPACE ) { $error = 'Extra space between opening control structure and T_COLON found'; From 0e97a2750daf7f1262252bea22a70825e10d5d05 Mon Sep 17 00:00:00 2001 From: Nikolay Yordanov Date: Thu, 11 Dec 2014 11:17:48 +0000 Subject: [PATCH 6/6] Remove old code --- WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php index 10fe87ce4e..58f70d86ae 100644 --- a/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php +++ b/WordPress/Sniffs/WhiteSpace/ControlStructureSpacingSniff.php @@ -85,9 +85,8 @@ public function register() */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { - $this->blank_line_check = (bool) $this->blank_line_check; - $this->blank_line_after_check = (bool) $this->blank_line_after_check; - $this->space_before_colon_required = (bool) $this->space_before_colon_required; + $this->blank_line_check = (bool) $this->blank_line_check; + $this->blank_line_after_check = (bool) $this->blank_line_after_check; $tokens = $phpcsFile->getTokens();