1 package net.sourceforge.phpdt.internal.compiler.parser;
3 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
5 import org.eclipse.jface.resource.ImageDescriptor;
8 import java.util.Hashtable;
10 import java.util.Enumeration;
15 * A function declaration.
18 public class PHPFunctionDeclaration extends PHPSegmentWithChildren {
20 /** The parameters of the function. */
21 private final Hashtable parameters;
23 /** The parameters of the function. */
24 private final Hashtable declaredVariables;
27 * A String representation of the function (it's generated once because it needs to iterate
28 * a hashtable == long.
30 private String stringRepresentation;
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
38 public PHPFunctionDeclaration(Object parent, String name, int index) {
39 super(parent, name, index);
41 declaredVariables = null;
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)
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();
59 * Get the image of a class.
60 * @return the image that represents a php class
62 public ImageDescriptor getImage() {
63 return PHPUiImages.DESC_FUN;
67 * Return the string representation of the function.
68 * @return the string representation of the function
70 public String toString() {
71 if (parameters == null) {
72 return super.toString();
74 return stringRepresentation;
78 * Create the String representation of the function.
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();
91 buff.append(o.toString());
94 stringRepresentation = buff.toString();
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
102 public PHPVarDeclaration getParameter(String parameterName) {
103 return (PHPVarDeclaration) parameters.get(parameterName);
107 * Return all parameters of the function.
108 * @return a hashtable containing all PHPVarDeclaration
110 public Hashtable getParameters() {
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
119 public PHPVarDeclaration getVariable(String variableName) {
120 return (PHPVarDeclaration) declaredVariables.get(variableName);
124 * Return all declared variables of the function.
125 * @return a hashtable containing all PHPVarDeclaration
127 public Hashtable getVariables() {
128 return declaredVariables;
131 public Object addVariable(PHPVarDeclaration var) {
132 return declaredVariables.put(var.getVariable().getName(),var);