Skip to content

Commit

Permalink
Merge pull request #6697 from troizet/php_function_guessing_array_ret…
Browse files Browse the repository at this point in the history
…urn_type

PHP: Implemented correct return type guessing for a function that returns an array
  • Loading branch information
junichi11 authored Nov 18, 2023
2 parents 269de64 + bfc5ccd commit 81162ff
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public void visit(ReturnStatement node) {
}
}
}
} else if (expression instanceof Scalar) {
} else if (expression instanceof Scalar || expression instanceof ArrayCreation) {
typeName = VariousUtils.extractVariableTypeFromExpression(expression, null);
}
if (!StringUtils.isEmpty(typeName)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public function myFoo(): array{
return parent::myFoo();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public function myFoo(){
return parent::myFoo();
}

Original file line number Diff line number Diff line change
@@ -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();

Original file line number Diff line number Diff line change
@@ -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>

Original file line number Diff line number Diff line change
@@ -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>

Original file line number Diff line number Diff line change
@@ -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];
}

Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void testInstanceOverrideMethodWithNullableType_02() throws Exception {
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(
Expand All @@ -220,7 +220,19 @@ public void testInstanceOverrideMethodWithGuessingBoolType_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 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ public void testDNFTypesPhpdocStaticField() throws Exception {
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:");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,8 @@ public void testDNFFieldTypes() throws Exception {
performTest("structure/php82/dnfFieldTypes");
}

public void testFunctionGuessingArrayReturnType() throws Exception {
performTest("structure/functionGuessingArrayReturnType");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,48 @@ public void testFunctionGuessingNullReturnType() throws Exception {
"?>\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('^');
Expand Down

0 comments on commit 81162ff

Please sign in to comment.