diff --git a/Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php b/Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php index 284bcd98..d91ea3d3 100644 --- a/Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php +++ b/Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php @@ -94,6 +94,10 @@ public function process(File $phpcsFile, $stackPtr) $this->tabWidth = Helper::getTabWidth($phpcsFile); } + if (defined('PHP_CODESNIFFER_IN_TESTS')) { + $this->tabWidth = Helper::getCommandLineData($phpcsFile, 'tabWidth'); + } + $tokens = $phpcsFile->getTokens(); $dummy = new DummyTokenizer('', $phpcsFile->config); @@ -115,16 +119,15 @@ public function process(File $phpcsFile, $stackPtr) continue; } - $dummy->replaceTabsInToken($token, ' ', ' ', $this->tabWidth); + $dummy->replaceTabsInToken($token); } + /* + * Tokens only have the 'orig_content' key if they contain tabs, + * so from here on out, we **know** there will be tabs in the content. + */ $origContent = $token['orig_content']; - if ($origContent === '' || \strpos($origContent, "\t") === false) { - // If there are no tabs, we can continue, no matter what. - continue; - } - $multiLineComment = false; if (($tokens[$i]['code'] === \T_COMMENT || isset(Tokens::$phpcsCommentTokens[$tokens[$i]['code']])) diff --git a/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed index ceb402c3..ce001601 100644 --- a/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed +++ b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed @@ -1,35 +1,35 @@ tokens[ $closer ]['column'] - 1 ); // Bad. - $error = 'Array closer not aligned correctly; expected %s space(s) but found %s'; // Bad. - $data = array( // Bad. + $found = ( $this->tokens[ $closer ]['column'] - 1 ); // Bad. + $error = 'Array closer not aligned correctly; expected %s space(s) but found %s'; // Bad. + $data = array( // Bad. $expected_value => 'data', - $found => 'more_data', // Bad. + $found => 'more_data', // Bad. ); /* * Test that the tab replacements do not negatively influence existing mid-line alignments. */ -$a = true; -$aa = true; -$aaa = true; -$aaaa = true; -$aaaaa = true; +$a = true; +$aa = true; +$aaa = true; +$aaaa = true; +$aaaaa = true; $aaaaaa = true; $aaaaaaa = true; $aaaaaaaa = true; -// Test tab replacement in inline comments. +// Test tab replacement in inline comments. /* - * Tab indented and inline tabs. + * Tab indented and inline tabs. */ - // Tab indented and inline tabs. - // Tab indented and inline tabs. + // Tab indented and inline tabs. + // Tab indented and inline tabs. diff --git a/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.6.inc b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.6.inc new file mode 100644 index 00000000..05b55208 --- /dev/null +++ b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.6.inc @@ -0,0 +1,43 @@ +tokens[ $closer ]['column'] - 1 ); + $error = 'Array closer not aligned correctly; expected %s space(s) but found %s'; + $data = array( + $expected_value => 'data', + $found => 'more_data', + ); + +/** + * @param int $var Description + * @param string $string Another description. + */ + + $expected = ( $column - 1 ); + $found = ( $this->tokens[ $closer ]['column'] - 1 ); + $error = 'Array closer not aligned correctly; expected %s space(s) but found %s'; + $data = array( + $expected_value => 'data', + $found => 'more_data', + ); + +$a = true; +$aa = true; +$aaa = true; +$aaaa = true; +$aaaaa = true; +$aaaaaa = true; +$aaaaaaa = true; +$aaaaaaaa = true; + + /** + * @param int $var Description. + * @param string $string Another description. + */ diff --git a/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php index 3079a3ce..262127a9 100644 --- a/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php +++ b/Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php @@ -55,8 +55,8 @@ public function setCliValues($testFile, $config) return; } - if ($testFile === 'DisallowInlineTabsUnitTest.5.inc') { - // Set to the default. + if ($testFile === 'DisallowInlineTabsUnitTest.5.inc' || $testFile === 'DisallowInlineTabsUnitTest.6.inc') { + // Set to the default (results in tab width 1). $config->tabWidth = 0; return; }