Skip to content

Commit 3361ff2

Browse files
authored
Merge pull request #509 from PHPCSStandards/feature/file-findstartendofstatement-add-extra-qa-test
File::find[Start|End]OfStatement(): add QA tests
2 parents fb351b3 + a82f02e commit 3361ff2

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

tests/Core/File/FindEndOfStatementTest.php

+37
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHP_CodeSniffer\Tests\Core\File;
1111

1212
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
13+
use PHP_CodeSniffer\Util\Tokens;
1314

1415
/**
1516
* Tests for the \PHP_CodeSniffer\Files\File::findEndOfStatement method.
@@ -20,6 +21,42 @@ final class FindEndOfStatementTest extends AbstractMethodUnitTest
2021
{
2122

2223

24+
/**
25+
* Test that end of statement is NEVER before the "current" token.
26+
*
27+
* @return void
28+
*/
29+
public function testEndIsNeverLessThanCurrentToken()
30+
{
31+
$tokens = self::$phpcsFile->getTokens();
32+
$errors = [];
33+
34+
for ($i = 0; $i < self::$phpcsFile->numTokens; $i++) {
35+
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
36+
continue;
37+
}
38+
39+
$end = self::$phpcsFile->findEndOfStatement($i);
40+
41+
// Collect all the errors.
42+
if ($end < $i) {
43+
$errors[] = sprintf(
44+
'End of statement for token %1$d (%2$s: %3$s) on line %4$d is %5$d (%6$s), which is less than %1$d',
45+
$i,
46+
$tokens[$i]['type'],
47+
$tokens[$i]['content'],
48+
$tokens[$i]['line'],
49+
$end,
50+
$tokens[$end]['type']
51+
);
52+
}
53+
}
54+
55+
$this->assertSame([], $errors);
56+
57+
}//end testEndIsNeverLessThanCurrentToken()
58+
59+
2360
/**
2461
* Test a simple assignment.
2562
*

tests/Core/File/FindStartOfStatementTest.inc

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ while(true) {}
1414
$a = 1;
1515

1616
/* testClosureAssignment */
17-
$a = function($b=false;){};
17+
$a = function($b=false){};
1818

1919
/* testHeredocFunctionArg */
2020
myFunction(<<<END
@@ -39,8 +39,8 @@ use Vendor\Package\{ClassA as A, ClassB, ClassC as C};
3939

4040
$a = [
4141
/* testArrowFunctionArrayValue */
42-
'a' => fn() => return 1,
43-
'b' => fn() => return 1,
42+
'a' => fn() => 1,
43+
'b' => fn() => 1,
4444
];
4545

4646
/* testStaticArrowFunction */
@@ -139,11 +139,11 @@ switch ($foo) {
139139
/* testInsideCaseStatement */
140140
$var = doSomething();
141141
/* testInsideCaseBreakStatement */
142-
break 2;
142+
break 1;
143143

144144
case 2:
145145
/* testInsideCaseContinueStatement */
146-
continue 2;
146+
continue 1;
147147

148148
case 3:
149149
/* testInsideCaseReturnStatement */

tests/Core/File/FindStartOfStatementTest.php

+39-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace PHP_CodeSniffer\Tests\Core\File;
1313

1414
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
15+
use PHP_CodeSniffer\Util\Tokens;
1516

1617
/**
1718
* Tests for the \PHP_CodeSniffer\Files\File:findStartOfStatement method.
@@ -22,6 +23,42 @@ final class FindStartOfStatementTest extends AbstractMethodUnitTest
2223
{
2324

2425

26+
/**
27+
* Test that start of statement is NEVER beyond the "current" token.
28+
*
29+
* @return void
30+
*/
31+
public function testStartIsNeverMoreThanCurrentToken()
32+
{
33+
$tokens = self::$phpcsFile->getTokens();
34+
$errors = [];
35+
36+
for ($i = 0; $i < self::$phpcsFile->numTokens; $i++) {
37+
if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
38+
continue;
39+
}
40+
41+
$start = self::$phpcsFile->findStartOfStatement($i);
42+
43+
// Collect all the errors.
44+
if ($start > $i) {
45+
$errors[] = sprintf(
46+
'Start of statement for token %1$d (%2$s: %3$s) on line %4$d is %5$d (%6$s), which is more than %1$d',
47+
$i,
48+
$tokens[$i]['type'],
49+
$tokens[$i]['content'],
50+
$tokens[$i]['line'],
51+
$start,
52+
$tokens[$start]['type']
53+
);
54+
}
55+
}
56+
57+
$this->assertSame([], $errors);
58+
59+
}//end testStartIsNeverMoreThanCurrentToken()
60+
61+
2562
/**
2663
* Test a simple assignment.
2764
*
@@ -92,7 +129,7 @@ public function testClosureAssignment()
92129
$start = $this->getTargetToken('/* testClosureAssignment */', T_CLOSE_CURLY_BRACKET);
93130
$found = self::$phpcsFile->findStartOfStatement($start);
94131

95-
$this->assertSame(($start - 12), $found);
132+
$this->assertSame(($start - 11), $found);
96133

97134
}//end testClosureAssignment()
98135

@@ -224,7 +261,7 @@ public function testArrowFunctionArrayValue()
224261
$start = $this->getTargetToken('/* testArrowFunctionArrayValue */', T_COMMA);
225262
$found = self::$phpcsFile->findStartOfStatement($start);
226263

227-
$this->assertSame(($start - 9), $found);
264+
$this->assertSame(($start - 7), $found);
228265

229266
}//end testArrowFunctionArrayValue()
230267

0 commit comments

Comments
 (0)