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