diff --git a/CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/MultiLineConditionSniff.php b/CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/MultiLineConditionSniff.php index 1d0f88d318..884c8b4774 100644 --- a/CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/MultiLineConditionSniff.php +++ b/CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/MultiLineConditionSniff.php @@ -46,6 +46,26 @@ class PEAR_Sniffs_ControlStructures_MultiLineConditionSniff implements PHP_CodeS public $indent = 4; + /** + * Should tabs be used for indenting? + * + * If TRUE, fixes will be made using tabs instead of spaces. + * The size of each tab is important, so it should be specified + * using the --tab-width CLI argument. + * + * @var bool + */ + public $tabIndent = false; + + + /** + * The --tab-width CLI value that is being used. + * + * @var int + */ + private $_tabWidth = null; + + /** * Returns an array of tokens this test wants to listen for. * @@ -78,6 +98,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) return; } + if ($this->_tabWidth === null) { + $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); + if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { + // We have no idea how wide tabs are, so assume 4 spaces for fixing. + // It shouldn't really matter because indent checks elsewhere in the + // standard should fix things up. + $this->_tabWidth = 4; + } else { + $this->_tabWidth = $cliValues['tabWidth']; + } + } + $openBracket = $tokens[$stackPtr]['parenthesis_opener']; $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; $spaceAfterOpen = 0; @@ -169,22 +201,41 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } if ($expectedIndent !== $foundIndent) { - $error = 'Multi-line IF statement not indented correctly; expected %s spaces but found %s'; - $data = array( - $expectedIndent, - $foundIndent, - ); + $error = 'Multi-line IF statement not indented correctly; expected '; + if ($this->tabIndent === true) { + $error .= '%s tabs, found %s'; + $data = array( + floor($expectedIndent / $this->_tabWidth), + floor($foundIndent / $this->_tabWidth), + ); + } else { + $error .= '%s spaces but found %s'; + $data = array( + $expectedIndent, + $foundIndent, + ); + } $fix = $phpcsFile->addFixableError($error, $i, 'Alignment', $data); if ($fix === true) { - $spaces = str_repeat(' ', $expectedIndent); + $spaces = ''; + if ($this->tabIndent === true) { + $numTabs = floor($expectedIndent / $this->_tabWidth); + if ($numTabs > 0) { + $numSpaces = ($expectedIndent - ($numTabs * $this->_tabWidth)); + $spaces = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); + } + } else if ($expectedIndent > 0) { + $spaces = str_repeat(' ', $expectedIndent); + } + if ($foundIndent === 0) { $phpcsFile->fixer->addContentBefore($i, $spaces); } else { $phpcsFile->fixer->replaceToken($i, $spaces); } } - } + }//end if $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $i, null, true); if ($next !== $closeBracket) { diff --git a/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php b/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php index 88f0459f11..4e3e6e1c8b 100644 --- a/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php +++ b/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php @@ -67,6 +67,26 @@ class PEAR_Sniffs_Functions_FunctionCallSignatureSniff implements PHP_CodeSniffe public $requiredSpacesBeforeClose = 0; + /** + * Should tabs be used for indenting? + * + * If TRUE, fixes will be made using tabs instead of spaces. + * The size of each tab is important, so it should be specified + * using the --tab-width CLI argument. + * + * @var bool + */ + public $tabIndent = false; + + + /** + * The --tab-width CLI value that is being used. + * + * @var int + */ + private $_tabWidth = null; + + /** * Returns an array of tokens this test wants to listen for. * @@ -92,6 +112,19 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen; $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose; + + if ($this->_tabWidth === null) { + $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); + if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { + // We have no idea how wide tabs are, so assume 4 spaces for fixing. + // It shouldn't really matter because indent checks elsewhere in the + // standard should fix things up. + $this->_tabWidth = 4; + } else { + $this->_tabWidth = $cliValues['tabWidth']; + } + } + $tokens = $phpcsFile->getTokens(); // Find the next non-empty token. @@ -451,15 +484,34 @@ public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr, || ($inArg === false && $expectedIndent !== $foundIndent) ) { - $error = 'Multi-line function call not indented correctly; expected %s spaces but found %s'; - $data = array( - $expectedIndent, - $foundIndent, - ); + $error = 'Multi-line function call not indented correctly; expected '; + if ($this->tabIndent === true) { + $error .= '%s tabs, found %s'; + $data = array( + floor($expectedIndent / $this->_tabWidth), + floor($foundIndent / $this->_tabWidth), + ); + } else { + $error .= '%s spaces but found %s'; + $data = array( + $expectedIndent, + $foundIndent, + ); + } $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data); if ($fix === true) { - $padding = str_repeat(' ', $expectedIndent); + $padding = ''; + if ($this->tabIndent === true) { + $numTabs = floor($expectedIndent / $this->_tabWidth); + if ($numTabs > 0) { + $numSpaces = ($expectedIndent - ($numTabs * $this->_tabWidth)); + $padding = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); + } + } else if ($expectedIndent > 0) { + $padding = str_repeat(' ', $expectedIndent); + } + if ($foundIndent === 0) { $phpcsFile->fixer->addContentBefore($i, $padding); } else { diff --git a/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php b/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php index c681257d9e..87d91f58b7 100644 --- a/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php +++ b/CodeSniffer/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php @@ -36,6 +36,26 @@ class PEAR_Sniffs_Functions_FunctionDeclarationSniff implements PHP_CodeSniffer_ public $indent = 4; + /** + * Should tabs be used for indenting? + * + * If TRUE, fixes will be made using tabs instead of spaces. + * The size of each tab is important, so it should be specified + * using the --tab-width CLI argument. + * + * @var bool + */ + public $tabIndent = false; + + + /** + * The --tab-width CLI value that is being used. + * + * @var int + */ + private $_tabWidth = null; + + /** * Returns an array of tokens this test wants to listen for. * @@ -62,6 +82,18 @@ public function register() */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { + if ($this->_tabWidth === null) { + $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); + if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { + // We have no idea how wide tabs are, so assume 4 spaces for fixing. + // It shouldn't really matter because indent checks elsewhere in the + // standard should fix things up. + $this->_tabWidth = 4; + } else { + $this->_tabWidth = $cliValues['tabWidth']; + } + } + $tokens = $phpcsFile->getTokens(); if (isset($tokens[$stackPtr]['parenthesis_opener']) === false @@ -351,22 +383,41 @@ public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $st } if ($expectedIndent !== $foundIndent) { - $error = 'Multi-line function declaration not indented correctly; expected %s spaces but found %s'; - $data = array( - $expectedIndent, - $foundIndent, - ); + $error = 'Multi-line function declaration not indented correctly; expected '; + if ($this->tabIndent === true) { + $error .= '%s tabs, found %s'; + $data = array( + floor($expectedIndent / $this->_tabWidth), + floor($foundIndent / $this->_tabWidth), + ); + } else { + $error .= '%s spaces but found %s'; + $data = array( + $expectedIndent, + $foundIndent, + ); + } $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data); if ($fix === true) { - $spaces = str_repeat(' ', $expectedIndent); + $spaces = ''; + if ($this->tabIndent === true) { + $numTabs = floor($expectedIndent / $this->_tabWidth); + if ($numTabs > 0) { + $numSpaces = ($expectedIndent - ($numTabs * $this->_tabWidth)); + $spaces = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); + } + } else if ($expectedIndent > 0) { + $spaces = str_repeat(' ', $expectedIndent); + } + if ($foundIndent === 0) { $phpcsFile->fixer->addContentBefore($i, $spaces); } else { $phpcsFile->fixer->replaceToken($i, $spaces); } } - } + }//end if $lastLine = $tokens[$i]['line']; }//end if diff --git a/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php b/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php index e82790d9f8..53c4d0a2f3 100644 --- a/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php +++ b/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php @@ -37,6 +37,26 @@ class PEAR_Sniffs_WhiteSpace_ObjectOperatorIndentSniff implements PHP_CodeSniffe public $indent = 4; + /** + * Should tabs be used for indenting? + * + * If TRUE, fixes will be made using tabs instead of spaces. + * The size of each tab is important, so it should be specified + * using the --tab-width CLI argument. + * + * @var bool + */ + public $tabIndent = false; + + + /** + * The --tab-width CLI value that is being used. + * + * @var int + */ + private $_tabWidth = null; + + /** * Returns an array of tokens this test wants to listen for. * @@ -60,6 +80,18 @@ public function register() */ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { + if ($this->_tabWidth === null) { + $cliValues = $phpcsFile->phpcs->cli->getCommandLineValues(); + if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) { + // We have no idea how wide tabs are, so assume 4 spaces for fixing. + // It shouldn't really matter because indent checks elsewhere in the + // standard should fix things up. + $this->_tabWidth = 4; + } else { + $this->_tabWidth = $cliValues['tabWidth']; + } + } + $tokens = $phpcsFile->getTokens(); // Make sure this is the first object operator in a chain of them. @@ -142,22 +174,41 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) } if ($foundIndent !== $requiredIndent) { - $error = 'Object operator not indented correctly; expected %s spaces but found %s'; - $data = array( - $requiredIndent, - $foundIndent, - ); + $error = 'Object operator not indented correctly; expected '; + if ($this->tabIndent === true) { + $error .= '%s tabs, found %s'; + $data = array( + floor($requiredIndent / $this->_tabWidth), + floor($foundIndent / $this->_tabWidth), + ); + } else { + $error .= '%s spaces but found %s'; + $data = array( + $requiredIndent, + $foundIndent, + ); + } $fix = $phpcsFile->addFixableError($error, $next, 'Incorrect', $data); if ($fix === true) { - $spaces = str_repeat(' ', $requiredIndent); + $spaces = ''; + if ($this->tabIndent === true) { + $numTabs = floor($requiredIndent / $this->_tabWidth); + if ($numTabs > 0) { + $numSpaces = ($requiredIndent - ($numTabs * $this->_tabWidth)); + $spaces = str_repeat("\t", $numTabs).str_repeat(' ', $numSpaces); + } + } else if ($requiredIndent > 0) { + $spaces = str_repeat(' ', $requiredIndent); + } + if ($foundIndent === 0) { $phpcsFile->fixer->addContentBefore($next, $spaces); } else { $phpcsFile->fixer->replaceToken(($next - 1), $spaces); } } - } + }//end if }//end if // It cant be the last thing on the line either.