Skip to content

Commit

Permalink
The Squiz.Commenting.BlockComment sniff now supports tabs for indenti…
Browse files Browse the repository at this point in the history
…ng comment lines (request squizlabs#1056)
  • Loading branch information
gsherwood committed Jul 5, 2016
1 parent 8e98973 commit 19ee670
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
class Squiz_Sniffs_Commenting_BlockCommentSniff implements PHP_CodeSniffer_Sniff
{

/**
* 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 @@ -57,6 +64,16 @@ 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.
$this->_tabWidth = 4;
} else {
$this->_tabWidth = $cliValues['tabWidth'];
}
}

$tokens = $phpcsFile->getTokens();

// If it's an inline comment, return.
Expand Down Expand Up @@ -169,17 +186,26 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$error = 'Block comment text must start on a new line';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoNewLine');
if ($fix === true) {
$indent = '';
if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
if (isset($tokens[($stackPtr - 1)]['orig_content']) === true) {
$indent = $tokens[($stackPtr - 1)]['orig_content'];
} else {
$indent = $tokens[($stackPtr - 1)]['content'];
}
}

$comment = preg_replace(
'/^(\s*\/\*\*?)/',
'$1'.$phpcsFile->eolChar.' ',
'$1'.$phpcsFile->eolChar.$indent,
$tokens[$stackPtr]['content'],
1
);
$phpcsFile->fixer->replaceToken($stackPtr, $comment);
}

return;
}
}//end if

$starColumn = ($tokens[$stackPtr]['column'] + 3);

Expand Down Expand Up @@ -209,10 +235,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$error = 'First line of comment not aligned correctly; expected %s but found %s';
$fix = $phpcsFile->addFixableError($error, $commentLines[1], 'FirstLineIndent', $data);
if ($fix === true) {
$newContent = str_repeat(' ', $starColumn).ltrim($content);
$phpcsFile->fixer->replaceToken($commentLines[1], $newContent);
if (isset($tokens[$commentLines[1]]['orig_content']) === true
&& $tokens[$commentLines[1]]['orig_content'][0] === "\t"
) {
// Line is indented using tabs.
$padding = str_repeat("\t", floor($starColumn / $this->_tabWidth));
} else {
$padding = str_repeat(' ', $starColumn);
}

$phpcsFile->fixer->replaceToken($commentLines[1], $padding.ltrim($content));
}
}
}//end if

if (preg_match('/^\p{Ll}/u', $commentText) === 1) {
$error = 'Block comments must start with a capital letter';
Expand Down Expand Up @@ -252,10 +286,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$error = 'Comment line indented incorrectly; expected at least %s but found %s';
$fix = $phpcsFile->addFixableError($error, $line, 'LineIndent', $data);
if ($fix === true) {
$newContent = str_repeat(' ', $starColumn).ltrim($tokens[$line]['content']);
$phpcsFile->fixer->replaceToken($line, $newContent);
if (isset($tokens[$line]['orig_content']) === true
&& $tokens[$line]['orig_content'][0] === "\t"
) {
// Line is indented using tabs.
$padding = str_repeat("\t", floor($starColumn / $this->_tabWidth));
} else {
$padding = str_repeat(' ', $starColumn);
}

$phpcsFile->fixer->replaceToken($line, $padding.ltrim($tokens[$line]['content']));
}
}
}//end if
}//end foreach

// Finally, test the last line is correct.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,24 @@ class Foo
var $bar = array();

}

class TabTest {
public function bar() {
/*
* Comment line 1.
* Comment line 2.
*/

if ($foo) {
echo 'foo';
}

/* Comment line 1.
Comment line 2.
*/

if ($foo) {
echo 'foo';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,25 @@ class Foo
var $bar = array();

}

class TabTest {
public function bar() {
/*
* Comment line 1.
* Comment line 2.
*/

if ($foo) {
echo 'foo';
}

/*
Comment line 1.
Comment line 2.
*/

if ($foo) {
echo 'foo';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ class Squiz_Tests_Commenting_BlockCommentUnitTest extends AbstractSniffUnitTest
{


/**
* Get a list of CLI values to set befor the file is tested.
*
* @param string $testFile The name of the file being tested.
*
* @return array
*/
public function getCliValues($testFile)
{
return array('--tab-width=4');

}//end getCliValues()


/**
* Returns the lines where errors should occur.
*
Expand Down Expand Up @@ -71,6 +85,9 @@ public function getErrorList()
159 => 1,
181 => 1,
188 => 1,
206 => 1,
207 => 1,
214 => 1,
);

return $errors;
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Version control reports now show which errors are fixable if you are showing sources
- Added a new sniff to enforce a single space after a NOT operator (request #1051)
-- Include in a ruleset using the code Generic.Formatting.SpaceAfterNot
- The Squiz.Commenting.BlockComment sniff now supports tabs for indenting comment lines (request #1056)
- Fixed bug #790 : Incorrect missing @throws error in methods that use closures
- Fixed bug #908 : PSR2 standard is not checking that closing brace is on line following the body
- Fixed bug #945 : Incorrect indent behavior using deep-nested function and arrays
Expand Down

0 comments on commit 19ee670

Please sign in to comment.