Skip to content

Commit

Permalink
Merge pull request #707 from rodrigoprimo/test-converage-opening-func…
Browse files Browse the repository at this point in the history
…tion-brace-kernighan-ritchie

Generic/OpeningFunctionBraceKernighanRichie: improve test coverage
  • Loading branch information
jrfnl authored Nov 27, 2024
2 parents a13cdfd + 286cd81 commit 941a00e
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 54 deletions.
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

0 comments on commit 941a00e

Please sign in to comment.