diff --git a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php index 1e2df37d86..d5a84982cd 100644 --- a/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php +++ b/src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php @@ -138,22 +138,26 @@ public function process(File $phpcsFile, $stackPtr) return; } - // We are looking for tabs, even if they have been replaced, because - // we enforce a space here. - if (isset($tokens[($openingBrace - 1)]['orig_content']) === true) { - $spacing = $tokens[($openingBrace - 1)]['orig_content']; - } else { - $spacing = $tokens[($openingBrace - 1)]['content']; - } - + // Enforce a single space. Tabs not allowed. + $spacing = $tokens[($openingBrace - 1)]['content']; if ($tokens[($openingBrace - 1)]['code'] !== T_WHITESPACE) { $length = 0; } else if ($spacing === "\t") { + // Tab without tab-width set, so no tab replacement has taken place. $length = '\t'; } else { $length = strlen($spacing); } + // If tab replacement is on, avoid confusing the user with a "expected 1 space, found 1" + // message when the "1" found is actually a tab, not a space. + if ($length === 1 + && isset($tokens[($openingBrace - 1)]['orig_content']) === true + && $tokens[($openingBrace - 1)]['orig_content'] === "\t" + ) { + $length = '\t'; + } + if ($length !== 1) { $error = 'Expected 1 space before opening brace; found %s'; $data = [$length]; diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc index a145bb9efe..36b5e8187d 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc @@ -9,3 +9,11 @@ function myFunction() { // Uses three tabs. function myFunction() { } + +// Uses one tab in a way that it translates to exactly one space with tab replacement. +function oneT() { +} + +// Mixed tabs and spaces. +function mixed() { +} diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc.fixed b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc.fixed index 515a93e50a..a3c47435de 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc.fixed +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.2.inc.fixed @@ -9,3 +9,11 @@ function myFunction() { // Uses three tabs. function myFunction() { } + +// Uses one tab in a way that it translates to exactly one space with tab replacement. +function oneT() { +} + +// Mixed tabs and spaces. +function mixed() { +} diff --git a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.php b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.php index 0c1a99300a..747d54d84c 100644 --- a/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.php +++ b/src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.php @@ -88,6 +88,8 @@ public function getErrorList($testFile='') return [ 6 => 1, 10 => 1, + 14 => 1, + 18 => 1, ]; default: return [];