Skip to content

Commit 19ee670

Browse files
committed
The Squiz.Commenting.BlockComment sniff now supports tabs for indenting comment lines (request #1056)
1 parent 8e98973 commit 19ee670

File tree

5 files changed

+111
-8
lines changed

5 files changed

+111
-8
lines changed

CodeSniffer/Standards/Squiz/Sniffs/Commenting/BlockCommentSniff.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
class Squiz_Sniffs_Commenting_BlockCommentSniff implements PHP_CodeSniffer_Sniff
3131
{
3232

33+
/**
34+
* The --tab-width CLI value that is being used.
35+
*
36+
* @var int
37+
*/
38+
private $_tabWidth = null;
39+
3340

3441
/**
3542
* Returns an array of tokens this test wants to listen for.
@@ -57,6 +64,16 @@ public function register()
5764
*/
5865
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
5966
{
67+
if ($this->_tabWidth === null) {
68+
$cliValues = $phpcsFile->phpcs->cli->getCommandLineValues();
69+
if (isset($cliValues['tabWidth']) === false || $cliValues['tabWidth'] === 0) {
70+
// We have no idea how wide tabs are, so assume 4 spaces for fixing.
71+
$this->_tabWidth = 4;
72+
} else {
73+
$this->_tabWidth = $cliValues['tabWidth'];
74+
}
75+
}
76+
6077
$tokens = $phpcsFile->getTokens();
6178

6279
// If it's an inline comment, return.
@@ -169,17 +186,26 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
169186
$error = 'Block comment text must start on a new line';
170187
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoNewLine');
171188
if ($fix === true) {
189+
$indent = '';
190+
if ($tokens[($stackPtr - 1)]['code'] === T_WHITESPACE) {
191+
if (isset($tokens[($stackPtr - 1)]['orig_content']) === true) {
192+
$indent = $tokens[($stackPtr - 1)]['orig_content'];
193+
} else {
194+
$indent = $tokens[($stackPtr - 1)]['content'];
195+
}
196+
}
197+
172198
$comment = preg_replace(
173199
'/^(\s*\/\*\*?)/',
174-
'$1'.$phpcsFile->eolChar.' ',
200+
'$1'.$phpcsFile->eolChar.$indent,
175201
$tokens[$stackPtr]['content'],
176202
1
177203
);
178204
$phpcsFile->fixer->replaceToken($stackPtr, $comment);
179205
}
180206

181207
return;
182-
}
208+
}//end if
183209

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

@@ -209,10 +235,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
209235
$error = 'First line of comment not aligned correctly; expected %s but found %s';
210236
$fix = $phpcsFile->addFixableError($error, $commentLines[1], 'FirstLineIndent', $data);
211237
if ($fix === true) {
212-
$newContent = str_repeat(' ', $starColumn).ltrim($content);
213-
$phpcsFile->fixer->replaceToken($commentLines[1], $newContent);
238+
if (isset($tokens[$commentLines[1]]['orig_content']) === true
239+
&& $tokens[$commentLines[1]]['orig_content'][0] === "\t"
240+
) {
241+
// Line is indented using tabs.
242+
$padding = str_repeat("\t", floor($starColumn / $this->_tabWidth));
243+
} else {
244+
$padding = str_repeat(' ', $starColumn);
245+
}
246+
247+
$phpcsFile->fixer->replaceToken($commentLines[1], $padding.ltrim($content));
214248
}
215-
}
249+
}//end if
216250

217251
if (preg_match('/^\p{Ll}/u', $commentText) === 1) {
218252
$error = 'Block comments must start with a capital letter';
@@ -252,10 +286,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
252286
$error = 'Comment line indented incorrectly; expected at least %s but found %s';
253287
$fix = $phpcsFile->addFixableError($error, $line, 'LineIndent', $data);
254288
if ($fix === true) {
255-
$newContent = str_repeat(' ', $starColumn).ltrim($tokens[$line]['content']);
256-
$phpcsFile->fixer->replaceToken($line, $newContent);
289+
if (isset($tokens[$line]['orig_content']) === true
290+
&& $tokens[$line]['orig_content'][0] === "\t"
291+
) {
292+
// Line is indented using tabs.
293+
$padding = str_repeat("\t", floor($starColumn / $this->_tabWidth));
294+
} else {
295+
$padding = str_repeat(' ', $starColumn);
296+
}
297+
298+
$phpcsFile->fixer->replaceToken($line, $padding.ltrim($tokens[$line]['content']));
257299
}
258-
}
300+
}//end if
259301
}//end foreach
260302

261303
// Finally, test the last line is correct.

CodeSniffer/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,24 @@ class Foo
199199
var $bar = array();
200200

201201
}
202+
203+
class TabTest {
204+
public function bar() {
205+
/*
206+
* Comment line 1.
207+
* Comment line 2.
208+
*/
209+
210+
if ($foo) {
211+
echo 'foo';
212+
}
213+
214+
/* Comment line 1.
215+
Comment line 2.
216+
*/
217+
218+
if ($foo) {
219+
echo 'foo';
220+
}
221+
}
222+
}

CodeSniffer/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc.fixed

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,25 @@ class Foo
202202
var $bar = array();
203203

204204
}
205+
206+
class TabTest {
207+
public function bar() {
208+
/*
209+
* Comment line 1.
210+
* Comment line 2.
211+
*/
212+
213+
if ($foo) {
214+
echo 'foo';
215+
}
216+
217+
/*
218+
Comment line 1.
219+
Comment line 2.
220+
*/
221+
222+
if ($foo) {
223+
echo 'foo';
224+
}
225+
}
226+
}

CodeSniffer/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ class Squiz_Tests_Commenting_BlockCommentUnitTest extends AbstractSniffUnitTest
3232
{
3333

3434

35+
/**
36+
* Get a list of CLI values to set befor the file is tested.
37+
*
38+
* @param string $testFile The name of the file being tested.
39+
*
40+
* @return array
41+
*/
42+
public function getCliValues($testFile)
43+
{
44+
return array('--tab-width=4');
45+
46+
}//end getCliValues()
47+
48+
3549
/**
3650
* Returns the lines where errors should occur.
3751
*
@@ -71,6 +85,9 @@ public function getErrorList()
7185
159 => 1,
7286
181 => 1,
7387
188 => 1,
88+
206 => 1,
89+
207 => 1,
90+
214 => 1,
7491
);
7592

7693
return $errors;

package.xml

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

0 commit comments

Comments
 (0)