This is an automated email from the ASF dual-hosted git repository.

tmysik 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 1abc169eae Fix Find Usages for __construct method #4382
     new fa050d4a0f Merge pull request #5766 from 
junichi11/php-gh-4382-find-usage-alias-of-constructor
1abc169eae is described below

commit 1abc169eae7c13940f9c9e472b3ba800fd4b038d
Author: Junichi Yamamoto <[email protected]>
AuthorDate: Mon Apr 3 13:28:37 2023 +0900

    Fix Find Usages for __construct method #4382
    
    - https://github.com/apache/netbeans/issues/4382
    - Also find the new instance creation for the alias
---
 .../php/editor/model/impl/OccurenceBuilder.java    | 26 ++++++++++++++--
 .../data/testfiles/findusages/testGH4382/index.php | 35 ++++++++++++++++++++++
 .../testGH4382/index.php.testGH4382_01.findUsages  | 14 +++++++++
 .../testGH4382/index.php.testGH4382_02.findUsages  |  9 ++++++
 .../testGH4382/index.php.testGH4382_03.findUsages  | 14 +++++++++
 .../php/findusages/WhereUsedSupportTest.java       | 12 ++++++++
 6 files changed, 108 insertions(+), 2 deletions(-)

diff --git 
a/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/OccurenceBuilder.java
 
b/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/OccurenceBuilder.java
index f971b45c14..67839defc6 100644
--- 
a/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/OccurenceBuilder.java
+++ 
b/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/OccurenceBuilder.java
@@ -34,6 +34,7 @@ import org.netbeans.modules.csl.api.OffsetRange;
 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;
@@ -801,7 +802,7 @@ class OccurenceBuilder {
                             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);
                             }
                         }
                     }
@@ -1907,6 +1908,10 @@ class OccurenceBuilder {
     }
 
     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()) {
@@ -1915,7 +1920,24 @@ class OccurenceBuilder {
                 }
                 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(),
diff --git 
a/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php 
b/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php
new file mode 100644
index 0000000000..bf7638cf9f
--- /dev/null
+++ 
b/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php
@@ -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();
diff --git 
a/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php.testGH4382_01.findUsages
 
b/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php.testGH4382_01.findUsages
new file mode 100644
index 0000000000..6a85cdb122
--- /dev/null
+++ 
b/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php.testGH4382_01.findUsages
@@ -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
\ No newline at end of file
diff --git 
a/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php.testGH4382_02.findUsages
 
b/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php.testGH4382_02.findUsages
new file mode 100644
index 0000000000..da26cbc1e3
--- /dev/null
+++ 
b/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php.testGH4382_02.findUsages
@@ -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
\ No newline at end of file
diff --git 
a/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php.testGH4382_03.findUsages
 
b/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php.testGH4382_03.findUsages
new file mode 100644
index 0000000000..2eceda5c3f
--- /dev/null
+++ 
b/php/php.refactoring/test/unit/data/testfiles/findusages/testGH4382/index.php.testGH4382_03.findUsages
@@ -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
\ No newline at end of file
diff --git 
a/php/php.refactoring/test/unit/src/org/netbeans/modules/refactoring/php/findusages/WhereUsedSupportTest.java
 
b/php/php.refactoring/test/unit/src/org/netbeans/modules/refactoring/php/findusages/WhereUsedSupportTest.java
index cfb7b3815b..2739b9a3f7 100644
--- 
a/php/php.refactoring/test/unit/src/org/netbeans/modules/refactoring/php/findusages/WhereUsedSupportTest.java
+++ 
b/php/php.refactoring/test/unit/src/org/netbeans/modules/refactoring/php/findusages/WhereUsedSupportTest.java
@@ -372,4 +372,16 @@ public class WhereUsedSupportTest extends 
FindUsagesTestBase {
         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();");
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to