Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Find Usages for __construct method #4382 #5766

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.netbeans.modules.csl.spi.support.CancelSupport;
import org.netbeans.modules.php.api.util.StringUtils;
import org.netbeans.modules.php.editor.CodeUtils;
import org.netbeans.modules.php.editor.api.AliasedName;
import org.netbeans.modules.php.editor.api.ElementQuery;
import org.netbeans.modules.php.editor.api.ElementQuery.Index;
import org.netbeans.modules.php.editor.api.NameKind;
Expand Down Expand Up @@ -801,7 +802,7 @@ private void build(FileScopeImpl fileScope) {
buildMethods(index, fileScope, cachedOccurences);
setElementInfo((TypeScope) scope.getInScope());
if (elementInfo.setDeclarations(index.getTypes(NameKind.exact(elementInfo.getQualifiedName())))) {
buildClassInstanceCreation(elementInfo, fileScope, cachedOccurences);
buildClassInstanceCreation(elementInfo, fileScope, cachedOccurences, true);
}
}
}
Expand Down Expand Up @@ -1907,6 +1908,10 @@ private void buildDocTagsForClasses(ElementInfo nodeCtxInfo, FileScopeImpl fileS
}

private void buildClassInstanceCreation(ElementInfo query, FileScopeImpl fileScope, final List<Occurence> occurences) {
buildClassInstanceCreation(query, fileScope, occurences, false);
}

private void buildClassInstanceCreation(ElementInfo query, FileScopeImpl fileScope, final List<Occurence> occurences, boolean forConstructMethod) {
Set<? extends PhpElement> elements = query.getDeclarations();
for (PhpElement phpElement : elements) {
for (Entry<ASTNodeInfo<ClassInstanceCreation>, Scope> entry : clasInstanceCreations.entrySet()) {
Expand All @@ -1915,7 +1920,24 @@ private void buildClassInstanceCreation(ElementInfo query, FileScopeImpl fileSco
}
ASTNodeInfo<ClassInstanceCreation> nodeInfo = entry.getKey();
final boolean isAliased = VariousUtils.isAliased(nodeInfo.getQualifiedName(), nodeInfo.getOriginalNode().getStartOffset(), entry.getValue());
if (!isAliased || nodeInfo.getQualifiedName().getSegments().size() > 1) {
// GH-4382: Find usages of the __construct method
// also add alias of class instance creation
// e.g.
// use Example as ExampleAlias;
// class Example {
// public function __construct(){}
// }
// $alias = new ExampleAlias();
// $original = new Example();
boolean isConstructorAlias = false;
if (forConstructMethod) {
AliasedName aliasedName = VariousUtils.getAliasedName(nodeInfo.getQualifiedName(), nodeInfo.getOriginalNode().getStartOffset(), query.getScope());
if (aliasedName != null) {
QualifiedName realName = aliasedName.getRealName();
isConstructorAlias = phpElement.getName().equals(realName.getName());
}
}
if (!isAliased || nodeInfo.getQualifiedName().getSegments().size() > 1 || isConstructorAlias) {
final QualifiedName qualifiedName = VariousUtils.getFullyQualifiedName(
nodeInfo.getQualifiedName(),
nodeInfo.getOriginalNode().getStartOffset(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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.
*/

use Example as ExampleAlias;

class Example {

public function __construct() {

}

}

$alias = new ExampleAlias();
$test1 = new \Test\ExampleAlias();
$test2 = new Test\ExampleAlias();
$example = new Example();
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Display text: public function <b>__construct</b>() {
File name: index.php
Name: __construct
Position: BEGIN: 883 END: 894

Display text: $alias = new <b>ExampleAlias</b>();
File name: index.php
Name: Example
Position: BEGIN: 931 END: 943

Display text: $example = new <b>Example</b>();
File name: index.php
Name: Example
Position: BEGIN: 1031 END: 1038
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Display text: use Example as <b>ExampleAlias</b>;
File name: index.php
Name: ExampleAlias
Position: BEGIN: 831 END: 843

Display text: $alias = new <b>ExampleAlias</b>();
File name: index.php
Name: ExampleAlias
Position: BEGIN: 931 END: 943
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Display text: use <b>Example</b> as ExampleAlias;
File name: index.php
Name: Example
Position: BEGIN: 820 END: 827

Display text: class <b>Example</b> {
File name: index.php
Name: Example
Position: BEGIN: 852 END: 859

Display text: $example = new <b>Example</b>();
File name: index.php
Name: Example
Position: BEGIN: 1031 END: 1038
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,16 @@ public void testEnums_Interface_01b() throws Exception {
findUsages("enum EnumB: string implements Inter^faceB {");
}

public void testGH4382_01() throws Exception {
findUsages(" public function __constr^uct() {");
}

public void testGH4382_02() throws Exception {
findUsages("$alias = new ExampleAli^as();");
}

public void testGH4382_03() throws Exception {
findUsages("$example = new Examp^le();");
}

}