Skip to content

Commit 012f46f

Browse files
committed
Add fixed TabIndent sniff.
1 parent b6a9649 commit 012f46f

File tree

6 files changed

+143
-30
lines changed

6 files changed

+143
-30
lines changed

PSR2R/Sniffs/Commenting/DocBlockParamSniff.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ private function getMethodSignature(File $phpCsFile, $stackPtr) {
157157
if ($possibleEqualIndex) {
158158
$possibleDefaultValue =
159159
$phpCsFile->findNext(
160-
[T_STRING, T_TRUE, T_FALSE, T_NULL, T_ARRAY],
161-
$possibleEqualIndex + 1,
162-
$possibleEqualIndex + 2,
163-
);
160+
[T_STRING, T_TRUE, T_FALSE, T_NULL, T_ARRAY],
161+
$possibleEqualIndex + 1,
162+
$possibleEqualIndex + 2,
163+
);
164164
if ($possibleDefaultValue) {
165165
$default = $possibleDefaultValue;
166166
}

PSR2R/Sniffs/Commenting/DocBlockVarWithoutNameSniff.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public function process(File $phpcsFile, $stackPtr) {
6666

6767
if (isset($matches[0][0]) && trim($matches[0][0]) !== '$this') {
6868
$fix = $phpcsFile->addFixableError(
69-
'@var annotations should not contain the variable name.',
70-
$i,
71-
'RemoveVarName',
72-
);
69+
'@var annotations should not contain the variable name.',
70+
$i,
71+
'RemoveVarName',
72+
);
7373
if ($fix) {
7474
$phpcsFile->fixer->replaceToken($nextIndex, str_replace($matches[0][0], '', $content));
7575
}

PSR2R/Sniffs/Namespaces/UnusedUseStatementSniff.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public function process(File $phpcsFile, $stackPtr) {
5757
}
5858

5959
$classUsed = $phpcsFile->findNext(
60-
[T_STRING, T_RETURN_TYPE],
61-
$classNameIndex + 1,
62-
null,
63-
false,
64-
$tokens[$classNameIndex]['content'],
65-
);
60+
[T_STRING, T_RETURN_TYPE],
61+
$classNameIndex + 1,
62+
null,
63+
false,
64+
$tokens[$classNameIndex]['content'],
65+
);
6666

6767
while ($classUsed !== false) {
6868
$beforeUsage = $phpcsFile->findPrevious(
@@ -83,12 +83,12 @@ public function process(File $phpcsFile, $stackPtr) {
8383
}
8484

8585
$classUsed = $phpcsFile->findNext(
86-
[T_STRING, T_RETURN_TYPE],
87-
$classUsed + 1,
88-
null,
89-
false,
90-
$tokens[$classNameIndex]['content'],
91-
);
86+
[T_STRING, T_RETURN_TYPE],
87+
$classUsed + 1,
88+
null,
89+
false,
90+
$tokens[$classNameIndex]['content'],
91+
);
9292
}
9393

9494
$warning = 'Unused use statement';
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace PSR2R\Sniffs\WhiteSpace;
4+
5+
use PHP_CodeSniffer\Files\File;
6+
use PHP_CodeSniffer\Sniffs\Sniff;
7+
8+
/**
9+
* Check for any line starting with 4 spaces - which would indicate space indenting.
10+
*
11+
* @author Mark Scherer
12+
* @license MIT
13+
*/
14+
class TabIndentSniff implements Sniff {
15+
16+
/**
17+
* A list of tokenizers this sniff supports.
18+
*
19+
* @var array
20+
*/
21+
public $supportedTokenizers = [
22+
'PHP',
23+
'JS',
24+
'CSS',
25+
];
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function register(): array {
31+
return [T_WHITESPACE, T_DOC_COMMENT_OPEN_TAG];
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function process(File $phpcsFile, $stackPtr) {
38+
$tokens = $phpcsFile->getTokens();
39+
40+
if ($tokens[$stackPtr]['code'] !== T_WHITESPACE) {
41+
// Doc block
42+
if (empty($tokens[$stackPtr]['comment_closer'])) {
43+
return;
44+
}
45+
46+
for ($i = $stackPtr + 1; $i < $tokens[$stackPtr]['comment_closer']; $i++) {
47+
if ($tokens[$i]['code'] === 'PHPCS_T_DOC_COMMENT_WHITESPACE' && $tokens[$i]['column'] === 1) {
48+
$this->fixTab($phpcsFile, $i, $tokens);
49+
} /** @noinspection NotOptimalIfConditionsInspection */ elseif ($tokens[$i]['code'] ===
50+
'PHPCS_T_DOC_COMMENT_WHITESPACE'
51+
) {
52+
$this->fixSpace($phpcsFile, $i, $tokens);
53+
}
54+
}
55+
56+
return;
57+
}
58+
59+
$line = $tokens[$stackPtr]['line'];
60+
if ($stackPtr > 0 && $tokens[$stackPtr - 1]['line'] === $line) {
61+
return;
62+
}
63+
64+
$this->fixTab($phpcsFile, $stackPtr, $tokens);
65+
}
66+
67+
/**
68+
* @param \PHP_CodeSniffer\Files\File $phpcsFile
69+
* @param int $stackPtr
70+
* @param array $tokens
71+
*
72+
* @return void
73+
*/
74+
protected function fixTab(File $phpcsFile, $stackPtr, $tokens) {
75+
$content = $tokens[$stackPtr]['orig_content'] ?? $tokens[$stackPtr]['content'];
76+
$tabs = 0;
77+
while (strpos($content, ' ') === 0) {
78+
$content = substr($content, 4);
79+
$tabs++;
80+
}
81+
82+
if ($tabs) {
83+
$error = ($tabs * 4) . ' spaces found, expected ' . $tabs . ' tabs';
84+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacesFound');
85+
if ($fix) {
86+
$phpcsFile->fixer->replaceToken($stackPtr, str_repeat("\t", $tabs) . $content);
87+
}
88+
}
89+
}
90+
91+
/**
92+
* @param \PHP_CodeSniffer\Files\File $phpcsFile
93+
* @param int $stackPtr
94+
* @param array $tokens
95+
*
96+
* @return void
97+
*/
98+
protected function fixSpace(File $phpcsFile, $stackPtr, $tokens) {
99+
$content = $tokens[$stackPtr]['content'];
100+
101+
$newContent = str_replace("\t", ' ', $content);
102+
103+
if ($newContent !== $content) {
104+
$error = 'Non-indentation (inline) tabs found, expected spaces';
105+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'TabsFound');
106+
if ($fix) {
107+
$phpcsFile->fixer->replaceToken($stackPtr, $newContent);
108+
}
109+
}
110+
}
111+
112+
}

PSR2R/Sniffs/WhiteSpace/UnaryOperatorSpacingSniff.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ protected function checkBefore(File $phpcsFile, $stackPtr) {
105105
}
106106

107107
$fix = $phpcsFile->addFixableError(
108-
'No whitespace should be between variable and incrementer.',
109-
$stackPtr,
110-
'WhitespaceIncrementer',
111-
);
108+
'No whitespace should be between variable and incrementer.',
109+
$stackPtr,
110+
'WhitespaceIncrementer',
111+
);
112112
if ($fix) {
113113
$phpcsFile->fixer->replaceToken($stackPtr - 1, '');
114114
}
@@ -131,10 +131,10 @@ protected function checkAfter(File $phpcsFile, $stackPtr) {
131131
}
132132

