Skip to content

Commit 941a00e

Browse files
authored
Merge pull request #707 from rodrigoprimo/test-converage-opening-function-brace-kernighan-ritchie
Generic/OpeningFunctionBraceKernighanRichie: improve test coverage
2 parents a13cdfd + 286cd81 commit 941a00e

7 files changed

+144
-54
lines changed

src/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,9 @@ public function process(File $phpcsFile, $stackPtr)
7272
}
7373

7474
$openingBrace = $tokens[$stackPtr]['scope_opener'];
75-
$closeBracket = $tokens[$stackPtr]['parenthesis_closer'];
76-
if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
77-
$use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']);
78-
if ($use !== false) {
79-
$openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
80-
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
81-
}
82-
}
8375

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

8779
$functionLine = $tokens[$prev]['line'];
8880
$braceLine = $tokens[$openingBrace]['line'];
@@ -99,7 +91,6 @@ public function process(File $phpcsFile, $stackPtr)
9991
$error = 'Opening brace should be on the same line as the declaration';
10092
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnNewLine');
10193
if ($fix === true) {
102-
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), $closeBracket, true);
10394
$phpcsFile->fixer->beginChangeset();
10495
$phpcsFile->fixer->addContent($prev, ' {');
10596
$phpcsFile->fixer->replaceToken($openingBrace, '');
@@ -168,7 +159,7 @@ public function process(File $phpcsFile, $stackPtr)
168159
$data = [$length];
169160
$fix = $phpcsFile->addFixableError($error, $openingBrace, 'SpaceBeforeBrace', $data);
170161
if ($fix === true) {
171-
if ($length === 0 || $length === '\t') {
162+
if ($length === 0) {
172163
$phpcsFile->fixer->addContentBefore($openingBrace, ' ');
173164
} else {
174165
$phpcsFile->fixer->replaceToken(($openingBrace - 1), ' ');

src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc renamed to src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.1.inc

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ class myClass
2323
// Good.
2424
function myFunction() {
2525
}
26-
26+
2727
// Brace should be on same line.
2828
function myFunction()
2929
{
3030
}
31-
31+
3232
// Too many spaces.
3333
function myFunction() {
3434
}
35-
35+
3636
// Uses tab.
3737
function myFunction() {
3838
}
@@ -70,18 +70,18 @@ class myClass
7070
function myFunction($variable1, $variable2,
7171
$variable3, $variable4) {
7272
}
73-
73+
7474
// Brace should be on same line.
7575
function myFunction($variable1, $variable2,
7676
$variable3, $variable4)
7777
{
7878
}
79-
79+
8080
// Too many spaces.
8181
function myFunction($variable1, $variable2,
8282
$variable3, $variable4) {
8383
}
84-
84+
8585
// Uses tab.
8686
function myFunction($variable1, $variable2,
8787
$variable3, $variable4) {
@@ -212,3 +212,21 @@ function myFunction($a, $lot, $of, $params)
212212
function myFunction() {}
213213
function myFunction() {} // Too many spaces with an empty function.
214214
function myFunction() {} // Too many spaces (tab) with an empty function.
215+
216+
// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 0
217+
function shouldBeIgnored()
218+
{}
219+
// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 1
220+
221+
function dnfReturnType(): (Response&SuccessResponse)|AnotherResponse|string
222+
{}
223+
224+
function commentAfterOpeningBrace() { // Some comment.
225+
}
226+
227+
function variableAssignmentAfterOpeningBrace() { $a = 1;
228+
}
229+
230+
abstract class MyClass {
231+
abstract public function abstractMethod();
232+
}

src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.inc.fixed renamed to src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.1.inc.fixed

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ class myClass
2222
// Good.
2323
function myFunction() {
2424
}
25-
25+
2626
// Brace should be on same line.
2727
function myFunction() {
2828
}
29-
29+
3030
// Too many spaces.
3131
function myFunction() {
3232
}
33-
33+
3434
// Uses tab.
3535
function myFunction() {
3636
}
@@ -67,17 +67,17 @@ class myClass
6767
function myFunction($variable1, $variable2,
6868
$variable3, $variable4) {
6969
}
70-
70+
7171
// Brace should be on same line.
7272
function myFunction($variable1, $variable2,
7373
$variable3, $variable4) {
7474
}
75-
75+
7676
// Too many spaces.
7777
function myFunction($variable1, $variable2,
7878
$variable3, $variable4) {
7979
}
80-
80+
8181
// Uses tab.
8282
function myFunction($variable1, $variable2,
8383
$variable3, $variable4) {
@@ -200,3 +200,23 @@ function myFunction($a, $lot, $of, $params)
200200
function myFunction() {}
201201
function myFunction() {} // Too many spaces with an empty function.
202202
function myFunction() {} // Too many spaces (tab) with an empty function.
203+
204+
// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 0
205+
function shouldBeIgnored()
206+
{}
207+
// phpcs:set Generic.Functions.OpeningFunctionBraceKernighanRitchie checkFunctions 1
208+
209+
function dnfReturnType(): (Response&SuccessResponse)|AnotherResponse|string {
210+
}
211+
212+
function commentAfterOpeningBrace() {
213+
// Some comment.
214+
}
215+
216+
function variableAssignmentAfterOpeningBrace() {
217+
$a = 1;
218+
}
219+
220+
abstract class MyClass {
221+
abstract public function abstractMethod();
222+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
// Tests with tabs and the tabWidth config set to 4.
4+
5+
// Uses one tab.
6+
function myFunction() {
7+
}
8+
9+
// Uses three tabs.
10+
function myFunction() {
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
// Tests with tabs and the tabWidth config set to 4.
4+
5+
// Uses one tab.
6+
function myFunction() {
7+
}
8+
9+
// Uses three tabs.
10+
function myFunction() {
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing opening curly brace).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
function missingOpeningCurlyBrace()

src/Standards/Generic/Tests/Functions/OpeningFunctionBraceKernighanRitchieUnitTest.php

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,78 @@ final class OpeningFunctionBraceKernighanRitchieUnitTest extends AbstractSniffUn
2020
{
2121

2222

23+
/**
24+
* Get a list of CLI values to set before the file is tested.
25+
*
26+
* @param string $testFile The name of the file being tested.
27+
* @param \PHP_CodeSniffer\Config $config The config data for the test run.
28+
*
29+
* @return void
30+
*/
31+
public function setCliValues($testFile, $config)
32+
{
33+
if ($testFile === 'OpeningFunctionBraceKernighanRitchieUnitTest.2.inc') {
34+
$config->tabWidth = 4;
35+
}
36+
37+
}//end setCliValues()
38+
39+
2340
/**
2441
* Returns the lines where errors should occur.
2542
*
2643
* The key of the array should represent the line number and the value
2744
* should represent the number of errors that should occur on that line.
2845
*
46+
* @param string $testFile The name of the test file to process.
47+
*
2948
* @return array<int, int>
3049
*/
31-
public function getErrorList()
50+
public function getErrorList($testFile='')
3251
{
33-
return [
34-
9 => 1,
35-
13 => 1,
36-
17 => 1,
37-
29 => 1,
38-
33 => 1,
39-
37 => 1,
40-
53 => 1,
41-
58 => 1,
42-
63 => 1,
43-
77 => 1,
44-
82 => 1,
45-
87 => 1,
46-
104 => 1,
47-
119 => 1,
48-
123 => 1,
49-
127 => 1,
50-
132 => 1,
51-
137 => 1,
52-
142 => 1,
53-
157 => 1,
54-
162 => 1,
55-
171 => 1,
56-
181 => 1,
57-
191 => 1,
58-
197 => 1,
59-
203 => 1,
60-
213 => 1,
61-
214 => 1,
62-
];
52+
switch ($testFile) {
53+
case 'OpeningFunctionBraceKernighanRitchieUnitTest.1.inc':
54+
return [
55+
9 => 1,
56+
13 => 1,
57+
17 => 1,
58+
29 => 1,
59+
33 => 1,
60+
37 => 1,
61+
53 => 1,
62+
58 => 1,
63+
63 => 1,
64+
77 => 1,
65+
82 => 1,
66+
87 => 1,
67+
104 => 1,
68+
119 => 1,
69+
123 => 1,
70+
127 => 1,
71+
132 => 1,
72+
137 => 1,
73+
142 => 1,
74+
157 => 1,
75+
162 => 1,
76+
171 => 1,
77+
181 => 1,
78+
191 => 1,
79+
197 => 1,
80+
203 => 1,
81+
213 => 1,
82+
214 => 1,
83+
222 => 1,
84+
224 => 1,
85+
227 => 1,
86+
];
87+
case 'OpeningFunctionBraceKernighanRitchieUnitTest.2.inc':
88+
return [
89+
6 => 1,
90+
10 => 1,
91+
];
92+
default:
93+
return [];
94+
}//end switch
6395

6496
}//end getErrorList()
6597

0 commit comments

Comments
 (0)