From e53b9278c63011edf23dc592d66827e77d98edbe Mon Sep 17 00:00:00 2001 From: Junichi Yamamoto Date: Mon, 27 Nov 2023 14:31:38 +0900 Subject: [PATCH] PHP 8.3 Support: Dynamic class constant fetch (Part 5) - https://github.com/apache/netbeans/issues/6701 - https://wiki.php.net/rfc/dynamic_class_constant_fetch - Fix the `IntroduceSuggestion` and `PHP55UnhandledError` - Add unit tests --- .../php/editor/model/impl/VariousUtils.java | 4 +- .../verification/IntroduceSuggestion.java | 5 +- .../verification/PHP55UnhandledError.java | 14 ++-- .../testDynamicClassConstantFetch.php | 50 ++++++++++++ ...php.testDynamicClassConstantFetch_01.hints | 1 + ...php.testDynamicClassConstantFetch_02.hints | 1 + ...php.testDynamicClassConstantFetch_03.hints | 4 + ....testDynamicClassConstantFetch_03Fix.fixed | 51 +++++++++++++ ...php.testDynamicClassConstantFetch_04.hints | 1 + ...php.testDynamicClassConstantFetch_05.hints | 1 + ...php.testDynamicClassConstantFetch_06.hints | 1 + ...php.testDynamicClassConstantFetch_07.hints | 1 + ...DynamicClassConstantFetch_EnumCase01.hints | 1 + ...DynamicClassConstantFetch_EnumCase02.hints | 1 + ...DynamicClassConstantFetch_EnumCase03.hints | 6 ++ ...micClassConstantFetch_EnumCase03aFix.fixed | 53 +++++++++++++ ...micClassConstantFetch_EnumCase03bFix.fixed | 51 +++++++++++++ ...DynamicClassConstantFetch_EnumCase04.hints | 1 + ...DynamicClassConstantFetch_EnumCase05.hints | 1 + ...DynamicClassConstantFetch_EnumCase06.hints | 1 + ...estDynamicClassConstantFetch_Trait01.hints | 1 + ...estDynamicClassConstantFetch_Trait02.hints | 4 + ...DynamicClassConstantFetch_Trait02Fix.fixed | 51 +++++++++++++ .../verification/IntroduceSuggestionTest.java | 76 +++++++++++++++++++ 24 files changed, 373 insertions(+), 8 deletions(-) create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_01.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_02.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_03.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_03Fix.fixed create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_04.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_05.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_06.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_07.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_EnumCase01.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_EnumCase02.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_EnumCase03.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_EnumCase03aFix.fixed create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_EnumCase03bFix.fixed create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_EnumCase04.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_EnumCase05.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_EnumCase06.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_Trait01.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_Trait02.hints create mode 100644 php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php.testDynamicClassConstantFetch_Trait02Fix.fixed diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/VariousUtils.java b/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/VariousUtils.java index 84720f7184aa..effffeed3aa0 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/VariousUtils.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/VariousUtils.java @@ -52,7 +52,6 @@ import org.netbeans.modules.php.editor.model.FileScope; import org.netbeans.modules.php.editor.model.FunctionScope; import org.netbeans.modules.php.editor.model.IndexScope; -import org.netbeans.modules.php.editor.model.InterfaceScope; import org.netbeans.modules.php.editor.model.MethodScope; import org.netbeans.modules.php.editor.model.ModelElement; import org.netbeans.modules.php.editor.model.ModelUtils; @@ -1684,7 +1683,8 @@ public static Collection getStaticTypeName(Scope inScope, S csi = (EnumScope) methodInScope; } } - if (inScope instanceof ClassScope || inScope instanceof InterfaceScope) { + if (inScope instanceof TypeScope) { + // e.g. const EXAMPLE = self::UNDEFINED; csi = (TypeScope) inScope; } if (csi != null) { diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestion.java b/php/php.editor/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestion.java index 368569f68ab0..2d9c149a5b67 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestion.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/verification/IntroduceSuggestion.java @@ -80,6 +80,7 @@ import org.netbeans.modules.php.editor.parser.astnodes.Expression; import org.netbeans.modules.php.editor.parser.astnodes.FieldAccess; import org.netbeans.modules.php.editor.parser.astnodes.MethodInvocation; +import org.netbeans.modules.php.editor.parser.astnodes.NamespaceName; import org.netbeans.modules.php.editor.parser.astnodes.StaticConstantAccess; import org.netbeans.modules.php.editor.parser.astnodes.StaticFieldAccess; import org.netbeans.modules.php.editor.parser.astnodes.StaticMethodInvocation; @@ -349,7 +350,9 @@ public void visit(StaticConstantAccess staticConstantAccess) { if (CancelSupport.getDefault().isCancelled()) { return; } - if (lineBounds.containsInclusive(staticConstantAccess.getStartOffset())) { + if (!staticConstantAccess.isDynamicName() + && (staticConstantAccess.getDispatcher() instanceof NamespaceName) // e.g. ClassName::CONSTANT, self::CONSTANT + && lineBounds.containsInclusive(staticConstantAccess.getStartOffset())) { String constName = staticConstantAccess.getConstantName().getName(); String clzName = CodeUtils.extractUnqualifiedClassName(staticConstantAccess); diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/verification/PHP55UnhandledError.java b/php/php.editor/src/org/netbeans/modules/php/editor/verification/PHP55UnhandledError.java index 6cbb4bfcca43..676018087603 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/verification/PHP55UnhandledError.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/verification/PHP55UnhandledError.java @@ -124,11 +124,15 @@ public void visit(StaticConstantAccess node) { if (CancelSupport.getDefault().isCancelled()) { return; } - Identifier constant = node.getConstantName(); - if (constant != null) { - String constantName = constant.getName(); - if ("class".equals(constantName.toLowerCase())) { //NOI18N - createError(constant); + if (node.isDynamicName()) { + super.visit(node); + } else { + Identifier constant = node.getConstantName(); + if (constant != null) { + String constantName = constant.getName(); + if ("class".equals(constantName.toLowerCase())) { //NOI18N + createError(constant); + } } } } diff --git a/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php b/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php new file mode 100644 index 000000000000..c170a94ab150 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/verification/IntroduceSuggestion/testDynamicClassConstantFetch/testDynamicClassConstantFetch.php @@ -0,0 +1,50 @@ +