Skip to content

Commit 2aa0e09

Browse files
committed
Don't record line length metrics for empty lines + some additional fixes to detect mixed indents
1 parent bbe55a5 commit 2aa0e09

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

src/Standards/Generic/Sniffs/WhiteSpace/DisallowSpaceIndentSniff.php

+20-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public function process(File $phpcsFile, $stackPtr)
8888
$content = $tokens[$i]['content'];
8989
}
9090

91+
$recordMetrics = true;
92+
9193
// If this is an inline HTML token, split the content into
9294
// indentation whitespace and the actual HTML/text.
9395
$nonWhitespace = '';
@@ -105,6 +107,9 @@ public function process(File $phpcsFile, $stackPtr)
105107
// There is no content after this whitespace except for a newline.
106108
$content = rtrim($content, "\r\n");
107109
$nonWhitespace = $phpcsFile->eolChar;
110+
111+
// Don't record metrics for empty lines.
112+
$recordMetrics = false;
108113
}
109114

110115
$hasSpaces = strpos($content, ' ');
@@ -117,7 +122,10 @@ public function process(File $phpcsFile, $stackPtr)
117122

118123
if ($hasSpaces === false && $hasTabs !== false) {
119124
// All ok, nothing to do.
120-
$phpcsFile->recordMetric($i, 'Line indent', 'tabs');
125+
if ($recordMetrics === true) {
126+
$phpcsFile->recordMetric($i, 'Line indent', 'tabs');
127+
}
128+
121129
continue;
122130
}
123131

@@ -136,7 +144,9 @@ public function process(File $phpcsFile, $stackPtr)
136144
$tabAfterSpaces = strpos($content, "\t", $hasSpaces);
137145

138146
if ($hasTabs === false) {
139-
$phpcsFile->recordMetric($i, 'Line indent', 'spaces');
147+
if ($recordMetrics === true) {
148+
$phpcsFile->recordMetric($i, 'Line indent', 'spaces');
149+
}
140150

141151
if ($numTabs === 0) {
142152
// Ignore: precision indentation.
@@ -145,14 +155,20 @@ public function process(File $phpcsFile, $stackPtr)
145155
} else {
146156
if ($numTabs === 0) {
147157
// Precision indentation.
148-
$phpcsFile->recordMetric($i, 'Line indent', 'tabs');
158+
if ($recordMetrics === true) {
159+
if ($tabAfterSpaces !== false) {
160+
$phpcsFile->recordMetric($i, 'Line indent', 'mixed');
161+
} else {
162+
$phpcsFile->recordMetric($i, 'Line indent', 'tabs');
163+
}
164+
}
149165

150166
if ($tabAfterSpaces === false) {
151167
// Ignore: precision indentation is already at the
152168
// end of the whitespace.
153169
continue;
154170
}
155-
} else {
171+
} else if ($recordMetrics === true) {
156172
$phpcsFile->recordMetric($i, 'Line indent', 'mixed');
157173
}
158174
}//end if

src/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,38 @@ public function process(File $phpcsFile, $stackPtr)
8383
continue;
8484
}
8585

86+
$recordMetrics = true;
87+
if (isset($tokens[($i + 1)]) === true
88+
&& $tokens[$i]['line'] < $tokens[($i + 1)]['line']
89+
) {
90+
// Don't record metrics for empty lines.
91+
$recordMetrics = false;
92+
}
93+
8694
$tabFound = false;
8795
if ($tokens[$i]['column'] === 1) {
8896
if ($content[0] === "\t") {
8997
$tabFound = true;
90-
if (strpos($content, ' ') !== false) {
91-
$phpcsFile->recordMetric($i, 'Line indent', 'mixed');
92-
} else {
93-
$phpcsFile->recordMetric($i, 'Line indent', 'tabs');
98+
if ($recordMetrics === true) {
99+
$spacePosition = strpos($content, ' ');
100+
$tabAfterSpaces = strpos($content, "\t", $spacePosition);
101+
if ($spacePosition !== false && $tabAfterSpaces !== false) {
102+
$phpcsFile->recordMetric($i, 'Line indent', 'mixed');
103+
} else {
104+
$phpcsFile->recordMetric($i, 'Line indent', 'tabs');
105+
}
94106
}
95107
} else if ($content[0] === ' ') {
96108
if (strpos($content, "\t") !== false) {
97-
$phpcsFile->recordMetric($i, 'Line indent', 'mixed');
109+
if ($recordMetrics === true) {
110+
$phpcsFile->recordMetric($i, 'Line indent', 'mixed');
111+
}
112+
98113
$tabFound = true;
99-
} else {
114+
} else if ($recordMetrics === true) {
100115
$phpcsFile->recordMetric($i, 'Line indent', 'spaces');
101116
}
102-
}
117+
}//end if
103118
} else {
104119
// Look for tabs so we can report and replace, but don't
105120
// record any metrics about them because they aren't

0 commit comments

Comments
 (0)