changed how special variables are detected
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / Variable.java
1 package net.sourceforge.phpdt.internal.compiler.ast;
2
3 import net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage;
4
5 import java.util.List;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8
9 /**
10  * A variable.
11  * It could be a simple variable, or contains another variable.
12  * @author Matthieu Casanova
13  */
14 public class Variable extends AbstractVariable {
15
16   /** The name of the variable. */
17   private String name;
18
19   /** A variable inside ($$varname). */
20   private AbstractVariable variable;
21
22   public static final String _GET = "_GET";
23   public static final String _POST = "_POST";
24   public static final String _REQUEST = "_REQUEST";
25   public static final String _SERVER = "_SERVER";
26   public static final String _SESSION = "_SESSION";
27   public static final String _this = "this";
28   public static final String GLOBALS = "GLOBALS";
29   public static final String _COOKIE = "_COOKIE";
30   public static final String _FILES = "_FILES";
31   public static final String _ENV = "_ENV";
32
33   /** Here is an array of all superglobals variables and the special "this". */
34   public static final String[] SPECIAL_VARS = {_GET,
35                                                _POST,
36                                                _REQUEST,
37                                                _SERVER,
38                                                _SESSION,
39                                                _this,
40                                                GLOBALS,
41                                                _COOKIE,
42                                                _FILES,
43                                                _ENV};
44
45   /**
46    * Create a new simple variable.
47    * @param name the name
48    * @param sourceStart the starting position
49    * @param sourceEnd the ending position
50    */
51   public Variable(final String name,
52                   final int sourceStart,
53                   final int sourceEnd) {
54     super(sourceStart, sourceEnd);
55     this.name = name;
56   }
57
58   /**
59    * Create a special variable ($$toto for example).
60    * @param variable the variable contained
61    * @param sourceStart the starting position
62    * @param sourceEnd the ending position
63    */
64   public Variable(final AbstractVariable variable,
65                   final int sourceStart,
66                   final int sourceEnd) {
67     super(sourceStart, sourceEnd);
68     this.variable = variable;
69   }
70
71   /**
72    * Return the expression as String.
73    * @return the expression
74    */
75   public String toStringExpression() {
76     return "$" + getName();
77   }
78
79   public String getName() {
80     if (variable == null) {
81       return name;
82     }
83     return variable.toStringExpression();
84   }
85
86   /**
87    * Get the variables from outside (parameters, globals ...)
88    * @return the variables from outside
89    */
90   public List getOutsideVariable() {
91     return new ArrayList(1);
92   }
93
94   /**
95    * get the modified variables.
96    * @return the variables modified
97    */
98   public List getModifiedVariable() {
99     return new ArrayList(1);
100   }
101
102   /**
103    * Get the variables used.
104    * @return the variables used
105    */
106   public List getUsedVariable() {
107     final String varName;
108     if (name == null) {
109       varName = variable.getName();
110     } else {
111       varName = name;
112     }
113     if (arrayContains(SPECIAL_VARS, name)) {
114       return new ArrayList(1);
115     }
116     final ArrayList list = new ArrayList(1);
117     list.add(new VariableUsage(varName, sourceStart));
118     return list;
119   }
120 }