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/OpeningFunctionBraceKernighanRichie: improve test coverage #707

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 @@ -72,17 +72,9 @@ public function process(File $phpcsFile, $stackPtr)
}

$openingBrace = $tokens[$stackPtr]['scope_opener'];
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
$use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
if ($use !== false) {
$openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
}
}

// Find the end of the function declaration.
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), $closeBracket, true);
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), null, true);

$functionLine = $tokens[$prev]['line'];
$braceLine = $tokens[$openingBrace]['line'];
Expand All @@ -99,7 +91,6 @@ public function process(File $phpcsFile, $stackPtr)
$error = 'Opening brace should be on the same line as the declaration';
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnNewLine');
if ($fix === true) {
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), $closeBracket, true);
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContent($prev, ' {');
$phpcsFile->fixer->replaceToken($openingBrace, '');
Expand Down Expand Up @@ -168,7 +159,7 @@ public function process(File $phpcsFile, $stackPtr)
$data = [$length];
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'SpaceBeforeBrace', $data);
if ($fix === true) {
if ($length === 0 || $length === '\t') {
if ($length === 0) {
$phpcsFile->fixer->addContentBefore($openingBrace, ' ');
} else {
$phpcsFile->fixer->replaceToken(($openingBrace - 1), ' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ class myClass
// Good.
function myFunction() {
}

// Brace should be on same line.
function myFunction()
{
}

// Too many spaces.
function myFunction() {
}

// Uses tab.
function myFunction() {
}
Expand Down Expand Up @@ -70,18 +70,18 @@ class myClass
function myFunction($variable1, $variable2,
$variable3, $variable4) {
}

// Brace should be on same line.
function myFunction($variable1, $variable2,
$variable3, $variable4)
{
}

// Too many spaces.
function myFunction($variable1, $variable2,
$variable3, $variable4) {
}

// Uses tab.
function myFunction($variable1, $variable2,
$variable3, $variable4) {
Expand Down Expand Up @@ -212,3 +212,21 @@ function myFunction($a, $lot, $of, $params)
function myFunction() {}
function myFunction() {} // Too many spaces with an empty function.
function myFunction() {} // Too many spaces (tab) with an empty function.

// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 0
function shouldBeIgnored()
{}
// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 1

function dnfReturnType(): (Response&SuccessResponse)|AnotherResponse|string
{}

function commentAfterOpeningBrace() { // Some comment.
}

function variableAssignmentAfterOpeningBrace() { $a = 1;
}

abstract class MyClass {
abstract public function abstractMethod();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class myClass
// Good.
function myFunction() {
}

// Brace should be on same line.
function myFunction() {
}

// Too many spaces.
function myFunction() {
}

// Uses tab.
function myFunction() {
}
Expand Down Expand Up @@ -67,17 +67,17 @@ class myClass
function myFunction($variable1, $variable2,
$variable3, $variable4) {
}

// Brace should be on same line.
function myFunction($variable1, $variable2,
$variable3, $variable4) {
}

// Too many spaces.
function myFunction($variable1, $variable2,
$variable3, $variable4) {
}

// Uses tab.
function myFunction($variable1, $variable2,
$variable3, $variable4) {
Expand Down Expand Up @@ -200,3 +200,23 @@ function myFunction($a, $lot, $of, $params)
function myFunction() {}
function myFunction() {} // Too many spaces with an empty function.
function myFunction() {} // Too many spaces (tab) with an empty function.

// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 0
function shouldBeIgnored()
{}
// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 1

function dnfReturnType(): (Response&SuccessResponse)|AnotherResponse|string {
}

function commentAfterOpeningBrace() {
// Some comment.
}

function variableAssignmentAfterOpeningBrace() {
$a = 1;
}

abstract class MyClass {
abstract public function abstractMethod();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

// Tests with tabs and the tabWidth config set to 4.

// Uses one tab.
function myFunction() {
}

// Uses three tabs.
function myFunction() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

// Tests with tabs and the tabWidth config set to 4.

// Uses one tab.
function myFunction() {
}

// Uses three tabs.
function myFunction() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// Intentional parse error (missing opening curly brace).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.

function missingOpeningCurlyBrace()
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,78 @@ final class OpeningFunctionBraceKernighanRitchieUnitTest extends AbstractSniffUn
{


/**
* Get a list of CLI values to set before the file is tested.
*
* @param string $testFile The name of the file being tested.
* @param \PHP_CodeSniffer\Config $config The config data for the test run.
*
* @return void
*/
public function setCliValues($testFile, $config)
{
if ($testFile === 'OpeningFunctionBraceKernighanRitchieUnitTest.2.inc') {
$config->tabWidth = 4;
}

}//end setCliValues()


/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the test file to process.
*
* @return array<int, int>
*/
public function getErrorList()
public function getErrorList($testFile='')
{
return [
9 => 1,
13 => 1,
17 => 1,
29 => 1,
33 => 1,
37 => 1,
53 => 1,
58 => 1,
63 => 1,
77 => 1,
82 => 1,
87 => 1,
104 => 1,
119 => 1,
123 => 1,
127 => 1,
132 => 1,
137 => 1,
142 => 1,
157 => 1,
162 => 1,
171 => 1,
181 => 1,
191 => 1,
197 => 1,
203 => 1,
213 => 1,
214 => 1,
];
switch ($testFile) {
case 'OpeningFunctionBraceKernighanRitchieUnitTest.1.inc':
return [
9 => 1,
13 => 1,
17 => 1,
29 => 1,
33 => 1,
37 => 1,
53 => 1,
58 => 1,
63 => 1,
77 => 1,
82 => 1,
87 => 1,
104 => 1,
119 => 1,
123 => 1,
127 => 1,
132 => 1,
137 => 1,
142 => 1,
157 => 1,
162 => 1,
171 => 1,
181 => 1,
191 => 1,
197 => 1,
203 => 1,
213 => 1,
214 => 1,
222 => 1,
224 => 1,
227 => 1,
];
case 'OpeningFunctionBraceKernighanRitchieUnitTest.2.inc':
return [
6 => 1,
10 => 1,
];
default:
return [];
}//end switch

}//end getErrorList()

Expand Down