Skip to content

Commit

Permalink
Fix #252 - $argc and $argv as function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
zulus committed Oct 13, 2023
1 parent c91b7ae commit 5b7c8aa
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleProposal> RESERVED_WORDS = new ArrayList<>();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public void initializeSuperGlobals(Collection<String> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> superGlobals) {
super.initializeSuperGlobals(superGlobals);
superGlobals.remove("$php_errormsg");
}

@Override
public void initializeGlobals(Collection<String> globals) {
super.initializeGlobals(globals);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Test Globals
<? $a = ''; $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)
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ field($_REQUEST)
field($_SERVER)
field($_SESSION)
field($GLOBALS)

field($php_errormsg)
field($http_response_header)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Test Globals
<? $a = ''; $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)
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ field(_REQUEST)
field(_SERVER)
field(_SESSION)
field(GLOBALS)

field(php_errormsg)
field(http_response_header)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
--FILE--
<?php
function functionWithGlobal($argc, $argv) {}
function functionWithGlobal($_GET) {}
?>
--EXPECT--
[line=3, start=79, end=84] Cannot re-assign auto-global variable $_GET

0 comments on commit 5b7c8aa

Please sign in to comment.