3.x RC1 compatibility
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / MethodDeclaration.java
index ad18c19..f1220ab 100644 (file)
@@ -1,45 +1,46 @@
 package net.sourceforge.phpdt.internal.compiler.ast;
 
-import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
-import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
 import net.sourceforge.phpdt.internal.compiler.ast.declarations.VariableUsage;
+import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
+import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.text.Position;
-import org.eclipse.core.runtime.CoreException;
+import test.PHPParserSuperclass;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
 
-import test.PHPParserSuperclass;
-
 /**
  * A Method declaration.
+ * 
  * @author Matthieu Casanova
  */
-public class MethodDeclaration extends Statement implements OutlineableWithChildren {
+public final class MethodDeclaration extends Statement implements OutlineableWithChildren {
 
   /** The name of the method. */
-  public String name;
-  public ArrayList arguments;
+  public final String name;
+  private final ArrayList arguments;
 
 
   public Statement[] statements;
-  public int bodyStart;
-  public int bodyEnd = -1;
+  private final int bodyStart;
+  private int bodyEnd = -1;
   /** Tell if the method is a class constructor. */
   public boolean isConstructor;
 
   /** The parent object. */
   private Object parent;
   /** The outlineable children (those will be in the node array too. */
-  private ArrayList children = new ArrayList();
+  private final ArrayList children = new ArrayList();
 
   /** Tell if the method returns a reference. */
-  public boolean reference;
+  private final boolean reference;
 
-  private Position position;
+  private final Position position;
 
   public MethodDeclaration(final Object parent,
                            final String name,
@@ -61,6 +62,7 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
 
   /**
    * Return method into String, with a number of tabs
+   * 
    * @param tab the number of tabs
    * @return the String containing the method
    */
@@ -71,16 +73,17 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
     return buff.toString();
   }
 
-  public String toStringHeader() {
+  private String toStringHeader() {
     return "function " + toString();
   }
 
   /**
    * Return the statements of the method into Strings
+   * 
    * @param tab the number of tabs
    * @return the String containing the statements
    */
-  public String toStringStatements(final int tab) {
+  private String toStringStatements(final int tab) {
     final StringBuffer buff = new StringBuffer(" {"); //$NON-NLS-1$
     if (statements != null) {
       for (int i = 0; i < statements.length; i++) {
@@ -96,6 +99,7 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
 
   /**
    * Get the image of a class.
+   * 
    * @return the image that represents a php class
    */
   public ImageDescriptor getImage() {
@@ -131,7 +135,7 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
 
     if (arguments != null) {
       for (int i = 0; i < arguments.size(); i++) {
-        VariableDeclaration o =  (VariableDeclaration) arguments.get(i);
+        final VariableDeclaration o = (VariableDeclaration) arguments.get(i);
         buff.append(o.toStringExpression());
         if (i != (arguments.size() - 1)) {
           buff.append(", "); //$NON-NLS-1$
@@ -150,20 +154,31 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
     return children;
   }
 
-  /** no outside variables. */
-  public void getOutsideVariable(final List list) {
-  }
+  /**
+   * Get the variables from outside (parameters, globals ...)
+   * 
+   * @param list the list where we will put variables
+   */
+  public void getOutsideVariable(final List list) {}
 
-  public void getModifiedVariable(final List list) {
-  }
+  /**
+   * get the modified variables.
+   * 
+   * @param list the list where we will put variables
+   */
+  public void getModifiedVariable(final List list) {}
 
-  public void getUsedVariable(final List list) {
-  }
+  /**
+   * This method will analyze the code.
+   * 
+   * @param list the list where we will put variables
+   */
+  public void getUsedVariable(final List list) {}
 
   /**
    * Get global variables (not parameters).
    */
-  public void getGlobalVariable(final List list) {
+  private void getGlobalVariable(final List list) {
     if (statements != null) {
       for (int i = 0; i < statements.length; i++) {
         statements[i].getOutsideVariable(list);
@@ -174,7 +189,7 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
   private void getParameters(final List list) {
     if (arguments != null) {
       for (int i = 0; i < arguments.size(); i++) {
-        VariableDeclaration variable = (VariableDeclaration) arguments.get(i);
+        final VariableDeclaration variable = (VariableDeclaration) arguments.get(i);
         list.add(new VariableUsage(variable.name(), variable.sourceStart));
       }
     }
@@ -202,7 +217,7 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
     }
   }
 
-  private boolean isVariableDeclaredBefore(final List list, final VariableUsage var) {
+  private static boolean isVariableDeclaredBefore(final List list, final VariableUsage var) {
     final String name = var.getName();
     final int pos = var.getStartOffset();
     for (int i = 0; i < list.size(); i++) {
@@ -214,7 +229,9 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
     return false;
   }
 
-  /** This method will analyze the code. */
+  /**
+   * This method will analyze the code.
+   */
   public void analyzeCode() {
     if (statements != null) {
       for (int i = 0; i < statements.length; i++) {
@@ -227,7 +244,7 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
     getGlobalVariable(globalsVars);
     final List modifiedVars = new ArrayList();
     getAssignedVariableInCode(modifiedVars);
-    final List parameters = new ArrayList();
+    final List parameters = new ArrayList(arguments.size());
     getParameters(parameters);
 
     final List declaredVars = new ArrayList(globalsVars.size() + modifiedVars.size());
@@ -248,12 +265,13 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
 
   /**
    * This method will add a warning on all unused parameters.
-   * @param vars the used variable list
+   * 
+   * @param vars       the used variable list
    * @param parameters the declared variable list
    */
-  private void findUnusedParameters(final List vars, final List parameters) {
+  private static void findUnusedParameters(final List vars, final List parameters) {
     for (int i = 0; i < parameters.size(); i++) {
-      final VariableUsage param = ((VariableUsage) parameters.get(i));
+      final VariableUsage param = (VariableUsage) parameters.get(i);
       if (!isVariableInList(param.getName(), vars)) {
         try {
           PHPParserSuperclass.setMarker(
@@ -269,7 +287,14 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
     }
   }
 
-  private boolean isVariableInList(final String name, final List list) {
+  /**
+   * Tell if the list of VariableUsage contains a variable named by the name given.
+   * 
+   * @param name the variable name
+   * @param list the list of VariableUsage
+   * @return true if the variable is in the list false otherwise
+   */
+  private static boolean isVariableInList(final String name, final List list) {
     for (int i = 0; i < list.size(); i++) {
       if (((VariableUsage) list.get(i)).getName().equals(name)) {
         return true;
@@ -280,14 +305,17 @@ public class MethodDeclaration extends Statement implements OutlineableWithChild
 
   /**
    * This method will add a warning on all used variables in a method that aren't declared before.
-   * @param usedVars the used variable list
+   * 
+   * @param usedVars     the used variable list
    * @param declaredVars the declared variable list
    */
-  private void findUnknownUsedVars(final List usedVars, final List declaredVars) {
+  private static void findUnknownUsedVars(final List usedVars, final List declaredVars) {
+    final HashSet list = new HashSet(usedVars.size());
     for (int i = 0; i < usedVars.size(); i++) {
       final VariableUsage variableUsage = (VariableUsage) usedVars.get(i);
-      if (variableUsage.getName().equals("this")) continue; // this is a special variable
-      if (!isVariableDeclaredBefore(declaredVars, variableUsage)) {
+      if ("this".equals(variableUsage.getName())) continue; // this is a special variable
+      if (!list.contains(variableUsage.getName()) && !isVariableDeclaredBefore(declaredVars, variableUsage)) {
+        list.add(variableUsage.getName());
         try {
           PHPParserSuperclass.setMarker(
                   "warning, usage of a variable that seems to be unassigned yet : " + variableUsage.getName(),