fixed used variables : special variables like _REQUEST, _POST ... will not be reporte...
[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
8 /**
9  * A variable.
10  * It could be a simple variable, or contains another variable.
11  * @author Matthieu Casanova
12  */
13 public class Variable extends AbstractVariable {
14
15   /** The name of the variable. */
16   private String name;
17
18   /** A variable inside ($$varname). */
19   private AbstractVariable variable;
20
21   private static final String _GET = "_GET";
22   private static final String _POST = "_POST";
23   private static final String _REQUEST = "_REQUEST";
24   private static final String _SERVER = "_SERVER";
25   private static final String _SESSION = "_SESSION";
26   private static final String _this = "this";
27
28   /**
29    * Create a new simple variable.
30    * @param name the name
31    * @param sourceStart the starting position
32    * @param sourceEnd the ending position
33    */
34   public Variable(final String name,
35                   final int sourceStart,
36                   final int sourceEnd) {
37     super(sourceStart, sourceEnd);
38     this.name = name;
39   }
40
41   /**
42    * Create a special variable ($$toto for example).
43    * @param variable the variable contained
44    * @param sourceStart the starting position
45    * @param sourceEnd the ending position
46    */
47   public Variable(final AbstractVariable variable,
48                   final int sourceStart,
49                   final int sourceEnd) {
50     super(sourceStart, sourceEnd);
51     this.variable = variable;
52   }
53
54   /**
55    * Return the expression as String.
56    * @return the expression
57    */
58   public String toStringExpression() {
59     return "$" + getName();
60   }
61
62   public String getName() {
63     if (variable == null) {
64       return name;
65     }
66     return variable.toStringExpression();
67   }
68
69   /**
70    * Get the variables from outside (parameters, globals ...)
71    * @return the variables from outside
72    */
73   public List getOutsideVariable() {
74     return new ArrayList(1);
75   }
76
77   /**
78    * get the modified variables.
79    * @return the variables modified
80    */
81   public List getModifiedVariable() {
82     return new ArrayList(1);
83   }
84
85   /**
86    * Get the variables used.
87    * @return the variables used
88    */
89   public List getUsedVariable() {
90     final String varName;
91     if (name == null) {
92       varName = variable.getName();
93     } else {
94       varName = name;
95     }
96     if (name.equals(_GET) ||
97         name.equals(_POST) ||
98         name.equals(_REQUEST) ||
99         name.equals(_SERVER) ||
100         name.equals(_SESSION) ||
101         name.equals(_this)) {
102       return new ArrayList(1);
103     }
104     final ArrayList list = new ArrayList(1);
105     list.add(new VariableUsage(varName, sourceStart));
106     return list;
107   }
108 }