1 package net.sourceforge.phpdt.internal.compiler.ast;
3 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
4 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
5 import net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage;
6 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
7 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
8 import org.eclipse.jface.resource.ImageDescriptor;
9 import org.eclipse.jface.text.Position;
10 import org.eclipse.core.runtime.CoreException;
12 import java.util.ArrayList;
13 import java.util.List;
15 import test.PHPParserSuperclass;
18 * A Method declaration.
19 * @author Matthieu Casanova
21 public class MethodDeclaration extends Statement implements OutlineableWithChildren {
23 /** The name of the method. */
25 public ArrayList arguments;
28 public Statement[] statements;
30 public int bodyEnd = -1;
31 /** Tell if the method is a class constructor. */
32 public boolean isConstructor;
34 /** The parent object. */
35 private Object parent;
36 /** The outlineable children (those will be in the node array too. */
37 private ArrayList children = new ArrayList();
39 /** Tell if the method returns a reference. */
40 public boolean reference;
42 private Position position;
44 public MethodDeclaration(final Object parent,
46 final ArrayList arguments,
47 final boolean reference,
48 final int sourceStart,
52 super(sourceStart, sourceEnd);
54 this.arguments = arguments;
56 this.reference = reference;
57 this.bodyStart = bodyStart;
58 this.bodyEnd = bodyEnd;
59 position = new Position(sourceStart, sourceEnd);
63 * Return method into String, with a number of tabs
64 * @param tab the number of tabs
65 * @return the String containing the method
67 public String toString(final int tab) {
68 final StringBuffer buff = new StringBuffer(tabString(tab));
69 buff.append(toStringHeader());
70 buff.append(toStringStatements(tab + 1));
71 return buff.toString();
74 public String toStringHeader() {
75 return "function " + toString();
79 * Return the statements of the method into Strings
80 * @param tab the number of tabs
81 * @return the String containing the statements
83 public String toStringStatements(final int tab) {
84 final StringBuffer buff = new StringBuffer(" {"); //$NON-NLS-1$
85 if (statements != null) {
86 for (int i = 0; i < statements.length; i++) {
87 buff.append("\n").append(statements[i].toString(tab)); //$NON-NLS-1$
88 if (!(statements[i] instanceof Block)) {
89 buff.append(";"); //$NON-NLS-1$
93 buff.append("\n").append(tabString(tab == 0 ? 0 : tab - 1)).append("}"); //$NON-NLS-2$ //$NON-NLS-1$
94 return buff.toString();
98 * Get the image of a class.
99 * @return the image that represents a php class
101 public ImageDescriptor getImage() {
102 return PHPUiImages.DESC_FUN;
105 public void setParent(final Object parent) {
106 this.parent = parent;
109 public Object getParent() {
113 public boolean add(final Outlineable o) {
114 return children.add(o);
117 public Outlineable get(final int index) {
118 return (Outlineable) children.get(index);
122 return children.size();
125 public String toString() {
126 final StringBuffer buff = new StringBuffer();
128 buff.append("&");//$NON-NLS-1$
130 buff.append(name).append("(");//$NON-NLS-1$
132 if (arguments != null) {
133 for (int i = 0; i < arguments.size(); i++) {
134 VariableDeclaration o = (VariableDeclaration) arguments.get(i);
135 buff.append(o.toStringExpression());
136 if (i != (arguments.size() - 1)) {
137 buff.append(", "); //$NON-NLS-1$
141 buff.append(")"); //$NON-NLS-1$
142 return buff.toString();
145 public Position getPosition() {
149 public List getList() {
153 /** no outside variables. */
154 public void getOutsideVariable(final List list) {
157 public void getModifiedVariable(final List list) {
160 public void getUsedVariable(final List list) {
164 * Get global variables (not parameters).
166 public void getGlobalVariable(final List list) {
167 if (statements != null) {
168 for (int i = 0; i < statements.length; i++) {
169 statements[i].getOutsideVariable(list);
174 private void getParameters(final List list) {
175 if (arguments != null) {
176 for (int i = 0; i < arguments.size(); i++) {
177 VariableDeclaration variable = (VariableDeclaration) arguments.get(i);
178 list.add(new VariableUsage(variable.name(), variable.sourceStart));
184 * get the modified variables.
186 private void getAssignedVariableInCode(final List list) {
187 if (statements != null) {
188 for (int i = 0; i < statements.length; i++) {
189 statements[i].getModifiedVariable(list);
195 * Get the variables used.
197 private void getUsedVariableInCode(final List list) {
198 if (statements != null) {
199 for (int i = 0; i < statements.length; i++) {
200 statements[i].getUsedVariable(list);
205 private boolean isVariableDeclaredBefore(final List list, final VariableUsage var) {
206 final String name = var.getName();
207 final int pos = var.getStartOffset();
208 for (int i = 0; i < list.size(); i++) {
209 final VariableUsage variableUsage = (VariableUsage) list.get(i);
210 if (variableUsage.getName().equals(name) && variableUsage.getStartOffset() < pos) {
217 /** This method will analyze the code. */
218 public void analyzeCode() {
219 if (statements != null) {
220 for (int i = 0; i < statements.length; i++) {
221 statements[i].analyzeCode();
226 final List globalsVars = new ArrayList();
227 getGlobalVariable(globalsVars);
228 final List modifiedVars = new ArrayList();
229 getAssignedVariableInCode(modifiedVars);
230 final List parameters = new ArrayList();
231 getParameters(parameters);
233 final List declaredVars = new ArrayList(globalsVars.size() + modifiedVars.size());
234 declaredVars.addAll(globalsVars);
235 declaredVars.addAll(modifiedVars);
236 declaredVars.addAll(parameters);
238 final List usedVars = new ArrayList();
239 getUsedVariableInCode(usedVars);
240 final List readOrWriteVars = new ArrayList(modifiedVars.size() + usedVars.size());
241 readOrWriteVars.addAll(modifiedVars);
242 readOrWriteVars.addAll(usedVars);
244 //look for used variables that were not declared before
245 findUnusedParameters(readOrWriteVars, parameters);
246 findUnknownUsedVars(usedVars, declaredVars);
250 * This method will add a warning on all unused parameters.
251 * @param vars the used variable list
252 * @param parameters the declared variable list
254 private void findUnusedParameters(final List vars, final List parameters) {
255 for (int i = 0; i < parameters.size(); i++) {
256 final VariableUsage param = ((VariableUsage) parameters.get(i));
257 if (!isVariableInList(param.getName(), vars)) {
259 PHPParserSuperclass.setMarker(
260 "warning, the parameter " + param.getName() + " seems to be never used in your method",
261 param.getStartOffset(),
262 param.getStartOffset() + param.getName().length(),
263 PHPParserSuperclass.WARNING,
265 } catch (CoreException e) {
266 PHPeclipsePlugin.log(e);
272 private boolean isVariableInList(final String name, final List list) {
273 for (int i = 0; i < list.size(); i++) {
274 if (((VariableUsage) list.get(i)).getName().equals(name)) {
282 * This method will add a warning on all used variables in a method that aren't declared before.
283 * @param usedVars the used variable list
284 * @param declaredVars the declared variable list
286 private void findUnknownUsedVars(final List usedVars, final List declaredVars) {
287 for (int i = 0; i < usedVars.size(); i++) {
288 final VariableUsage variableUsage = (VariableUsage) usedVars.get(i);
289 if (variableUsage.getName().equals("this")) continue; // this is a special variable
290 if (!isVariableDeclaredBefore(declaredVars, variableUsage)) {
292 PHPParserSuperclass.setMarker(
293 "warning, usage of a variable that seems to be unassigned yet : " + variableUsage.getName(),
294 variableUsage.getStartOffset(),
295 variableUsage.getStartOffset() + variableUsage.getName().length(),
296 PHPParserSuperclass.WARNING,
298 } catch (CoreException e) {
299 PHPeclipsePlugin.log(e);