Skip to content

Commit

Permalink
Universal/DisallowInlineTabs: handle more tokens
Browse files Browse the repository at this point in the history
While likely to be edge cases, there are a couple more tokens in which inline, non-indentation tabs can exist:
* `T_START_HEREDOC` and `T_START_NOWDOC` - between the `<<<` and the identifiers.
* `T_YIELD_FROM` - between the `yield` and the `from` keywords.

This updates the sniff to also handle those tokens.

Includes additional unit tests.

Loosely related to upstream changes which start doing tab replacement in these tokens too.
Note: this PR is _not_ dependent on the upstream changes and does not warrant raising the minimum supported PHPCS version.
  • Loading branch information
jrfnl committed Oct 23, 2024
1 parent dc888e7 commit 21709e1
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ final class DisallowInlineTabsSniff implements Sniff
\T_DOC_COMMENT_WHITESPACE => true,
\T_DOC_COMMENT_STRING => true,
\T_COMMENT => true,
\T_START_HEREDOC => true,
\T_START_NOWDOC => true,
\T_YIELD_FROM => true,
];

/**
Expand Down Expand Up @@ -102,7 +105,7 @@ public function process(File $phpcsFile, $stackPtr)
$dummy = new DummyTokenizer('', $phpcsFile->config);

for ($i = 0; $i < $phpcsFile->numTokens; $i++) {
// Skip all non-whitespace tokens and skip whitespace at the start of a new line.
// Skip all non-target tokens and skip whitespace at the start of a new line.
if (isset($this->find[$tokens[$i]['code']]) === false
|| (($tokens[$i]['code'] === \T_WHITESPACE
|| $tokens[$i]['code'] === \T_DOC_COMMENT_WHITESPACE)
Expand Down Expand Up @@ -147,6 +150,16 @@ public function process(File $phpcsFile, $stackPtr)
}
}

/*
* For "yield from", we should only handle tabs _between_ the keywords (single token),
* not indentation for those situations where the keyword is split in multiple tokens.
*/
if ($tokens[$i]['code'] === \T_YIELD_FROM
&& \preg_match('`^yield.+from$`i', $tokens[$i]['content']) !== 1
) {
continue;
}

$fix = $phpcsFile->addFixableError(
'Spaces must be used for mid-line alignment; tabs are not allowed',
$i,
Expand Down
21 changes: 21 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,24 @@ $aaaaaaaa = true;
// Tab indented, no tabs.
// phpcs:ignore Stnd.Cat.SniffName -- testing mixed comment + annotations don't trigger on indentation.
// Tab indented, no tabs.

$a = <<< TAB_BETWEEN
text
TAB_BETWEEN;

$a = <<< 'TABS_BETWEEN'
text
TABS_BETWEEN;

function myGenerator() {
yield from tabsBetweenShouldBeFixed();

yield /*comment*/ from tabsBetweenShouldBeFixedEvenWhenKeywordStartsOnColumn1();

yield
from tabIndentationShouldBeIgnored();

yield
/*comment*/
from tabIndentationShouldBeIgnored();
}
21 changes: 21 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,24 @@ $aaaaaaaa = true;
// Tab indented, no tabs.
// phpcs:ignore Stnd.Cat.SniffName -- testing mixed comment + annotations don't trigger on indentation.
// Tab indented, no tabs.

$a = <<< TAB_BETWEEN
text
TAB_BETWEEN;

$a = <<< 'TABS_BETWEEN'
text
TABS_BETWEEN;

function myGenerator() {
yield from tabsBetweenShouldBeFixed();

yield /*comment*/ from tabsBetweenShouldBeFixedEvenWhenKeywordStartsOnColumn1();

yield
from tabIndentationShouldBeIgnored();

yield
/*comment*/
from tabIndentationShouldBeIgnored();
}
14 changes: 14 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.4.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,17 @@ $aaaaaaaa = true;

// Tab indented and inline tabs.
// Tab indented and inline tabs.

$a = <<< TAB_BETWEEN
text
TAB_BETWEEN;

$a = <<< 'TABS_BETWEEN'
text
TABS_BETWEEN;

function myGenerator() {
yield from tabsBetweenShouldBeFixed();

yield /*comment*/ from tabsBetweenShouldBeFixedEvenWhenKeywordStartsOnColumn1();
}
14 changes: 14 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.4.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,17 @@ $aaaaaaaa = true;

// Tab indented and inline tabs.
// Tab indented and inline tabs.

$a = <<< TAB_BETWEEN
text
TAB_BETWEEN;

$a = <<< 'TABS_BETWEEN'
text
TABS_BETWEEN;

function myGenerator() {
yield from tabsBetweenShouldBeFixed();

yield /*comment*/ from tabsBetweenShouldBeFixedEvenWhenKeywordStartsOnColumn1();
}
14 changes: 14 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,17 @@ $aaaaaaaa = true;

// Tab indented and inline tabs.
// Tab indented and inline tabs.

$a = <<< TAB_BETWEEN
text
TAB_BETWEEN;

$a = <<< 'TABS_BETWEEN'
text
TABS_BETWEEN;

function myGenerator() {
yield from tabsBetweenShouldBeFixed();

yield /*comment*/ from tabsBetweenShouldBeFixedEvenWhenKeywordStartsOnColumn1();
}
14 changes: 14 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,17 @@ $aaaaaaaa = true;

// Tab indented and inline tabs.
// Tab indented and inline tabs.

$a = <<< TAB_BETWEEN
text
TAB_BETWEEN;

$a = <<< 'TABS_BETWEEN'
text
TABS_BETWEEN;

function myGenerator() {
yield from tabsBetweenShouldBeFixed();

yield /*comment*/ from tabsBetweenShouldBeFixedEvenWhenKeywordStartsOnColumn1();
}
21 changes: 21 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.6.inc
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,24 @@ $aaaaaaaa = true;
* @param int $var Description.
* @param string $string Another description.
*/

$a = <<< TAB_BETWEEN
text
TAB_BETWEEN;

$a = <<< 'TABS_BETWEEN'
text
TABS_BETWEEN;

function myGenerator() {
yield from tabsBetweenShouldBeFixed();

yield /*comment*/ from tabsBetweenShouldBeFixed();

yield
from tabIndentationShouldBeIgnored();

yield
/*comment*/
from tabIndentationShouldBeIgnored();
}
12 changes: 12 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public function getErrorList($testFile = '')
49 => 1,
52 => 1,
53 => 1,
63 => 1,
67 => 1,
72 => 1,
74 => 1,
];

case 'DisallowInlineTabsUnitTest.2.inc':
Expand Down Expand Up @@ -126,6 +130,10 @@ public function getErrorList($testFile = '')
31 => 1,
34 => 1,
35 => 1,
37 => 1,
41 => 1,
46 => 1,
48 => 1,
];

case 'DisallowInlineTabsUnitTest.5.inc':
Expand All @@ -145,6 +153,10 @@ public function getErrorList($testFile = '')
31 => 1,
34 => 1,
35 => 1,
37 => 1,
41 => 1,
46 => 1,
48 => 1,
];

default:
Expand Down

0 comments on commit 21709e1

Please sign in to comment.