This is an automated email from the ASF dual-hosted git repository. junichi11 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new 60461afab6 Prevent getting an incorrect type if a return type is `static` with PHPDoc tag(`@return`) #6247 new fbd6ddd2fb Merge pull request #6251 from junichi11/php-gh-6247-wrong-suggestion-for-fix-imports 60461afab6 is described below commit 60461afab6dd315ca55f4d1d63eb2bc526b904a5 Author: Junichi Yamamoto <junich...@apache.org> AuthorDate: Tue Jul 25 10:49:24 2023 +0900 Prevent getting an incorrect type if a return type is `static` with PHPDoc tag(`@return`) #6247 - https://github.com/apache/netbeans/issues/6247 - Ignore `static` only when a PHPDoc tag is `@method` - Add a unit test --- .../php/editor/parser/PHPDocCommentParser.java | 23 ++++++++++----- .../testfiles/actions/testGH6247/testGH6247_01.php | 34 ++++++++++++++++++++++ .../testGH6247/testGH6247_01.php.importData | 12 ++++++++ .../php/editor/actions/ImportDataCreatorTest.java | 4 +++ 4 files changed, 65 insertions(+), 8 deletions(-) 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 64c9c3417e..6ba3e14cbf 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 @@ -202,8 +202,7 @@ public class PHPDocCommentParser { private PHPDocTag createTag(int start, int end, AnnotationParsedLine type, String description, String originalComment, int originalCommentStart) { final Map<OffsetRange, String> types = type.getTypes(); if (types.isEmpty()) { - boolean isReturnTag = type.equals(PHPDocTag.Type.RETURN); - List<PHPDocTypeNode> docTypes = findTypes(description, start, originalComment, originalCommentStart, isReturnTag); + List<PHPDocTypeNode> docTypes = findTypes(description, start, originalComment, originalCommentStart, type); if (PHP_DOC_VAR_TYPE_TAGS.contains(type)) { String variable = getVaribleName(description); PHPDocNode varibaleNode = null; @@ -252,16 +251,16 @@ public class PHPDocCommentParser { } private List<PHPDocTypeNode> findTypes(String description, int startDescription, String originalComment, int originalCommentStart) { - return findTypes(description, startDescription, originalComment, originalCommentStart, false); + return findTypes(description, startDescription, originalComment, originalCommentStart, PHPDocTypeTag.Type.PARAM); } - private List<PHPDocTypeNode> findTypes(String description, int startDescription, String originalComment, int originalCommentStart, boolean isReturnTag) { + private List<PHPDocTypeNode> findTypes(String description, int startDescription, String originalComment, int originalCommentStart, AnnotationParsedLine tagType) { if (StringUtils.isEmpty(description)) { return Collections.emptyList(); } List<PHPDocTypeNode> result = new ArrayList<>(); - for (String stype : getTypes(description, isReturnTag)) { + for (String stype : getTypes(description, tagType)) { stype = removeHTMLTags(stype); stype = sanitizeShapes(stype); int startDocNode = findStartOfDocNode(originalComment, originalCommentStart, stype, startDescription); @@ -288,13 +287,13 @@ public class PHPDocCommentParser { return result; } - private List<String> getTypes(String description, boolean isReturnTag) { + private List<String> getTypes(String description, AnnotationParsedLine tagType) { String[] tokens = description.trim().split("[ ]+"); //NOI18N - if (tokens.length > 0 && tokens[0].equals("static")) { // NOI18N + if (isMethodTag(tagType) && tokens.length > 0 && tokens[0].equals(Type.STATIC)) { tokens = Arrays.copyOfRange(tokens, 1, tokens.length); } ArrayList<String> types = new ArrayList<>(); - if (tokens.length > 0 && (isReturnTag || !tokens[0].startsWith("$"))) { //NOI18N + if (tokens.length > 0 && (isReturnTag(tagType) || !tokens[0].startsWith("$"))) { //NOI18N if (tokens[0].indexOf('|') > -1 || tokens[0].indexOf('&') > -1) { String[] ttokens = tokens[0].split("[|&]"); //NOI18N for (String ttoken : ttokens) { @@ -473,6 +472,14 @@ public class PHPDocCommentParser { return result; } + private static boolean isReturnTag(AnnotationParsedLine type) { + return PHPDocTypeTag.Type.RETURN == type; + } + + private static boolean isMethodTag(AnnotationParsedLine type) { + return PHPDocTypeTag.Type.METHOD == type; + } + private static final class ParametersExtractorImpl implements ParametersExtractor { private int position = 0; diff --git a/php/php.editor/test/unit/data/testfiles/actions/testGH6247/testGH6247_01.php b/php/php.editor/test/unit/data/testfiles/actions/testGH6247/testGH6247_01.php new file mode 100644 index 0000000000..c45dc0752f --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/actions/testGH6247/testGH6247_01.php @@ -0,0 +1,34 @@ +<?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 NS1; + +class GH6247 {} + +namespace Test; + +$test = new class extends GH6247 { + /** + * @return static Description + */ + public function test(): static { + return $this; + } +}; diff --git a/php/php.editor/test/unit/data/testfiles/actions/testGH6247/testGH6247_01.php.importData b/php/php.editor/test/unit/data/testfiles/actions/testGH6247/testGH6247_01.php.importData new file mode 100644 index 0000000000..c0c4acfe20 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/actions/testGH6247/testGH6247_01.php.importData @@ -0,0 +1,12 @@ +Caret position: 980 +Should show uses panel: true +Defaults: + \NS1\GH6247 + +Names: + GH6247 + +Variants: + \NS1\GH6247 + 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 f1f201a68f..9f487675b3 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 @@ -130,6 +130,10 @@ public class ImportDataCreatorTest extends PHPTestBase { performTest(" public function gh6039_01(): ^array {"); } + public void testGH6247_01() throws Exception { + performTest("public function test(): st^atic {"); + } + private void performTest(String caretLine) throws Exception { performTest(caretLine, null); } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists