Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic/OpeningFunctionBraceKernighanRitchie: improve error message #736

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public function getErrorList($testFile='')
return [
6 => 1,
10 => 1,
14 => 1,
18 => 1,
];
default:
return [];
Expand Down