133133
$fix = $phpcsFile->addFixableError(
134-
'No whitespace should be between incrementer and variable.',
135-
$stackPtr,
136-
'WhitespaceVariable',
137-
);
134+
'No whitespace should be between incrementer and variable.',
135+
$stackPtr,
136+
'WhitespaceVariable',
137+
);
138138
if ($fix) {
139139
$phpcsFile->fixer->replaceToken($stackPtr + 1, '');
140140
}

docs/sniffs.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PSR2R Code Sniffer
22

3-
The PSR2R standard contains 185 sniffs
3+
The PSR2R standard contains 186 sniffs
44

55
Generic (22 sniffs)
66
-------------------
@@ -58,7 +58,7 @@ PSR2 (6 sniffs)
5858
- PSR2.Namespaces.NamespaceDeclaration
5959
- PSR2.Namespaces.UseDeclaration
6060

61-
PSR2R (44 sniffs)
61+
PSR2R (45 sniffs)
6262
-----------------
6363
- PSR2R.Classes.BraceOnSameLine
6464
- PSR2R.Classes.InterfaceName
@@ -103,6 +103,7 @@ PSR2R (44 sniffs)
103103
- PSR2R.WhiteSpace.MethodSpacing
104104
- PSR2R.WhiteSpace.NamespaceSpacing
105105
- PSR2R.WhiteSpace.TabAndSpace
106+
- PSR2R.WhiteSpace.TabIndent
106107
- PSR2R.WhiteSpace.UnaryOperatorSpacing
107108

108109
SlevomatCodingStandard (40 sniffs)

0 commit comments

Comments
 (0)