Skip to content

Commit

Permalink
[NETBEANS-1859] PHP autocompletion does only get fully qualified retu…
Browse files Browse the repository at this point in the history
…rned objects

- Get fully qualified types from return types of functions
  • Loading branch information
junichi11 committed Sep 12, 2019
1 parent 654b5f0 commit 5ddaa32
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.netbeans.modules.php.editor.model.nodes.GroupUseStatementPartInfo;
import org.netbeans.modules.php.editor.model.nodes.NamespaceDeclarationInfo;
import org.netbeans.modules.php.editor.model.nodes.SingleUseStatementPartInfo;
import org.netbeans.modules.php.editor.parser.astnodes.Expression;
import org.netbeans.modules.php.editor.parser.astnodes.FunctionDeclaration;
import org.netbeans.modules.php.editor.parser.astnodes.Program;
import org.netbeans.modules.php.editor.parser.astnodes.Scalar;
Expand Down Expand Up @@ -85,8 +86,14 @@ GroupUseScopeImpl createUseStatementPart(GroupUseStatementPartInfo useStatementP
}

FunctionScopeImpl createElement(Program program, FunctionDeclaration node) {
// NETBEANS-1859 add qualified return type
Expression returnType = node.getReturnType();
String qualifiedReturnType = VariousUtils.getReturnType(program, node);
if (returnType != null) {
qualifiedReturnType = VariousUtils.qualifyTypeNames(VariousUtils.getReturnType(program, node), returnType.getStartOffset(), this);
}
FunctionScopeImpl retval = new FunctionScopeImpl(this, FunctionDeclarationInfo.create(program, node),
VariousUtils.getReturnType(program, node), VariousUtils.isDeprecatedFromPHPDoc(program, node));
qualifiedReturnType, VariousUtils.isDeprecatedFromPHPDoc(program, node));
return retval;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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 TestNamespace;

class TestClass {

public $test = "test";
public static $staticTest = "staticTest";

public function test() {
}

public static function staticTest() {
}
}

function newTestClass(): TestClass {
return new TestClass();
}

namespace TestNamespace2;

class TestClass {

public $test2 = "test2";
public static $staticTest2 = "staticTest2";

public function test2() {
}

public static function staticTest2() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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.
*/

echo \TestNamespace\newTestClass()->
echo \TestNamespace\newTestClass()::
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Code completion result for source line:
echo \TestNamespace\newTestClass()->|
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
METHOD staticTest() [STATIC] \TestNamespace\TestClass
METHOD test() [PUBLIC] \TestNamespace\TestClass
VARIABLE ? test [PUBLIC] \TestNamespace\TestClass
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Code completion result for source line:
echo \TestNamespace\newTestClass()::|
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
------------------------------------
METHOD staticTest() [STATIC] \TestNamespace\TestClass
VARIABLE ? $staticTest [STATIC] \TestNamespace\TestClass
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Document 0
Searchable Keys:
base : parametertype;parameterType;16;$msg:?string:1::1:0:0,$num:int:1::1:0:0;;;0;<TESTURL>/testNullableTypesForFunctions.php;
base : returntype;returnType;68;$str:string:1::1:0:0;?Foo;;0;<TESTURL>/testNullableTypesForFunctions.php;
base : returntype;returnType;68;$str:string:1::1:0:0;?\Foo;;0;<TESTURL>/testNullableTypesForFunctions.php;
top : parametertype
top : returntype

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.
*/
package org.netbeans.modules.php.editor.completion;

import java.io.File;
import java.util.Collections;
import java.util.Map;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.modules.php.project.api.PhpSourcePath;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;


public class PHPCodeCompletionNb1859Test extends PHPCodeCompletionTestBase {

public PHPCodeCompletionNb1859Test(String testName) {
super(testName);
}

public void testNamespaceFunction_01() throws Exception {
checkCompletion("testfiles/completion/lib/nb1859/nb1859_02.php", "echo \\TestNamespace\\newTestClass()->^", false);
}

public void testNamespaceFunction_02() throws Exception {
checkCompletion("testfiles/completion/lib/nb1859/nb1859_02.php", "echo \\TestNamespace\\newTestClass()::^", false);
}

@Override
protected Map<String, ClassPath> createClassPathsForTest() {
return Collections.singletonMap(
PhpSourcePath.SOURCE_CP,
ClassPathSupport.createClassPath(new FileObject[] {
FileUtil.toFileObject(new File(getDataDir(), "/testfiles/completion/lib/nb1859"))
})
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public void testReturnTypes01() throws Exception {
assertNotNull(functionScope);
Collection<? extends String> returnTypeNames = functionScope.getReturnTypeNames();
assertEquals(1, returnTypeNames.size());
assertEquals("DateTime", ModelUtils.getFirst(returnTypeNames));
assertEquals("\\DateTime", ModelUtils.getFirst(returnTypeNames));
}

public void testReturnTypes02() throws Exception {
Expand Down Expand Up @@ -301,7 +301,7 @@ public void testReturnTypes04() throws Exception {
assertNotNull(foo);
Collection<? extends String> fooReturnTypeNames = foo.getReturnTypeNames();
assertEquals(1, fooReturnTypeNames.size());
assertEquals("Iterator", ModelUtils.getFirst(fooReturnTypeNames));
assertEquals("\\Iterator", ModelUtils.getFirst(fooReturnTypeNames));
FunctionScope bar = ModelUtils.getFirst(ModelUtils.filter(ModelUtils.getDeclaredFunctions(program), "bar"));
assertNotNull(bar);
Collection<? extends String> barReturnTypeNames = bar.getReturnTypeNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public void testIssue270235_01() throws Exception {
+ "/**\n"
+ " * \n"
+ " * @param string|null $string\n"
+ " * @return Foo|null^\n"
+ " * @return \\Foo|null^\n"
+ " */\n"
+ "function test(?string $string): ?Foo {\n"
+ " \n"
Expand Down

0 comments on commit 5ddaa32

Please sign in to comment.