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 60e1b6a5b1a0..6d9b1eb00278 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 @@ -29,6 +29,7 @@ import java.util.Map; import java.util.TreeSet; import org.netbeans.modules.php.api.PhpVersion; +import org.netbeans.modules.php.api.util.StringUtils; import org.netbeans.modules.php.editor.CodeUtils; import org.netbeans.modules.php.editor.actions.FixUsesAction.Options; import org.netbeans.modules.php.editor.actions.ImportData.DataItem; @@ -87,6 +88,10 @@ public ImportData create() { } private void processFQElementName(final String fqElementName) { + // GH-6039: avoid getting all types + if (!StringUtils.hasText(fqElementName)) { + return; + } // GH-6075 String fqeName = CodeUtils.removeNullableTypePrefix(fqElementName); Collection possibleFQElements = fetchPossibleFQElements(fqeName); diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/parser/PHPDocCommentParser.java b/php/php.editor/src/org/netbeans/modules/php/editor/parser/PHPDocCommentParser.java index 160df039254f..64c9c3417ed0 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/parser/PHPDocCommentParser.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/parser/PHPDocCommentParser.java @@ -263,6 +263,7 @@ private List findTypes(String description, int startDescription, List result = new ArrayList<>(); for (String stype : getTypes(description, isReturnTag)) { stype = removeHTMLTags(stype); + stype = sanitizeShapes(stype); int startDocNode = findStartOfDocNode(originalComment, originalCommentStart, stype, startDescription); if (startDocNode == -1) { continue; @@ -377,17 +378,33 @@ private List findMethodParams(String description, int startOfD private String removeHTMLTags(String text) { String value = text; - int index = value.indexOf('>'); - if (index > -1) { - value = value.substring(index + 1); - index = value.indexOf('<'); - if (index > -1) { - value = value.substring(0, index); - } + int startTagIndex = value.indexOf('<'); + if (startTagIndex > -1) { + value = value.substring(0, startTagIndex).trim(); } return value; } + /** + * Remove `{'key': type}`. + * + * e.g. {@code array{'foo': int}}, {@code object{'foo': int, "bar": string}} + * + * @see https://phpstan.org/writing-php-code/phpdoc-types#array-shapes + * @see https://phpstan.org/writing-php-code/phpdoc-types#object-shapes + * + * @param type the type + * @return the sanitized type + */ + private String sanitizeShapes(String type) { + String sanitizedType = type; + int startIndex = sanitizedType.indexOf("{"); // NOI18N + if (startIndex > -1) { + sanitizedType = sanitizedType.substring(0, startIndex).trim(); + } + return sanitizedType; + } + /** * Find the start position of the specified string in the comment. * diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ObjectShapes01.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ObjectShapes01.pass new file mode 100644 index 000000000000..58dd1a0fcec3 --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ObjectShapes01.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ObjectShapes02.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ObjectShapes02.pass new file mode 100644 index 000000000000..2c62ef79c178 --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ObjectShapes02.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeArrayShapes01.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeArrayShapes01.pass new file mode 100644 index 000000000000..d1c3bd88aece --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeArrayShapes01.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeArrayShapes02.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeArrayShapes02.pass new file mode 100644 index 000000000000..fb2224833d1c --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeArrayShapes02.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics01.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics01.pass new file mode 100644 index 000000000000..ee8a4e1caab9 --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics01.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics02.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics02.pass new file mode 100644 index 000000000000..d1c3bd88aece --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics02.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics03.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics03.pass new file mode 100644 index 000000000000..398c9ccbe8a4 --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics03.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics04.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics04.pass new file mode 100644 index 000000000000..a474a71ce39f --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics04.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics05.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics05.pass new file mode 100644 index 000000000000..a474a71ce39f --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics05.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeObjectShapes01.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeObjectShapes01.pass new file mode 100644 index 000000000000..58dd1a0fcec3 --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeObjectShapes01.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeObjectShapes02.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeObjectShapes02.pass new file mode 100644 index 000000000000..2c62ef79c178 --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeObjectShapes02.pass @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/php/php.editor/test/unit/data/testfiles/actions/testGH6039/testGH6039_01.php b/php/php.editor/test/unit/data/testfiles/actions/testGH6039/testGH6039_01.php new file mode 100644 index 000000000000..dae38009b57c --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/actions/testGH6039/testGH6039_01.php @@ -0,0 +1,64 @@ + Description + */ + public function gh6039_01(): array { + return []; + } + + /** + * @return array Description + */ + public function gh6039_02(): array { + return []; + } + + /** + * @return Example Description + */ + public function gh6039_03(): Example { + return $this; + } + + /** + * @return int<0, 100> Description + */ + public function gh6039_04(): Example { + return 1; + } + + /** + * @return array{int, int} Description + */ + public function gh6039_05(): array { + return []; + } +} diff --git a/php/php.editor/test/unit/data/testfiles/actions/testGH6039/testGH6039_01.php.importData b/php/php.editor/test/unit/data/testfiles/actions/testGH6039/testGH6039_01.php.importData new file mode 100644 index 000000000000..31f1e16dab60 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/actions/testGH6039/testGH6039_01.php.importData @@ -0,0 +1,7 @@ +Caret position: 1037 +Should show uses panel: false +Defaults: + +Names: + +Variants: 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 c65cca98dc0b..f1f201a68f91 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 @@ -126,6 +126,10 @@ public void testGH6075_01() throws Exception { performTest("function test(): void ^{"); } + public void testGH6039_01() throws Exception { + performTest(" public function gh6039_01(): ^array {"); + } + private void performTest(String caretLine) throws Exception { performTest(caretLine, null); } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest.java index 712469101343..c78e64ecb7f0 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest.java @@ -385,6 +385,51 @@ public void testMethodStatic03() throws Exception { perform(comment, "MethodStatic03"); } + public void testReturnTypeGenerics01() throws Exception { + String comment = " * @return array description"; + perform(comment, "ReturnTypeGenerics01"); + } + + public void testReturnTypeGenerics02() throws Exception { + String comment = " * @return array description"; + perform(comment, "ReturnTypeGenerics02"); + } + + public void testReturnTypeGenerics03() throws Exception { + String comment = " * @return list description"; + perform(comment, "ReturnTypeGenerics03"); + } + + public void testReturnTypeGenerics04() throws Exception { + String comment = " * @return Test description"; + perform(comment, "ReturnTypeGenerics04"); + } + + public void testReturnTypeGenerics05() throws Exception { + String comment = " * @return Test description"; + perform(comment, "ReturnTypeGenerics05"); + } + + public void testReturnTypeArrayShapes01() throws Exception { + String comment = " * @return array{'foo':int} description"; + perform(comment, "ReturnTypeArrayShapes01"); + } + + public void testReturnTypeArrayShapes02() throws Exception { + String comment = " * @return array{'foo':int, \"bar\": string} description"; + perform(comment, "ReturnTypeArrayShapes02"); + } + + public void testObjectShapes01() throws Exception { + String comment = " * @return object{'foo':int} description"; + perform(comment, "ObjectShapes01"); + } + + public void testObjectShapes02() throws Exception { + String comment = " * @return object{'foo':int, \"bar\": string} description"; + perform(comment, "ObjectShapes02"); + } + public void perform(String comment, String filename) throws Exception { PHPDocCommentParser parser = new PHPDocCommentParser(); PHPDocBlock block = parser.parse(0, comment.length(), comment);