1 package net.sourceforge.phpdt.internal.compiler.ast;
3 import net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage;
4 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
5 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
6 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
7 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
8 import org.eclipse.core.runtime.CoreException;
9 import org.eclipse.jface.resource.ImageDescriptor;
10 import org.eclipse.jface.text.Position;
11 import test.PHPParserSuperclass;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
18 * A Method declaration.
20 * @author Matthieu Casanova
22 public final class MethodDeclaration extends Statement implements OutlineableWithChildren {
24 /** The name of the method. */
25 public final String name;
26 private final ArrayList arguments;
29 public Statement[] statements;
30 private final int bodyStart;
31 private int bodyEnd = -1;
32 /** Tell if the method is a class constructor. */
33 public boolean isConstructor;
35 /** The parent object. */
36 private Object parent;
37 /** The outlineable children (those will be in the node array too. */
38 private final ArrayList children = new ArrayList();
40 /** Tell if the method returns a reference. */
41 private final boolean reference;
43 private final Position position;
45 public MethodDeclaration(final Object parent,
47 final ArrayList arguments,
48 final boolean reference,
49 final int sourceStart,
53 super(sourceStart, sourceEnd);
55 this.arguments = arguments;
57 this.reference = reference;
58 this.bodyStart = bodyStart;
59 this.bodyEnd = bodyEnd;
60 position = new Position(sourceStart, sourceEnd);
64 * Return method into String, with a number of tabs
66 * @param tab the number of tabs
67 * @return the String containing the method
69 public String toString(final int tab) {
70 final StringBuffer buff = new StringBuffer(tabString(tab));
71 buff.append(toStringHeader());
72 buff.append(toStringStatements(tab + 1));
73 return buff.toString();
76 private String toStringHeader() {
77 return "function " + toString();
81 * Return the statements of the method into Strings
83 * @param tab the number of tabs
84 * @return the String containing the statements
86 private String toStringStatements(final int tab) {
87 final StringBuffer buff = new StringBuffer(" {"); //$NON-NLS-1$
88 if (statements != null) {
89 for (int i = 0; i < statements.length; i++) {
90 buff.append("\n").append(statements[i].toString(tab)); //$NON-NLS-1$
91 if (!(statements[i] instanceof Block)) {
92 buff.append(";"); //$NON-NLS-1$
96 buff.append("\n").append(tabString(tab == 0 ? 0 : tab - 1)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
97 return buff.toString();
101 * Get the image of a class.
103 * @return the image that represents a php class
105 public ImageDescriptor getImage() {
106 return PHPUiImages.DESC_FUN;
109 public void setParent(final Object parent) {
110 this.parent = parent;
113 public Object getParent() {
117 public boolean add(final Outlineable o) {
118 return children.add(o);
121 public Outlineable get(final int index) {
122 return (Outlineable) children.get(index);
126 return children.size();
129 public String toString() {
130 final StringBuffer buff = new StringBuffer();
132 buff.append("&");//$NON-NLS-1$
134 buff.append(name).append("(");//$NON-NLS-1$
136 if (arguments != null) {
137 for (int i = 0; i < arguments.size(); i++) {
138 final VariableDeclaration o = (VariableDeclaration) arguments.get(i);
139 buff.append(o.toStringExpression());
140 if (i != (arguments.size() - 1)) {
141 buff.append(", "); //$NON-NLS-1$
145 buff.append(")"); //$NON-NLS-1$
146 return buff.toString();
149 public Position getPosition() {
153 public List getList() {
158 * Get the variables from outside (parameters, globals ...)
160 * @param list the list where we will put variables
162 public void getOutsideVariable(final List list) {}
165 * get the modified variables.
167 * @param list the list where we will put variables
169 public void getModifiedVariable(final List list) {}
172 * This method will analyze the code.
174 * @param list the list where we will put variables
176 public void getUsedVariable(final List list) {}
179 * Get global variables (not parameters).
181 private void getGlobalVariable(final List list) {
182 if (statements != null) {
183 for (int i = 0; i < statements.length; i++) {
184 statements[i].getOutsideVariable(list);
189 private void getParameters(final List list) {
190 if (arguments != null) {
191 for (int i = 0; i < arguments.size(); i++) {
192 final VariableDeclaration variable = (VariableDeclaration) arguments.get(i);
193 list.add(new VariableUsage(variable.name(), variable.sourceStart));
199 * get the modified variables.
201 private void getAssignedVariableInCode(final List list) {
202 if (statements != null) {
203 for (int i = 0; i < statements.length; i++) {
204 statements[i].getModifiedVariable(list);
210 * Get the variables used.
212 private void getUsedVariableInCode(final List list) {
213 if (statements != null) {
214 for (int i = 0; i < statements.length; i++) {
215 statements[i].getUsedVariable(list);
220 private static boolean isVariableDeclaredBefore(final List list, final VariableUsage var) {
221 final String name = var.getName();
222 final int pos = var.getStartOffset();
223 for (int i = 0; i < list.size(); i++) {
224 final VariableUsage variableUsage = (VariableUsage) list.get(i);
225 if (variableUsage.getName().equals(name) && variableUsage.getStartOffset() < pos) {
233 * This method will analyze the code.
235 public void analyzeCode() {
236 if (statements != null) {
237 for (int i = 0; i < statements.length; i++) {
238 statements[i].analyzeCode();
243 final List globalsVars = new ArrayList();
244 getGlobalVariable(globalsVars);
245 final List modifiedVars = new ArrayList();
246 getAssignedVariableInCode(modifiedVars);
247 final List parameters = new ArrayList(arguments.size());
248 getParameters(parameters);
250 final List declaredVars = new ArrayList(globalsVars.size() + modifiedVars.size());
251 declaredVars.addAll(globalsVars);
252 declaredVars.addAll(modifiedVars);
253 declaredVars.addAll(parameters);
255 final List usedVars = new ArrayList();
256 getUsedVariableInCode(usedVars);
257 final List readOrWriteVars = new ArrayList(modifiedVars.size() + usedVars.size());
258 readOrWriteVars.addAll(modifiedVars);
259 readOrWriteVars.addAll(usedVars);
261 //look for used variables that were not declared before
262 findUnusedParameters(readOrWriteVars, parameters);
263 findUnknownUsedVars(usedVars, declaredVars);
267 * This method will add a warning on all unused parameters.
269 * @param vars the used variable list
270 * @param parameters the declared variable list
272 private static void findUnusedParameters(final List vars, final List parameters) {
273 for (int i = 0; i < parameters.size(); i++) {
274 final VariableUsage param = (VariableUsage) parameters.get(i);
275 if (!isVariableInList(param.getName(), vars)) {
277 PHPParserSuperclass.setMarker(
278 "warning, the parameter " + param.getName() + " seems to be never used in your method",
279 param.getStartOffset(),
280 param.getStartOffset() + param.getName().length(),
281 PHPParserSuperclass.WARNING,
283 } catch (CoreException e) {
284 PHPeclipsePlugin.log(e);
291 * Tell if the list of VariableUsage contains a variable named by the name given.
293 * @param name the variable name
294 * @param list the list of VariableUsage
295 * @return true if the variable is in the list false otherwise
297 private static boolean isVariableInList(final String name, final List list) {
298 for (int i = 0; i < list.size(); i++) {
299 if (((VariableUsage) list.get(i)).getName().equals(name)) {
307 * This method will add a warning on all used variables in a method that aren't declared before.
309 * @param usedVars the used variable list
310 * @param declaredVars the declared variable list
312 private static void findUnknownUsedVars(final List usedVars, final List declaredVars) {
313 final HashSet list = new HashSet(usedVars.size());
314 for (int i = 0; i < usedVars.size(); i++) {
315 final VariableUsage variableUsage = (VariableUsage) usedVars.get(i);
316 if ("this".equals(variableUsage.getName())) continue; // this is a special variable
317 if (!list.contains(variableUsage.getName()) && !isVariableDeclaredBefore(declaredVars, variableUsage)) {
318 list.add(variableUsage.getName());
320 PHPParserSuperclass.setMarker(
321 "warning, usage of a variable that seems to be unassigned yet : " + variableUsage.getName(),
322 variableUsage.getStartOffset(),
323 variableUsage.getStartOffset() + variableUsage.getName().length(),
324 PHPParserSuperclass.WARNING,
326 } catch (CoreException e) {
327 PHPeclipsePlugin.log(e);