Skip to content

Commit

Permalink
Allow using pear sniffs with tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonge committed Feb 19, 2017
1 parent 6746d9c commit c983e41
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c983e41

Please sign in to comment.