Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Universal/DisallowInlineTabs: handle more tokens #320

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
19 changes: 19 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSExtra\Universal\Tests\WhiteSpace;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
Expand Down Expand Up @@ -73,6 +74,12 @@ public function setCliValues($testFile, $config)
*/
public function getErrorList($testFile = '')
{
// As of PHP 8.3, comments may be tokenized within a "yield from" token, but only for PHPCS < 3.11.0.
$commentsInYieldFrom = false;
if (\PHP_VERSION_ID >= 80300 /*&& \version_compare(Config::VERSION, '3.11.0', '<')*/) {
$commentsInYieldFrom = true;
}

switch ($testFile) {
case 'DisallowInlineTabsUnitTest.1.inc':
return [
Expand All @@ -93,6 +100,10 @@ public function getErrorList($testFile = '')
49 => 1,
52 => 1,
53 => 1,
63 => 1,
67 => 1,
72 => 1,
74 => ($commentsInYieldFrom === true ? 1 : 2),
];

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

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

default:
Expand Down