diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/compiler/ast/visitor/ValidatorVisitor.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/compiler/ast/visitor/ValidatorVisitor.java index e8d5d4ca46..e5b414ad94 100644 --- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/compiler/ast/visitor/ValidatorVisitor.java +++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/compiler/ast/visitor/ValidatorVisitor.java @@ -79,6 +79,8 @@ public class ValidatorVisitor extends PHPASTVisitor implements IValidatorVisitor private static final int ALLOW_ARRAY = 1; private static final int ALLOW_NEW = 2; + private String[] globals = null; + // https://www.php.net/manual/en/reserved.other-reserved-words.php private static final List RESERVED_WORDS = new ArrayList<>(); @@ -1215,7 +1217,7 @@ public boolean visit(ConstantDeclaration s) throws Exception { public boolean visit(FormalParameter s) throws Exception { validateConstantExpression(s.getInitialization(), ALLOW_ARRAY | ALLOW_NEW); if (version.isGreaterThan(PHPVersion.PHP5_3) && s.getParameterName() != null - && PHPVariables.isVariable(s.getParameterName().getName(), version)) { + && isSuperGlobal(s.getParameterName().getName())) { reportProblem(s.getParameterName(), Messages.ReassignAutoGlobalVariable, PHPProblemIdentifier.ReassignAutoGlobalVariable, s.getParameterName().getName(), ProblemSeverities.Error); @@ -1448,4 +1450,35 @@ public PHPVersion getPHPVersion() { return version; } + /** + * @param name + * @return + */ + private boolean isSuperGlobal(String name) { + if ("$GLOBALS".equals(name)) { //$NON-NLS-1$ + return true; + } + if (!name.startsWith("$_")) { //$NON-NLS-1$ + return false; + } + + return isGlobal(name); + } + + /** + * @param name + * @return + */ + private boolean isGlobal(String name) { + if (globals == null) { + globals = PHPVariables.getVariables(getPHPVersion()); + } + for (String global : globals) { + if (global.equals(name)) { + return true; + } + } + return false; + } + } diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariables.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariables.java index 0ba9dfc74f..908b970800 100644 --- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariables.java +++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariables.java @@ -48,13 +48,7 @@ public static boolean isVariable(@Nullable String name, PHPVersion phpVersion) { if (name == null) { return false; } - String[] variables = getVariables(phpVersion); - for (String variable : variables) { - if (variable.equals(name)) { - return true; - } - } - return false; + return checkName(getVariables(phpVersion), name); } public static String[] getVariables(PHPVersion phpVersion) { @@ -65,11 +59,18 @@ public static String[] getVariables(PHPVersion phpVersion, int type) { return getInstance(phpVersion).getByType(type); } + public static String[] getSuperGlobalVariables(PHPVersion phpVersion) { + return getVariables(phpVersion, SUPER_GLOBAL); + } + public static boolean isSuperGlobal(@Nullable String name, PHPVersion phpVersion) { if (name == null) { return false; } - String[] variables = getInstance(phpVersion).variables.get(SUPER_GLOBAL); + return checkName(getInstance(phpVersion).variables.get(SUPER_GLOBAL), name); + } + + private static boolean checkName(String[] variables, String name) { for (String variable : variables) { if (variable.equals(name)) { return true; @@ -104,10 +105,12 @@ private static PHPVariables getInstance(PHPVersion phpVersion) { case PHP7_2: case PHP7_3: case PHP7_4: + initializer = new PHPVariablesInitializerPHP_5_4(); + break; case PHP8_0: case PHP8_1: case PHP8_2: - initializer = new PHPVariablesInitializerPHP_5_4(); + initializer = new PHPVariablesInitializerPHP_8_0(); break; case PHP5: case PHP5_3: diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariablesInitializerPHP_5.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariablesInitializerPHP_5.java index 6d5b03c6eb..04d5cfa958 100644 --- a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariablesInitializerPHP_5.java +++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariablesInitializerPHP_5.java @@ -27,6 +27,8 @@ public void initializeSuperGlobals(Collection superGlobals) { superGlobals.add("$_SERVER"); //$NON-NLS-1$ superGlobals.add("$_SESSION"); //$NON-NLS-1$ superGlobals.add("$GLOBALS"); //$NON-NLS-1$ + superGlobals.add("$php_errormsg"); //$NON-NLS-1$ + superGlobals.add("$http_response_header"); //$NON-NLS-1$ } @Override diff --git a/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariablesInitializerPHP_8_0.java b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariablesInitializerPHP_8_0.java new file mode 100644 index 0000000000..4a0fe7925c --- /dev/null +++ b/plugins/org.eclipse.php.core/src/org/eclipse/php/internal/core/language/PHPVariablesInitializerPHP_8_0.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2015 Dawid Pakuła and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Dawid Pakuła - initial API and implementation + *******************************************************************************/ +package org.eclipse.php.internal.core.language; + +import java.util.Collection; + +public class PHPVariablesInitializerPHP_8_0 extends PHPVariablesInitializerPHP_5_4 { + + @Override + public void initializeSuperGlobals(Collection superGlobals) { + super.initializeSuperGlobals(superGlobals); + superGlobals.remove("$php_errormsg"); + } + + @Override + public void initializeGlobals(Collection globals) { + super.initializeGlobals(globals); + } + +} diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php5/exclusive/testArrayKey2.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php5/exclusive/testArrayKey2.pdtt index 22b96978be..4690fd90a7 100644 --- a/tests/org.eclipse.php.core.tests/workspace/codeassist/php5/exclusive/testArrayKey2.pdtt +++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php5/exclusive/testArrayKey2.pdtt @@ -4,6 +4,13 @@ Test Globals --EXPECT-- field(a) +field(HTTP_COOKIE_VARS) +field(HTTP_ENV_VARS) +field(HTTP_GET_VARS) +field(HTTP_POST_FILES) +field(HTTP_POST_VARS) +field(HTTP_SERVER_VARS) +field(HTTP_SESSION_VARS) field(argc) field(argv) field(_COOKIE) @@ -15,10 +22,5 @@ field(_REQUEST) field(_SERVER) field(_SESSION) field(GLOBALS) -field(HTTP_COOKIE_VARS) -field(HTTP_ENV_VARS) -field(HTTP_GET_VARS) -field(HTTP_POST_FILES) -field(HTTP_POST_VARS) -field(HTTP_SERVER_VARS) -field(HTTP_SESSION_VARS) +field(php_errormsg) +field(http_response_header) diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php5/testFunctionVariables1.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php5/exclusive/testFunctionVariables1.pdtt similarity index 80% rename from tests/org.eclipse.php.core.tests/workspace/codeassist/php5/testFunctionVariables1.pdtt rename to tests/org.eclipse.php.core.tests/workspace/codeassist/php5/exclusive/testFunctionVariables1.pdtt index 16319323b6..a30d9b5f8e 100644 --- a/tests/org.eclipse.php.core.tests/workspace/codeassist/php5/testFunctionVariables1.pdtt +++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php5/exclusive/testFunctionVariables1.pdtt @@ -12,4 +12,5 @@ field($_REQUEST) field($_SERVER) field($_SESSION) field($GLOBALS) - +field($php_errormsg) +field($http_response_header) diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/exclusive/testArrayKey2.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/exclusive/testArrayKey2.pdtt index 22b96978be..4690fd90a7 100644 --- a/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/exclusive/testArrayKey2.pdtt +++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php53/exclusive/testArrayKey2.pdtt @@ -4,6 +4,13 @@ Test Globals --EXPECT-- field(a) +field(HTTP_COOKIE_VARS) +field(HTTP_ENV_VARS) +field(HTTP_GET_VARS) +field(HTTP_POST_FILES) +field(HTTP_POST_VARS) +field(HTTP_SERVER_VARS) +field(HTTP_SESSION_VARS) field(argc) field(argv) field(_COOKIE) @@ -15,10 +22,5 @@ field(_REQUEST) field(_SERVER) field(_SESSION) field(GLOBALS) -field(HTTP_COOKIE_VARS) -field(HTTP_ENV_VARS) -field(HTTP_GET_VARS) -field(HTTP_POST_FILES) -field(HTTP_POST_VARS) -field(HTTP_SERVER_VARS) -field(HTTP_SESSION_VARS) +field(php_errormsg) +field(http_response_header) diff --git a/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testArrayKey2.pdtt b/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/classExclusive/testArrayKey2.pdtt similarity index 83% rename from tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testArrayKey2.pdtt rename to tests/org.eclipse.php.core.tests/workspace/codeassist/php54/classExclusive/testArrayKey2.pdtt index 7ec56a671c..dc947d82b1 100644 --- a/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/testArrayKey2.pdtt +++ b/tests/org.eclipse.php.core.tests/workspace/codeassist/php54/classExclusive/testArrayKey2.pdtt @@ -15,4 +15,5 @@ field(_REQUEST) field(_SERVER) field(_SESSION) field(GLOBALS) - +field(php_errormsg) +field(http_response_header) \ No newline at end of file diff --git a/tests/org.eclipse.php.core.tests/workspace/errors/php7/globalVariableAsFunctionName.pdtt b/tests/org.eclipse.php.core.tests/workspace/errors/php7/globalVariableAsFunctionName.pdtt new file mode 100644 index 0000000000..a89ecbc704 --- /dev/null +++ b/tests/org.eclipse.php.core.tests/workspace/errors/php7/globalVariableAsFunctionName.pdtt @@ -0,0 +1,8 @@ +--TEST-- +--FILE-- + +--EXPECT-- +[line=3, start=79, end=84] Cannot re-assign auto-global variable $_GET