7bb9832a0852a929b915bec1c9f84455ef7588e7
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / parser / PHPFunctionDeclaration.java
1 package net.sourceforge.phpdt.internal.compiler.parser;
2
3 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
4
5 import org.eclipse.jface.resource.ImageDescriptor;
6
7 import java.util.List;
8 import java.util.Hashtable;
9 import java.util.Map;
10 import java.util.Enumeration;
11
12 import test.PHPVar;
13
14 /**
15  * A function declaration.
16  * @author khartlage
17  */
18 public class PHPFunctionDeclaration extends PHPSegmentWithChildren {
19
20   /** The parameters of the function. */
21   private final Hashtable parameters;
22
23   /** The parameters of the function. */
24   private final Hashtable declaredVariables;
25
26   /**
27    * A String representation of the function (it's generated once because it needs to iterate
28    * a hashtable == long.
29    */
30   private String stringRepresentation;
31
32   /**
33    * Create a function declaration.
34    * @param parent the parent object (it should be a php class)
35    * @param name the name of the function
36    * @param index where the function is in the file
37    */
38   public PHPFunctionDeclaration(Object parent, String name, int index) {
39     super(parent, name, index);
40     parameters = null;
41     declaredVariables = null;
42   }
43
44   /**
45    * Create a function declaration.
46    * @param parent the parent object (it should be a php class)
47    * @param name the name of the function
48    * @param index where the function is in the file
49    * @param parameters the list of parameters (it should contains only PHPVar)
50    */
51   public PHPFunctionDeclaration(Object parent, String name, int index, Hashtable parameters) {
52     super(parent, name, index);
53     this.parameters = parameters;
54     declaredVariables = (Hashtable) parameters.clone();
55     createStringView();
56   }
57
58   /**
59    * Get the image of a class.
60    * @return the image that represents a php class
61    */
62   public ImageDescriptor getImage() {
63     return PHPUiImages.DESC_FUN;
64   }
65
66   /**
67    * Return the string representation of the function.
68    * @return the string representation of the function
69    */
70   public String toString() {
71     if (parameters == null) {
72       return super.toString();
73     }
74     return stringRepresentation;
75   }
76
77   /**
78    * Create the String representation of the function.
79    */
80   private void createStringView() {
81     StringBuffer buff = new StringBuffer(name).append("(");
82     Enumeration vars = parameters.elements();
83     boolean first = false;
84     while (vars.hasMoreElements()) {
85       PHPVarDeclaration o = (PHPVarDeclaration) vars.nextElement();
86       if (first) {
87         buff.append(",");
88       } else {
89         first = true;
90       }
91       buff.append(o.toString());
92     }
93     buff.append(")");
94     stringRepresentation = buff.toString();
95   }
96
97   /**
98    * Return a parameter of the function
99    * @param parameterName the name of the parameter
100    * @return it will return the PHPVarDeclaration of the parameter asked, or null
101    */
102   public PHPVarDeclaration getParameter(String parameterName) {
103     return (PHPVarDeclaration) parameters.get(parameterName);
104   }
105
106   /**
107    * Return all parameters of the function.
108    * @return a hashtable containing all PHPVarDeclaration
109    */
110   public Hashtable getParameters() {
111     return parameters;
112   }
113
114   /**
115    * Return a variable of the function
116    * @param variableName the name of the parameter
117    * @return it will return the PHPVarDeclaration of the parameter asked, or null
118    */
119   public PHPVarDeclaration getVariable(String variableName) {
120     return (PHPVarDeclaration) declaredVariables.get(variableName);
121   }
122
123   /**
124    * Return all declared variables of the function.
125    * @return a hashtable containing all PHPVarDeclaration
126    */
127   public Hashtable getVariables() {
128     return declaredVariables;
129   }
130
131   public Object addVariable(PHPVarDeclaration var) {
132     return declaredVariables.put(var.getVariable().getName(),var);
133   }
134 }