From 2c46ef416a37db46d193084f6274734c52dcac4a Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Fri, 13 Oct 2017 15:08:34 +1100 Subject: [PATCH] More work to get DisallowTabIndent and DisallowSpaceIndent to report the same metrics --- .../WhiteSpace/DisallowTabIndentSniff.php | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php b/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php index 323427977f..c2ab0dd0c4 100644 --- a/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php +++ b/src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php @@ -26,6 +26,13 @@ class DisallowTabIndentSniff implements Sniff 'CSS', ); + /** + * The --tab-width CLI value that is being used. + * + * @var integer + */ + private $tabWidth = null; + /** * Returns an array of tokens this test wants to listen for. @@ -50,6 +57,15 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { + if ($this->tabWidth === null) { + if (isset($phpcsFile->config->tabWidth) === false || $phpcsFile->config->tabWidth === 0) { + // We have no idea how wide tabs are, so assume 4 spaces for metrics. + $this->tabWidth = 4; + } else { + $this->tabWidth = $phpcsFile->config->tabWidth; + } + } + $tokens = $phpcsFile->getTokens(); $error = 'Spaces must be used to indent lines; tabs are not allowed'; $errorCode = 'TabsUsed'; @@ -101,7 +117,15 @@ public function process(File $phpcsFile, $stackPtr) if ($spacePosition !== false && $tabAfterSpaces !== false) { $phpcsFile->recordMetric($i, 'Line indent', 'mixed'); } else { - $phpcsFile->recordMetric($i, 'Line indent', 'tabs'); + // Check for use of precision spaces. + $trimmed = str_replace(' ', '', $content); + $numSpaces = (strlen($content) - strlen($trimmed)); + $numTabs = (int) floor($numSpaces / $this->tabWidth); + if ($numTabs === 0) { + $phpcsFile->recordMetric($i, 'Line indent', 'tabs'); + } else { + $phpcsFile->recordMetric($i, 'Line indent', 'mixed'); + } } } } else if ($content[0] === ' ') {