Skip to content

Commit

Permalink
[GH-8286] Fix Go to Declaration for an enum method using an FQ name
Browse files Browse the repository at this point in the history
-Accessing a Constant of an Enum/Class now uses FQ name if possible

Example:
```php
namespace test;

enum TestA{

        case X;

        public function get(string $param): string{
                return '';
        }
}

namespace test2;

enum TestB{

        case X;

        public function get(): string{
                return '';
        }
}

\test\TestA::X->get('xxx'); // go to declaration and mark occurences do not work
```
  • Loading branch information
NReib committed Mar 5, 2025
1 parent f198805 commit 625ebde
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,13 @@ private static String extractVariableTypeFromVariableBase(VariableBase varBase,
} else if (varBase instanceof StaticConstantAccess) {
StaticConstantAccess constantAccess = (StaticConstantAccess) varBase;
if (!constantAccess.isDynamicName()) {
String clsName = CodeUtils.extractUnqualifiedName(constantAccess.getDispatcher());
Expression dispatcher = constantAccess.getDispatcher();
String clsName = null;
if (dispatcher instanceof NamespaceName) {
clsName = CodeUtils.extractQualifiedName(dispatcher);
} else {
clsName = CodeUtils.extractUnqualifiedName(dispatcher);
}
String constName = CodeUtils.extractQualifiedName(constantAccess.getConstant());
if (constName != null) {
if (clsName != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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 test;

enum TestA{

case X;

public function get(string $param): string{//01
return '';
}
}

namespace test2;

enum TestB{

case X;

public function get(string $param): string{//02
return '';
}
}

\test\TestA::X->get();
\test2\TestB::X->get();
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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 test;

enum TestA{

case X;

public function get(string $param): string{
return '';
}
}

namespace test2;

enum TestB{

case X;

public function get(): string{
return '';
}
}

\test\TestA::X->get();
\test2\TestB::X->get();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public function |>MARK_OCCURRENCES:get<|(string $param): string{
\test\TestA::X->|>MARK_OCCURRENCES:ge^t<|();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public function |>MARK_OCCURRENCES:get<|(): string{
\test2\TestB::X->|>MARK_OCCURRENCES:ge^t<|();
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.csl;

public class GotoDeclarationGH8286Test extends GotoDeclarationTestBase {

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

public void testIssueGH8286_01() throws Exception {
checkDeclaration(getTestPath(), "\\test\\TestA::X->ge^t();", "public function ^get(string $param): string{//01");
}

public void testIssueGH8286_02() throws Exception {
checkDeclaration(getTestPath(), "\\test2\\TestB::X->ge^t();", "public function ^get(string $param): string{//02");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.csl;

public class OccurrencesFinderImplGH8286Test extends OccurrencesFinderImplTestBase {

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

public void testIssueGH8286_01() throws Exception {
checkOccurrences(getTestPath(), "\\test\\TestA::X->ge^t();", true);
}

public void testIssueGH8286_02() throws Exception {
checkOccurrences(getTestPath(), "\\test2\\TestB::X->ge^t();", true);
}
}

0 comments on commit 625ebde

Please sign in to comment.