diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/actions/ImportDataCreator.java b/php/php.editor/src/org/netbeans/modules/php/editor/actions/ImportDataCreator.java index f8f79da26cab..1d4b5459c484 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/actions/ImportDataCreator.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/actions/ImportDataCreator.java @@ -182,7 +182,28 @@ private Collection filterPlatformConstsAndFunctions(final private Collection filterExactUnqualifiedName(final Collection possibleFQElements, final String typeName) { Collection result = new HashSet<>(); for (FullyQualifiedElement fqElement : possibleFQElements) { - if (fqElement.getFullyQualifiedName().toString().endsWith(typeName)) { + // type name: e.g. Foo, Name\Space\Foo, \Name\Space\Foo + // GH-7546 if an element name has the same prefix and suffix(e.g. Foo2Foo), + // the following check is incorrect + // `fqElement.getFullyQualifiedName().toString().endsWith(typeName)` + // so, compare the segments, instead + QualifiedName qualifiedTypeName = QualifiedName.create(typeName); + List segments = qualifiedTypeName.getSegments(); + Collections.reverse(segments); + List elementSegments = fqElement.getFullyQualifiedName().getSegments(); + Collections.reverse(elementSegments); + boolean exactUnqualifiedName = true; + if (!segments.isEmpty() && segments.size() <= elementSegments.size()) { + for (int i = 0; i < segments.size(); i++) { + if (!segments.get(i).equals(elementSegments.get(i))) { + exactUnqualifiedName = false; + break; + } + } + } else { + exactUnqualifiedName = false; + } + if (exactUnqualifiedName) { result.add(fqElement); } } diff --git a/php/php.editor/test/unit/data/testfiles/actions/testGH7546/testGH7546.php b/php/php.editor/test/unit/data/testfiles/actions/testGH7546/testGH7546.php new file mode 100644 index 000000000000..6982ca0ce2ff --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/actions/testGH7546/testGH7546.php @@ -0,0 +1,61 @@ +<cannot be resolved> + \NS1\TestClass + \NS1\TestEnum + \NS1\TestInterface + \NS1\TestTrait + \NS1\Sub1\Sub2\Sub3\TestInterface + \NS1\Sub1\Sub2\Sub3\func + +Names: + CONSTANT + NS1\Sub1\TestClass + TestClass + TestEnum + TestInterface + TestTrait + \NS1\Sub1\Sub2\Sub3\TestInterface + func + +Variants: + \NS1\CONSTANT + \NS1\Sub1\Sub2\Sub3\CONSTANT + Don't import. + + <cannot be resolved> + + \NS1\Sub1\Sub2\Sub3\TestClass + \NS1\TestClass + Don't import. + + \NS1\Sub1\Sub2\Sub3\TestEnum + \NS1\TestEnum + Don't import. + + \NS1\Sub1\Sub2\Sub3\TestInterface + \NS1\TestInterface + Don't import. + + \NS1\Sub1\Sub2\Sub3\TestTrait + \NS1\TestTrait + Don't import. + + \NS1\Sub1\Sub2\Sub3\TestInterface + Don't import. + + \NS1\func + \NS1\Sub1\Sub2\Sub3\func + Don't import. + diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/actions/ImportDataCreatorTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/actions/ImportDataCreatorTest.java index 9f487675b37d..833d7bc082d8 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/actions/ImportDataCreatorTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/actions/ImportDataCreatorTest.java @@ -134,6 +134,10 @@ public void testGH6247_01() throws Exception { performTest("public function test(): st^atic {"); } + public void testGH7546_01() throws Exception { + performTest("class GH7546 ^{"); + } + private void performTest(String caretLine) throws Exception { performTest(caretLine, null); }