diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestion.java b/php/php.editor/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestion.java index 798977462bed..7e280b4623e3 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestion.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestion.java @@ -991,8 +991,12 @@ private static int getOffsetAfterNextTokenId(BaseDocument doc, int offset, PHPTo * @return The offset after the last use trait statement if traits are used, * otherwise the offset after the open curly for the type */ - private static int getOffsetAfterUseTrait(BaseDocument document, TypeScope typeScope) { + private static int getOffsetAfterUseTrait(BaseDocument document, TypeScope typeScope) throws BadLocationException { OffsetRange blockRange = typeScope.getBlockRange(); + if (blockRange == null) { + // GH-6258 Block range of ClassScope created from ClassElement is null + return getOffsetAfterClassOpenCurly(document, typeScope.getOffset()); + } int offset = blockRange.getEnd() - 1; // before close curly "}" Collection elements = new HashSet<>(); elements.addAll(typeScope.getDeclaredMethods()); diff --git a/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testGH6258/TestClass.php b/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testGH6258/TestClass.php new file mode 100644 index 000000000000..f598e9d2c9f0 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testGH6258/TestClass.php @@ -0,0 +1,21 @@ +generatedMethod(); diff --git a/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testGH6258/testGH6258_02.php.testGH6258_02.hints b/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testGH6258/testGH6258_02.php.testGH6258_02.hints new file mode 100644 index 000000000000..538c34579052 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testGH6258/testGH6258_02.php.testGH6258_02.hints @@ -0,0 +1,4 @@ +$test->generatedMet^hod(); +------------------------ +HINT:Introduce Hint +FIX:Create Method " generatedMethod()" in Class "TestClass" (TestClass.php) diff --git a/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testGH6258/testGH6258_02.php.testGH6258_02Fix.fixed b/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testGH6258/testGH6258_02.php.testGH6258_02Fix.fixed new file mode 100644 index 000000000000..41837d486cd8 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testGH6258/testGH6258_02.php.testGH6258_02Fix.fixed @@ -0,0 +1,21 @@ +generatedMethod(); diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestionTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestionTest.java index 8d436c42ca6b..37e822e92ceb 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestionTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestionTest.java @@ -307,14 +307,42 @@ public void testEnumMethods_Fix06() throws Exception { applyHint("BackedEnumString::Case2::introdu^ceStaticMethod();", "Create Method"); } + public void testGH6258_01() throws Exception { + checkHints("testGH6258_01.php", "TestClass::generatedMeth^od();"); + } + + public void testGH6258_01Fix() throws Exception { + // no changes because the method is created into the TestClass + // check that NPE doesn't occur + applyHint("testGH6258_01.php", "TestClass::generatedMeth^od();", "Create Method"); + } + + public void testGH6258_02() throws Exception { + checkHints("testGH6258_02.php", "$test->generatedMet^hod();"); + } + + public void testGH6258_02Fix() throws Exception { + // no changes because the method is created into the TestClass + // check that NPE doesn't occur + applyHint("testGH6258_02.php", "$test->generatedMetho^d();", "Create Method"); + } + private void checkHints(String caretLine) throws Exception { checkHints(new IntroduceSuggestion(), getTestFileName(), caretLine); } + private void checkHints(String fileName, String caretLine) throws Exception { + checkHints(new IntroduceSuggestion(), fileName, caretLine); + } + private void applyHint(String caretLine, String fixDesc) throws Exception { applyHint(new IntroduceSuggestion(), getTestFileName(), caretLine, fixDesc); } + private void applyHint(String fileName, String caretLine, String fixDesc) throws Exception { + applyHint(new IntroduceSuggestion(), fileName, caretLine, fixDesc); + } + private void fixContent(File file) throws Exception { Path path = file.toPath(); Charset charset = StandardCharsets.UTF_8;