Skip to content

Commit 1e9a351

Browse files
committed
Fix NPE in NamespaceAndUseStatementCheck
1 parent c021ab3 commit 1e9a351

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

php-checks/src/main/java/org/sonar/php/checks/formatting/NamespaceAndUseStatementCheck.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,11 @@ public void visitScript(ScriptTree tree) {
6969
nextStatement = statements.get(i + 1);
7070
statements.get(i).accept(this);
7171
}
72-
73-
nextStatement = null;
74-
statements.get(nbStatements - 1).accept(this);
75-
7672
}
7773

78-
7974
@Override
8075
public void visitNamespaceStatement(NamespaceStatementTree tree) {
81-
if (check.hasNamespaceBlankLine && nextStatement != null && !isFollowedWithBlankLine(tree)) {
76+
if (check.hasNamespaceBlankLine && !isFollowedWithBlankLine(tree)) {
8277
String message = String.format(
8378
BLANK_LINE_NAMESPACE_MESSAGE,
8479
tree.namespaceName() == null ? "" : " " + tree.namespaceName().fullName());
@@ -90,7 +85,7 @@ public void visitNamespaceStatement(NamespaceStatementTree tree) {
9085
public void visitUseStatement(UseStatementTree tree) {
9186
useStatements.add(tree);
9287

93-
if (nextStatement != null && !nextStatement.is(USE_KINDS)) {
88+
if (!nextStatement.is(USE_KINDS)) {
9489
checkUsesAreBeforeNamespace();
9590
checkBlankLineAfterUses(tree);
9691
useStatements.clear();

php-checks/src/test/java/org/sonar/php/checks/formatting/NamespaceAndUseStatementCheckTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ public void custom() throws Exception {
4040
deactivateAll();
4141
PHPCheckTest.check(check, TestUtils.getCheckFile(TEST_DIR + "NamespaceAndUseStatementCheck.php"), ImmutableList.<Issue>of());
4242
}
43+
44+
@Test
45+
public void emptyScript() throws Exception {
46+
activeOnly("hasNamespaceBlankLine", "isUseAfterNamespace", "hasUseBlankLine");
47+
PHPCheckTest.check(check, TestUtils.getCheckFile(TEST_DIR + "empty-script.php"));
48+
}
49+
4350
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

0 commit comments

Comments
 (0)