package net.sourceforge.phpdt.internal.compiler.parser; import java.util.Enumeration; import java.util.Hashtable; import net.sourceforge.phpdt.internal.ui.PHPUiImages; import org.eclipse.jface.resource.ImageDescriptor; /** * A function declaration. * @author khartlage */ public class PHPFunctionDeclaration extends PHPSegmentWithChildren { /** The parameters of the function. */ private final Hashtable parameters; /** The parameters of the function. */ private final Hashtable declaredVariables; /** * A String representation of the function (it's generated once because it needs to iterate * a hashtable == long. */ private String stringRepresentation; /** * Create a function declaration. * @param parent the parent object (it should be a php class) * @param name the name of the function * @param index where the function is in the file */ public PHPFunctionDeclaration(Object parent, String name, int index) { super(parent, name, index); parameters = null; declaredVariables = null; } /** * Create a function declaration. * @param parent the parent object (it should be a php class) * @param name the name of the function * @param index where the function is in the file * @param parameters the list of parameters (it should contains only PHPVar) */ public PHPFunctionDeclaration(Object parent, String name, int index, Hashtable parameters) { super(parent, name, index); this.parameters = parameters; declaredVariables = (Hashtable) parameters.clone(); createStringView(); } /** * Get the image of a class. * @return the image that represents a php class */ public ImageDescriptor getImage() { return PHPUiImages.DESC_FUN; } /** * Return the string representation of the function. * @return the string representation of the function */ public String toString() { if (parameters == null) { return super.toString(); } return stringRepresentation; } /** * Create the String representation of the function. */ private void createStringView() { StringBuffer buff = new StringBuffer(name).append("("); Enumeration vars = parameters.elements(); boolean first = false; while (vars.hasMoreElements()) { PHPVarDeclaration o = (PHPVarDeclaration) vars.nextElement(); if (first) { buff.append(","); } else { first = true; } buff.append(o.toString()); } buff.append(")"); stringRepresentation = buff.toString(); } /** * Return a parameter of the function * @param parameterName the name of the parameter * @return it will return the PHPVarDeclaration of the parameter asked, or null */ public PHPVarDeclaration getParameter(String parameterName) { return (PHPVarDeclaration) parameters.get(parameterName); } /** * Return all parameters of the function. * @return a hashtable containing all PHPVarDeclaration */ public Hashtable getParameters() { return parameters; } /** * Return a variable of the function * @param variableName the name of the parameter * @return it will return the PHPVarDeclaration of the parameter asked, or null */ public PHPVarDeclaration getVariable(String variableName) { return (PHPVarDeclaration) declaredVariables.get(variableName); } /** * Return all declared variables of the function. * @return a hashtable containing all PHPVarDeclaration */ public Hashtable getVariables() { return declaredVariables; } public Object addVariable(PHPVarDeclaration var) { return declaredVariables.put(var.getVariable().getName(),var); } }