@@ -26,6 +26,13 @@ class DisallowTabIndentSniff implements Sniff
26
26
'CSS ' ,
27
27
);
28
28
29
+ /**
30
+ * The --tab-width CLI value that is being used.
31
+ *
32
+ * @var integer
33
+ */
34
+ private $ tabWidth = null ;
35
+
29
36
30
37
/**
31
38
* Returns an array of tokens this test wants to listen for.
@@ -50,6 +57,15 @@ public function register()
50
57
*/
51
58
public function process (File $ phpcsFile , $ stackPtr )
52
59
{
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
+
53
69
$ tokens = $ phpcsFile ->getTokens ();
54
70
$ error = 'Spaces must be used to indent lines; tabs are not allowed ' ;
55
71
$ errorCode = 'TabsUsed ' ;
@@ -101,7 +117,15 @@ public function process(File $phpcsFile, $stackPtr)
101
117
if ($ spacePosition !== false && $ tabAfterSpaces !== false ) {
102
118
$ phpcsFile ->recordMetric ($ i , 'Line indent ' , 'mixed ' );
103
119
} 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
+ }
105
129
}
106
130
}
107
131
} else if ($ content [0 ] === ' ' ) {
0 commit comments