4e81cb5636857af23b26f7c6871fe97e75124179
[phpeclipse.git] / net.sourceforge.phpeclipse / src / test / PHPVar.java
1 package test;
2
3 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
4
5 /**
6  * A Variable usage. It could be a first use, an in code use of an already declared var.
7  * In fact I'm not sure for the moment I will keep this
8  * @author Matthieu Casanova
9  */
10 public class PHPVar {
11
12   /** The name of the variable. It couldn't be changed. */
13   private final String name;
14
15   /** The value. It could change. */
16   private String value;
17
18   /**
19    * Tell if the variable is a reference or not (given in function by &$varname,
20    * or comming from a 'global' keyword.
21    */
22   private boolean reference;
23
24   /**
25    * Does the variable have a value or not.
26    * If we don't know if it was initialized it should be set on true.
27    * (when we have a global keyword for example)
28    */
29   private boolean initialized;
30
31   /** This variable indicate if it is used or not in the code. */
32   private boolean used;
33
34   /**
35    * We initialize the name and the value of the variable.
36    * @param name the name of the variable
37    * @param value the value of the variable
38    */
39   public PHPVar(String name, String value) {
40     this.name = name;
41     this.value = value;
42     initialized = value != null;
43   }
44
45   /**
46    * We initialize the name of the variable. The value will be null
47    * @param name the name of the variable
48    */
49   public PHPVar(String name) {
50     this.name = name;
51   }
52
53   /**
54    * Initialize the variable name and set the initialization status.
55    * @param name the name of the variable
56    * @param initialized the initialization status (it should be true or it's unuseful)
57    */
58   public PHPVar(String name, boolean initialized) {
59     this(name);
60     this.initialized = initialized;
61   }
62
63   /**
64    * Give a reference to the variable.
65    * @param reference a reference
66    */
67   public void setReference(boolean reference) {
68     this.reference = reference;
69     if (reference) {// a reference variable status is unknown so is initialized for me
70       //example : global
71       initialized = true;
72     }
73   }
74
75   /**
76    * Tell if the variable is reference.
77    * @return a boolean
78    */
79   public boolean isReference() {
80     return reference;
81   }
82
83   public void setUsed(boolean used) {
84     this.used = used;
85   }
86
87   public void setInitialized(boolean initialized) {
88     this.initialized = initialized;
89   }
90
91   /**
92    * Get the name of the variable.
93    * @return a string containing the name of the variable
94    */
95   public String getName() {
96     return name;
97   }
98
99   /**
100    * Tell if the variable was used.
101    * @return a boolean telling if the variable is used
102    */
103   public boolean isUsed() {
104     return used;
105   }
106
107   /**
108    * Return a human readable variable (:
109    * @return  a string representation of the object.
110    */
111   public String toString() {
112     if (value == null) {
113       if (reference) {
114         return "&$" + name;
115       }
116       return "$" + name;
117     }
118     if (reference) {
119       return "&$" + name + "=" + value;
120     }
121     return "$" + name + "=" + value;
122   }
123 }