@@ -49,35 +49,32 @@ public function process(File $phpcsFile, int $stackPtr): void {
4949 $ tokens = $ phpcsFile ->getTokens ();
5050
5151 $ line = $ tokens [$ stackPtr ]['line ' ];
52- if ($ stackPtr > 0 && $ tokens [$ stackPtr - 1 ]['line ' ] !== $ line ) {
52+ // Only check whitespace at the start of lines (indentation)
53+ if ($ stackPtr > 0 && $ tokens [$ stackPtr - 1 ]['line ' ] === $ line ) {
5354 return ;
5455 }
5556
56- if (strpos ($ tokens [$ stackPtr ]['content ' ], ' ' ) !== false ) {
57- $ error = 'Double space found ' ;
58- $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'DoubleSpace ' );
57+ $ content = $ tokens [$ stackPtr ]['orig_content ' ] ?? $ tokens [$ stackPtr ]['content ' ];
58+
59+ // Check for space followed by tab (wrong order)
60+ if (str_contains ($ content , ' ' . "\t" )) {
61+ $ error = 'Space followed by tab found in indentation; use tabs only ' ;
62+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'SpaceBeforeTab ' );
5963 if ($ fix ) {
60- $ phpcsFile ->fixer ->replaceToken ($ stackPtr , ' ' );
64+ // Replace space+tab patterns with just tabs
65+ $ phpcsFile ->fixer ->replaceToken ($ stackPtr , str_replace (" \t" , "\t" , $ content ));
6166 }
6267 }
63- if (strpos ($ tokens [$ stackPtr ]['content ' ], " \t" ) !== false ) {
64- $ error = 'Space and tab found ' ;
65- $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'SpaceAndTab ' );
68+
69+ // Check for tab followed by space (mixed indentation)
70+ if (str_contains ($ content , "\t " )) {
71+ $ error = 'Tab followed by space found in indentation; use tabs only ' ;
72+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'TabAndSpace ' );
6673 if ($ fix ) {
67- $ phpcsFile ->fixer ->replaceToken ($ stackPtr , ' ' );
74+ // Remove spaces after tabs at start of line
75+ $ phpcsFile ->fixer ->replaceToken ($ stackPtr , preg_replace ('/^(\t+) +/ ' , '$1 ' , $ content ));
6876 }
6977 }
70- if (strpos ($ tokens [$ stackPtr ]['content ' ], "\t " ) === false ) {
71- return ;
72- }
73-
74- $ error = 'Tab and space found ' ;
75- $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'TabAndSpace ' );
76- if (!$ fix ) {
77- return ;
78- }
79-
80- $ phpcsFile ->fixer ->replaceToken ($ stackPtr , ' ' );
8178 }
8279
8380}
0 commit comments