This is an automated email from the ASF dual-hosted git repository.
neilcsmith pushed a commit to branch delivery
in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/delivery by this push:
new a067bcb581 Avoid getting all types with an empty type name #6039
new f052d630a9 Merge pull request #6237 from
junichi11/php-gh-6039-fix-regression
a067bcb581 is described below
commit a067bcb581f7ba619e7f70e6f4eddde419ad69d9
Author: Junichi Yamamoto <[email protected]>
AuthorDate: Thu Jul 20 19:42:53 2023 +0900
Avoid getting all types with an empty type name #6039
- https://github.com/apache/netbeans/issues/6039
- Remove `<>` and `{}` from type name
- Ignore an empty type name to avoid getting all types
- Add unit tests
---
.../php/editor/actions/ImportDataCreator.java | 5 ++
.../php/editor/parser/PHPDocCommentParser.java | 31 ++++++++---
.../ReturnTypeArrayShapes01.pass | 9 +++
.../ReturnTypeArrayShapes02.pass | 9 +++
.../ReturnTypeGenerics01.pass | 9 +++
.../ReturnTypeGenerics02.pass | 9 +++
.../ReturnTypeGenerics03.pass | 9 +++
.../ReturnTypeGenerics04.pass | 9 +++
.../ReturnTypeGenerics05.pass | 9 +++
.../ReturnTypeObjectShapes01.pass | 9 +++
.../ReturnTypeObjectShapes02.pass | 9 +++
.../testfiles/actions/testGH6039/testGH6039_01.php | 64 ++++++++++++++++++++++
.../testGH6039/testGH6039_01.php.importData | 7 +++
.../php/editor/actions/ImportDataCreatorTest.java | 4 ++
.../php/editor/parser/PHPDocCommentParserTest.java | 45 +++++++++++++++
15 files changed, 230 insertions(+), 7 deletions(-)
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 60e1b6a5b1..6d9b1eb002 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.List;
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 class ImportDataCreator {
}
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<FullyQualifiedElement> 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 160df03925..64c9c3417e 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 @@ public class PHPDocCommentParser {
List<PHPDocTypeNode> 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 @@ public class PHPDocCommentParser {
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/ReturnTypeArrayShapes01.pass
b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeArrayShapes01.pass
new file mode 100644
index 0000000000..d1c3bd88ae
--- /dev/null
+++
b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeArrayShapes01.pass
@@ -0,0 +1,9 @@
+<PHPDocBlock start='3' end='39'>
+ <Tags>
+ <PHPDocTypeTag start='3' end='42' kind='return'>
+ <Types>
+ <PHPDocTypeNode start='14' end='19' value='array'
isArray='false'/>
+ </Types>
+ </PHPDocTypeTag>
+ </Tags>
+</PHPDocBlock>
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 0000000000..fb2224833d
--- /dev/null
+++
b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeArrayShapes02.pass
@@ -0,0 +1,9 @@
+<PHPDocBlock start='3' end='54'>
+ <Tags>
+ <PHPDocTypeTag start='3' end='57' kind='return'>
+ <Types>
+ <PHPDocTypeNode start='14' end='19' value='array'
isArray='false'/>
+ </Types>
+ </PHPDocTypeTag>
+ </Tags>
+</PHPDocBlock>
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 0000000000..ee8a4e1caa
--- /dev/null
+++
b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics01.pass
@@ -0,0 +1,9 @@
+<PHPDocBlock start='3' end='33'>
+ <Tags>
+ <PHPDocTypeTag start='3' end='36' kind='return'>
+ <Types>
+ <PHPDocTypeNode start='14' end='19' value='array'
isArray='false'/>
+ </Types>
+ </PHPDocTypeTag>
+ </Tags>
+</PHPDocBlock>
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 0000000000..d1c3bd88ae
--- /dev/null
+++
b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics02.pass
@@ -0,0 +1,9 @@
+<PHPDocBlock start='3' end='39'>
+ <Tags>
+ <PHPDocTypeTag start='3' end='42' kind='return'>
+ <Types>
+ <PHPDocTypeNode start='14' end='19' value='array'
isArray='false'/>
+ </Types>
+ </PHPDocTypeTag>
+ </Tags>
+</PHPDocBlock>
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 0000000000..398c9ccbe8
--- /dev/null
+++
b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics03.pass
@@ -0,0 +1,9 @@
+<PHPDocBlock start='3' end='32'>
+ <Tags>
+ <PHPDocTypeTag start='3' end='35' kind='return'>
+ <Types>
+ <PHPDocTypeNode start='14' end='18' value='list'
isArray='false'/>
+ </Types>
+ </PHPDocTypeTag>
+ </Tags>
+</PHPDocBlock>
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 0000000000..a474a71ce3
--- /dev/null
+++
b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics04.pass
@@ -0,0 +1,9 @@
+<PHPDocBlock start='3' end='32'>
+ <Tags>
+ <PHPDocTypeTag start='3' end='35' kind='return'>
+ <Types>
+ <PHPDocTypeNode start='14' end='18' value='Test'
isArray='false'/>
+ </Types>
+ </PHPDocTypeTag>
+ </Tags>
+</PHPDocBlock>
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 0000000000..a474a71ce3
--- /dev/null
+++
b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeGenerics05.pass
@@ -0,0 +1,9 @@
+<PHPDocBlock start='3' end='32'>
+ <Tags>
+ <PHPDocTypeTag start='3' end='35' kind='return'>
+ <Types>
+ <PHPDocTypeNode start='14' end='18' value='Test'
isArray='false'/>
+ </Types>
+ </PHPDocTypeTag>
+ </Tags>
+</PHPDocBlock>
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 0000000000..58dd1a0fce
--- /dev/null
+++
b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeObjectShapes01.pass
@@ -0,0 +1,9 @@
+<PHPDocBlock start='3' end='40'>
+ <Tags>
+ <PHPDocTypeTag start='3' end='43' kind='return'>
+ <Types>
+ <PHPDocTypeNode start='14' end='20' value='object'
isArray='false'/>
+ </Types>
+ </PHPDocTypeTag>
+ </Tags>
+</PHPDocBlock>
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 0000000000..2c62ef79c1
--- /dev/null
+++
b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/parser/PHPDocCommentParserTest/ReturnTypeObjectShapes02.pass
@@ -0,0 +1,9 @@
+<PHPDocBlock start='3' end='55'>
+ <Tags>
+ <PHPDocTypeTag start='3' end='58' kind='return'>
+ <Types>
+ <PHPDocTypeNode start='14' end='20' value='object'
isArray='false'/>
+ </Types>
+ </PHPDocTypeTag>
+ </Tags>
+</PHPDocBlock>
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 0000000000..dae38009b5
--- /dev/null
+++
b/php/php.editor/test/unit/data/testfiles/actions/testGH6039/testGH6039_01.php
@@ -0,0 +1,64 @@
+<?php
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+namespace NS\GH6039;
+
+class TestClass1{};
+class TestClass2{};
+class TestClass3{};
+class TestClass4{};
+
+namespace Test;
+
+class Example {
+ /**
+ * @return array<int> Description
+ */
+ public function gh6039_01(): array {
+ return [];
+ }
+
+ /**
+ * @return array<int, string> Description
+ */
+ public function gh6039_02(): array {
+ return [];
+ }
+
+ /**
+ * @return Example<int> 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 0000000000..31f1e16dab
--- /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 c65cca98dc..f1f201a68f 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 class ImportDataCreatorTest extends PHPTestBase {
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 7124691013..84bdab81b7 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 class PHPDocCommentParserTest extends PHPTestBase {
perform(comment, "MethodStatic03");
}
+ public void testReturnTypeGenerics01() throws Exception {
+ String comment = " * @return array<int> description";
+ perform(comment, "ReturnTypeGenerics01");
+ }
+
+ public void testReturnTypeGenerics02() throws Exception {
+ String comment = " * @return array<int, Test> description";
+ perform(comment, "ReturnTypeGenerics02");
+ }
+
+ public void testReturnTypeGenerics03() throws Exception {
+ String comment = " * @return list<int> description";
+ perform(comment, "ReturnTypeGenerics03");
+ }
+
+ public void testReturnTypeGenerics04() throws Exception {
+ String comment = " * @return Test<int> description";
+ perform(comment, "ReturnTypeGenerics04");
+ }
+
+ public void testReturnTypeGenerics05() throws Exception {
+ String comment = " * @return Test<max> 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 testReturnTypeObjectShapes01() throws Exception {
+ String comment = " * @return object{'foo':int} description";
+ perform(comment, "ReturnTypeObjectShapes01");
+ }
+
+ public void testReturnTypeObjectShapes02() throws Exception {
+ String comment = " * @return object{'foo':int, \"bar\": string}
description";
+ perform(comment, "ReturnTypeObjectShapes02");
+ }
+
public void perform(String comment, String filename) throws Exception {
PHPDocCommentParser parser = new PHPDocCommentParser();
PHPDocBlock block = parser.parse(0, comment.length(), comment);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists