Skip to content

Commit c88c241

Browse files
authored
Merge pull request #699 from rodrigoprimo/test-coverage-empty-php-statement
Generic/EmptyPHPStatement: improve code coverage
2 parents 609ac10 + eb7c3a9 commit c88c241

File tree

6 files changed

+117
-39
lines changed

6 files changed

+117
-39
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/EmptyPHPStatementSniff.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public function process(File $phpcsFile, $stackPtr)
5353
case 'T_SEMICOLON':
5454
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
5555

56-
if ($prevNonEmpty === false) {
57-
return;
58-
}
59-
6056
if ($tokens[$prevNonEmpty]['code'] !== T_SEMICOLON
6157
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG
6258
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG_WITH_ECHO
@@ -128,9 +124,8 @@ public function process(File $phpcsFile, $stackPtr)
128124
case 'T_CLOSE_TAG':
129125
$prevNonEmpty = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
130126

131-
if ($prevNonEmpty === false
132-
|| ($tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG
133-
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG_WITH_ECHO)
127+
if ($tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG
128+
&& $tokens[$prevNonEmpty]['code'] !== T_OPEN_TAG_WITH_ECHO
134129
) {
135130
return;
136131
}
@@ -150,10 +145,6 @@ public function process(File $phpcsFile, $stackPtr)
150145
$phpcsFile->fixer->endChangeset();
151146
}
152147
break;
153-
154-
default:
155-
// Deliberately left empty.
156-
break;
157148
}//end switch
158149

159150
}//end process()

src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc renamed to src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.1.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function_call();
1919

2020
?>
2121
<input name="<?php ; something_else(); ?>" />
22-
<input name="<?php something_else(); ; ?>" />
22+
<input name="<?php something_else(); ; ; ?>" />
2323

2424
/*
2525
* Test empty statement: no code between PHP open and close tag.
@@ -42,7 +42,7 @@ function_call();
4242
-->
4343
<?php ; ?>
4444

45-
<input name="<?php ; ?>" /> <!-- Bad. -->
45+
<input name="<?php ;; ?>" /> <!-- Bad. -->
4646

4747
<!-- Tests with short open echo tag. -->
4848
<input name="<?= 'some text' ?>" /> <!-- OK. -->
@@ -53,7 +53,7 @@ function_call();
5353
// Guard against false positives for two consecutive semicolons in a for statement.
5454
for ( $i = 0; ; $i++ ) {}
5555

56-
// Test for useless semicolons
56+
// Test for useless semicolons.
5757
for ( $i = 0; ; $i++ ) {};
5858

5959
if (true) {};
@@ -80,7 +80,7 @@ if ($foo) {
8080
;
8181
}
8282

83-
// Do not remove semicolon after match
83+
// Do not remove semicolon after match.
8484
$c = match ($a) {
8585
1 => true,
8686
};

src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.inc.fixed renamed to src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.1.inc.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function_call();
4848
// Guard against false positives for two consecutive semicolons in a for statement.
4949
for ( $i = 0; ; $i++ ) {}
5050

51-
// Test for useless semicolons
51+
// Test for useless semicolons.
5252
for ( $i = 0; ; $i++ ) {}
5353

5454
if (true) {}
@@ -74,7 +74,7 @@ echo $a{0};
7474
if ($foo) {
7575
}
7676

77-
// Do not remove semicolon after match
77+
// Do not remove semicolon after match.
7878
$c = match ($a) {
7979
1 => true,
8080
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!-- Tests with short open tag. -->
2+
3+
<input name="<? ; something_else(); ?>" />
4+
<input name="<? something_else(); ; ?>" />
5+
6+
/*
7+
* Test empty statement: no code between PHP open and close tag.
8+
*/
9+
<input name="<? something_else() ?>" /> <!-- OK. -->
10+
<input name="<? something_else(); ?>" /> <!-- OK. -->
11+
<input name="<? /* comment */ ?>" /> <!-- OK. -->
12+
13+
<input name="<? ?>" /> <!-- Bad. -->
14+
15+
<input name="<?
16+
17+
18+
?>" /> <!-- Bad. -->
19+
20+
<!--
21+
/*
22+
* Test detecting & fixing a combination of the two checks.
23+
*/
24+
-->
25+
<? ; ?>
26+
27+
<input name="<? ; ?>" /> <!-- Bad. -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!-- Tests with short open tag. -->
2+
3+
<input name="<?something_else(); ?>" />
4+
<input name="<? something_else(); ?>" />
5+
6+
/*
7+
* Test empty statement: no code between PHP open and close tag.
8+
*/
9+
<input name="<? something_else() ?>" /> <!-- OK. -->
10+
<input name="<? something_else(); ?>" /> <!-- OK. -->
11+
<input name="<? /* comment */ ?>" /> <!-- OK. -->
12+
13+
<input name="" /> <!-- Bad. -->
14+
15+
<input name="" /> <!-- Bad. -->
16+
17+
<!--
18+
/*
19+
* Test detecting & fixing a combination of the two checks.
20+
*/
21+
-->
22+
23+
<input name="" /> <!-- Bad. -->

