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: add extra tests + remove redundancy #217

Merged
merged 2 commits into from
Mar 26, 2023
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: 9 additions & 6 deletions Universal/Sniffs/WhiteSpace/DisallowInlineTabsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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']]))
Expand Down
28 changes: 14 additions & 14 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.5.inc.fixed
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
<?php
/* *** TESTING WITHOUT A TAB WIDTH SET *** */
/**
* @param int $var Description - Bad: alignment using tabs.
* @param int $var Description - Bad: alignment using tabs.
* @param string $string Another description.
*/

$expected = ( $column - 1 );
$found = ( $this->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.
43 changes: 43 additions & 0 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.6.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/* *** SPACE BASED FILE - NO ERRORS OR WARNINGS EXPECTED AT ALL *** */

/**
* @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',
);

/**
* @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.
*/
4 changes: 2 additions & 2 deletions Universal/Tests/WhiteSpace/DisallowInlineTabsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down