Skip to content

Commit 2c46ef4

Browse files
committed
More work to get DisallowTabIndent and DisallowSpaceIndent to report the same metrics
1 parent 2aa0e09 commit 2c46ef4

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class DisallowTabIndentSniff implements Sniff
2626
'CSS',
2727
);
2828

29+
/**
30+
* The --tab-width CLI value that is being used.
31+
*
32+
* @var integer
33+
*/
34+
private $tabWidth = null;
35+
2936

3037
/**
3138
* Returns an array of tokens this test wants to listen for.
@@ -50,6 +57,15 @@ public function register()
5057
*/
5158
public function process(File $phpcsFile, $stackPtr)
5259
{
60+
if ($this->tabWidth === null) {
61+
if (isset($phpcsFile->config->tabWidth) === false || $phpcsFile->config->tabWidth === 0) {
62+
// We have no idea how wide tabs are, so assume 4 spaces for metrics.
63+
$this->tabWidth = 4;
64+
} else {
65+
$this->tabWidth = $phpcsFile->config->tabWidth;
66+
}
67+
}
68+
5369
$tokens = $phpcsFile->getTokens();
5470
$error = 'Spaces must be used to indent lines; tabs are not allowed';
5571
$errorCode = 'TabsUsed';
@@ -101,7 +117,15 @@ public function process(File $phpcsFile, $stackPtr)
101117
if ($spacePosition !== false && $tabAfterSpaces !== false) {
102118
$phpcsFile->recordMetric($i, 'Line indent', 'mixed');
103119
} else {
104-
$phpcsFile->recordMetric($i, 'Line indent', 'tabs');
120+
// Check for use of precision spaces.
121+
$trimmed = str_replace(' ', '', $content);
122+
$numSpaces = (strlen($content) - strlen($trimmed));
123+
$numTabs = (int) floor($numSpaces / $this->tabWidth);
124+
if ($numTabs === 0) {
125+
$phpcsFile->recordMetric($i, 'Line indent', 'tabs');
126+
} else {
127+
$phpcsFile->recordMetric($i, 'Line indent', 'mixed');
128+
}
105129
}
106130
}
107131
} else if ($content[0] === ' ') {

0 commit comments

Comments
 (0)