src/Standards/Generic/Tests/CodeAnalysis/EmptyPHPStatementUnitTest.php

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ final class EmptyPHPStatementUnitTest extends AbstractSniffUnitTest
2020
{
2121

2222

23+
/**
24+
* Get a list of all test files to check.
25+
*
26+
* @param string $testFileBase The base path that the unit tests files will have.
27+
*
28+
* @return string[]
29+
*/
30+
protected function getTestFiles($testFileBase)
31+
{
32+
$testFiles = [$testFileBase.'1.inc'];
33+
34+
$option = (bool) ini_get('short_open_tag');
35+
if ($option === true) {
36+
$testFiles[] = $testFileBase.'2.inc';
37+
}
38+
39+
return $testFiles;
40+
41+
}//end getTestFiles()
42+
43+
2344
/**
2445
* Returns the lines where errors should occur.
2546
*
@@ -41,31 +62,47 @@ public function getErrorList()
4162
* The key of the array should represent the line number and the value
4263
* should represent the number of warnings that should occur on that line.
4364
*
65+
* @param string $testFile The name of the file being tested.
66+
*
4467
* @return array<int, int>
4568
*/
46-
public function getWarningList()
69+
public function getWarningList($testFile='')
4770
{
48-
return [
49-
9 => 1,
50-
12 => 1,
51-
15 => 1,
52-
18 => 1,
53-
21 => 1,
54-
22 => 1,
55-
31 => 1,
56-
33 => 1,
57-
43 => 1,
58-
45 => 1,
59-
49 => 1,
60-
50 => 1,
61-
57 => 1,
62-
59 => 1,
63-
61 => 1,
64-
63 => 2,
65-
71 => 1,
66-
72 => 1,
67-
80 => 1,
68-
];
71+
switch ($testFile) {
72+
case 'EmptyPHPStatementUnitTest.1.inc':
73+
return [
74+
9 => 1,
75+
12 => 1,
76+
15 => 1,
77+
18 => 1,
78+
21 => 1,
79+
22 => 2,
80+
31 => 1,
81+
33 => 1,
82+
43 => 1,
83+
45 => 2,
84+
49 => 1,
85+
50 => 1,
86+
57 => 1,
87+
59 => 1,
88+
61 => 1,
89+
63 => 2,
90+
71 => 1,
91+
72 => 1,
92+
80 => 1,
93+
];
94+
case 'EmptyPHPStatementUnitTest.2.inc':
95+
return [
96+
3 => 1,
97+
4 => 1,
98+
13 => 1,
99+
15 => 1,
100+
25 => 1,
101+
27 => 1,
102+
];
103+
default:
104+
return [];
105+
}//end switch
69106

70107
}//end getWarningList()
71108

0 commit comments

Comments
 (0)