This is an automated email from the ASF dual-hosted git repository. junichi11 pushed a commit to branch php-nb21-features in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/php-nb21-features by this push: new bfc5ccd144 PHP: Implemented correct return type guessing for a function that returns an array new 81162ffa6a Merge pull request #6697 from troizet/php_function_guessing_array_return_type bfc5ccd144 is described below commit bfc5ccd1446d0b646b84d59142358706b69213d7 Author: Alexey Borokhvostov <troi...@gmail.com> AuthorDate: Tue Nov 14 23:15:02 2023 +0700 PHP: Implemented correct return type guessing for a function that returns an array --- .../php/editor/model/impl/ModelVisitor.java | 2 +- .../structure/functionGuessingArrayReturnType.pass | 2 ++ ...InstanceOverrideMethodWithGuessingArrayType.php | 29 +++++++++++++++ ...eOverrideMethodWithGuessingArrayType_01.codegen | 4 +++ ...eOverrideMethodWithGuessingArrayType_02.codegen | 4 +++ .../functionGuessingArrayReturnType.php | 37 +++++++++++++++++++ ...php.testFunctionGuessingArrayReturnType_01.html | 10 ++++++ ...php.testFunctionGuessingArrayReturnType_02.html | 10 ++++++ .../structure/functionGuessingArrayReturnType.php | 34 ++++++++++++++++++ .../SelectedPropertyMethodsCreatorTest.java | 16 +++++++-- .../editor/completion/PHPCCDocumentationTest.java | 8 +++++ .../modules/php/editor/csl/NavigatorTest.java | 4 +++ .../typinghooks/PhpCommentGeneratorTest.java | 42 ++++++++++++++++++++++ 13 files changed, 199 insertions(+), 3 deletions(-) diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/ModelVisitor.java b/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/ModelVisitor.java index 76cde49e4f..e3350332d7 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/ModelVisitor.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/ModelVisitor.java @@ -311,7 +311,7 @@ public final class ModelVisitor extends DefaultTreePathVisitor { } } } - } else if (expression instanceof Scalar) { + } else if (expression instanceof Scalar || expression instanceof ArrayCreation) { typeName = VariousUtils.extractVariableTypeFromExpression(expression, null); } if (!StringUtils.isEmpty(typeName)) { diff --git a/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/csl/NavigatorTest/structure/functionGuessingArrayReturnType.pass b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/csl/NavigatorTest/structure/functionGuessingArrayReturnType.pass new file mode 100644 index 0000000000..f7b60379c0 --- /dev/null +++ b/php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/csl/NavigatorTest/structure/functionGuessingArrayReturnType.pass @@ -0,0 +1,2 @@ +|-testArrayReturnType [824, 868] : ESCAPED{testArrayReturnType}ESCAPED{(}ESCAPED{)}<font color="#999999">:ESCAPED{array}</font> +|-testArrayReturnTypeWithUnionType [880, 984] : ESCAPED{testArrayReturnTypeWithUnionType}ESCAPED{(}ESCAPED{)}<font color="#999999">:ESCAPED{string}ESCAPED{|}ESCAPED{array}</font> diff --git a/php/php.editor/test/unit/data/testfiles/codegen/testInstanceOverrideMethodWithGuessingArrayType/testInstanceOverrideMethodWithGuessingArrayType.php b/php/php.editor/test/unit/data/testfiles/codegen/testInstanceOverrideMethodWithGuessingArrayType/testInstanceOverrideMethodWithGuessingArrayType.php new file mode 100644 index 0000000000..30f7413244 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/codegen/testInstanceOverrideMethodWithGuessingArrayType/testInstanceOverrideMethodWithGuessingArrayType.php @@ -0,0 +1,29 @@ +<?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. + */ + +class Foo { + public function myFoo(){ + return [1, 2]; + } +} + +class Bar extends Foo { + +} diff --git a/php/php.editor/test/unit/data/testfiles/codegen/testInstanceOverrideMethodWithGuessingArrayType/testInstanceOverrideMethodWithGuessingArrayType.php.testInstanceOverrideMethodWithGuessingArrayType_01.codegen b/php/php.editor/test/unit/data/testfiles/codegen/testInstanceOverrideMethodWithGuessingArrayType/testInstanceOverrideMethodWithGuessingArrayType.php.testInstanceOverrideMethodWithGuessingArrayType_01.codegen new file mode 100644 index 0000000000..0a1842adff --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/codegen/testInstanceOverrideMethodWithGuessingArrayType/testInstanceOverrideMethodWithGuessingArrayType.php.testInstanceOverrideMethodWithGuessingArrayType_01.codegen @@ -0,0 +1,4 @@ +public function myFoo(): array{ +return parent::myFoo(); +} + diff --git a/php/php.editor/test/unit/data/testfiles/codegen/testInstanceOverrideMethodWithGuessingArrayType/testInstanceOverrideMethodWithGuessingArrayType.php.testInstanceOverrideMethodWithGuessingArrayType_02.codegen b/php/php.editor/test/unit/data/testfiles/codegen/testInstanceOverrideMethodWithGuessingArrayType/testInstanceOverrideMethodWithGuessingArrayType.php.testInstanceOverrideMethodWithGuessingArrayType_02.codegen new file mode 100644 index 0000000000..b976a00433 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/codegen/testInstanceOverrideMethodWithGuessingArrayType/testInstanceOverrideMethodWithGuessingArrayType.php.testInstanceOverrideMethodWithGuessingArrayType_02.codegen @@ -0,0 +1,4 @@ +public function myFoo(){ +return parent::myFoo(); +} + diff --git a/php/php.editor/test/unit/data/testfiles/completion/documentation/functionGuessingArrayReturnType.php b/php/php.editor/test/unit/data/testfiles/completion/documentation/functionGuessingArrayReturnType.php new file mode 100644 index 0000000000..8c6e20ecc5 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/completion/documentation/functionGuessingArrayReturnType.php @@ -0,0 +1,37 @@ +<?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. + */ + +function testArrayReturnType() +{ + return [1, 2]; +} + + +function testArrayReturnTypeWithUnionType() +{ + if (true) { + return 'string'; + } + return [1, 2]; +} + +testArrayReturnType(); +testArrayReturnTypeWithUnionType(); + diff --git a/php/php.editor/test/unit/data/testfiles/completion/documentation/functionGuessingArrayReturnType.php.testFunctionGuessingArrayReturnType_01.html b/php/php.editor/test/unit/data/testfiles/completion/documentation/functionGuessingArrayReturnType.php.testFunctionGuessingArrayReturnType_01.html new file mode 100644 index 0000000000..b80baff54d --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/completion/documentation/functionGuessingArrayReturnType.php.testFunctionGuessingArrayReturnType_01.html @@ -0,0 +1,10 @@ +<html><body> +<pre>Code completion result for source line: +testArrayReturn|Type(); +(QueryType=COMPLETION, prefixSearch=false, caseSensitive=true) +METHOD testArrayReturnType() [PUBLIC] functionGuessingArrayReturnType.php +</pre><h2>Documentation:</h2><div align="right"><font size=-1></font></div><b>testArrayReturnType</b><br/><br/><br /> +<h3>Returns:</h3> +<table> +<tr><th align="left">Type:</th><td>array</td></tr></table></body></html> + diff --git a/php/php.editor/test/unit/data/testfiles/completion/documentation/functionGuessingArrayReturnType.php.testFunctionGuessingArrayReturnType_02.html b/php/php.editor/test/unit/data/testfiles/completion/documentation/functionGuessingArrayReturnType.php.testFunctionGuessingArrayReturnType_02.html new file mode 100644 index 0000000000..c4ee3637e2 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/completion/documentation/functionGuessingArrayReturnType.php.testFunctionGuessingArrayReturnType_02.html @@ -0,0 +1,10 @@ +<html><body> +<pre>Code completion result for source line: +testArrayReturnTypeWithUnion|Type(); +(QueryType=COMPLETION, prefixSearch=false, caseSensitive=true) +METHOD testArrayReturnTypeWithUnionTy [PUBLIC] functionGuessingArrayReturnType.php +</pre><h2>Documentation:</h2><div align="right"><font size=-1></font></div><b>testArrayReturnTypeWithUnionType</b><br/><br/><br /> +<h3>Returns:</h3> +<table> +<tr><th align="left">Type:</th><td>string | array</td></tr></table></body></html> + diff --git a/php/php.editor/test/unit/data/testfiles/structure/functionGuessingArrayReturnType.php b/php/php.editor/test/unit/data/testfiles/structure/functionGuessingArrayReturnType.php new file mode 100644 index 0000000000..b24ea2555e --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/structure/functionGuessingArrayReturnType.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. + */ + +function testArrayReturnType() +{ + return [1, 2]; +} + + +function testArrayReturnTypeWithUnionType() +{ + if (true) { + return 'string'; + } + return [1, 2]; +} + diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/codegen/SelectedPropertyMethodsCreatorTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/codegen/SelectedPropertyMethodsCreatorTest.java index 7e2e153512..3a4e183c71 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/codegen/SelectedPropertyMethodsCreatorTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/codegen/SelectedPropertyMethodsCreatorTest.java @@ -209,7 +209,7 @@ public class SelectedPropertyMethodsCreatorTest extends PHPTestBase { checkResult(new SelectedPropertyMethodsCreator().create( selectProperties(cgsInfo.getPossibleMethods(), "myFoo"), new SinglePropertyMethodCreator.InheritedMethodCreator(cgsInfo))); } - + public void testInstanceOverrideMethodWithGuessingBoolType_01() throws Exception { CGSInfo cgsInfo = getCgsInfo("class Bar extends Foo {^", PhpVersion.PHP_70); checkResult(new SelectedPropertyMethodsCreator().create( @@ -220,7 +220,19 @@ public class SelectedPropertyMethodsCreatorTest extends PHPTestBase { CGSInfo cgsInfo = getCgsInfo("class Bar extends Foo {^", PhpVersion.PHP_56); checkResult(new SelectedPropertyMethodsCreator().create( selectProperties(cgsInfo.getPossibleMethods(), "myFoo"), new SinglePropertyMethodCreator.InheritedMethodCreator(cgsInfo))); - } + } + + public void testInstanceOverrideMethodWithGuessingArrayType_01() throws Exception { + CGSInfo cgsInfo = getCgsInfo("class Bar extends Foo {^", PhpVersion.PHP_70); + checkResult(new SelectedPropertyMethodsCreator().create( + selectProperties(cgsInfo.getPossibleMethods(), "myFoo"), new SinglePropertyMethodCreator.InheritedMethodCreator(cgsInfo))); + } + + public void testInstanceOverrideMethodWithGuessingArrayType_02() throws Exception { + CGSInfo cgsInfo = getCgsInfo("class Bar extends Foo {^", PhpVersion.PHP_56); + checkResult(new SelectedPropertyMethodsCreator().create( + selectProperties(cgsInfo.getPossibleMethods(), "myFoo"), new SinglePropertyMethodCreator.InheritedMethodCreator(cgsInfo))); + } public void testGetterWithType_01() throws Exception { CGSInfo cgsInfo = getCgsInfo("class Foo {^", PhpVersion.PHP_70); diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCCDocumentationTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCCDocumentationTest.java index 90d6a38718..cea5b1a5e0 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCCDocumentationTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCCDocumentationTest.java @@ -602,6 +602,14 @@ public class PHPCCDocumentationTest extends PHPCodeCompletionTestBase { checkCompletionDocumentation("testfiles/completion/documentation/php82/dnfTypes.php", "static::$phpdocStaticFiel^d;", false, ""); } + public void testFunctionGuessingArrayReturnType_01() throws Exception { + checkCompletionDocumentation("testfiles/completion/documentation/functionGuessingArrayReturnType.php", "testArrayReturn^Type();", false, ""); + } + + public void testFunctionGuessingArrayReturnType_02() throws Exception { + checkCompletionDocumentation("testfiles/completion/documentation/functionGuessingArrayReturnType.php", "testArrayReturnTypeWithUnion^Type();", false, ""); + } + @Override protected String alterDocumentationForTest(String documentation) { int start = documentation.indexOf("file:"); diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/NavigatorTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/NavigatorTest.java index b6970178a6..83b9ec08e0 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/NavigatorTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/NavigatorTest.java @@ -144,4 +144,8 @@ public class NavigatorTest extends PhpNavigatorTestBase { performTest("structure/php82/dnfFieldTypes"); } + public void testFunctionGuessingArrayReturnType() throws Exception { + performTest("structure/functionGuessingArrayReturnType"); + } + } diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/typinghooks/PhpCommentGeneratorTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/typinghooks/PhpCommentGeneratorTest.java index 18d0c0b860..54b316df9e 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/typinghooks/PhpCommentGeneratorTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/typinghooks/PhpCommentGeneratorTest.java @@ -870,6 +870,48 @@ public class PhpCommentGeneratorTest extends PHPNavTestBase { "?>\n"); } + public void testFunctionGuessingArrayReturnType() throws Exception { + insertBreak( "<?php\n" + + "/**^\n" + + "function foo() {\n" + + " return [1, 2];\n" + + "}\n" + + "?>\n", + "<?php\n" + + "/**\n" + + " * \n" + + " * @return array^\n" + + " */\n" + + "function foo() {\n" + + " return [1, 2];\n" + + "}\n" + + "?>\n"); + } + + public void testFunctionGuessingArrayReturnTypeWithUnionType() throws Exception { + insertBreak( "<?php\n" + + "/**^\n" + + "function foo() {\n" + + " if (true) {\n" + + " return 'str';\n" + + " }\n" + + " return [1, 2];\n" + + "}\n" + + "?>\n", + "<?php\n" + + "/**\n" + + " * \n" + + " * @return string|array^\n" + + " */\n" + + "function foo() {\n" + + " if (true) {\n" + + " return 'str';\n" + + " }\n" + + " return [1, 2];\n" + + "}\n" + + "?>\n"); + } + @Override public void insertNewline(String source, String reformatted, IndentPrefs preferences) throws Exception { int sourcePos = source.indexOf('^'); --------------------------------------------------------------------- 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