private String processNameNode(ASTNode nameNode) {
    if (nameNode instanceof FullyQualifiedReference) {
      String name;
      FullyQualifiedReference fullyQualifiedName = (FullyQualifiedReference) nameNode;
      name = fullyQualifiedName.getFullyQualifiedName();
      if (fullyQualifiedName.getNamespace() != null) {
        String namespace = fullyQualifiedName.getNamespace().getName();

        String subnamespace = ""; // $NON-NLS-1$
        if (namespace.charAt(0) != NamespaceReference.NAMESPACE_SEPARATOR
            && namespace.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) > 0) {
          int firstNSLocation = namespace.indexOf(NamespaceReference.NAMESPACE_SEPARATOR);
          subnamespace = namespace.substring(firstNSLocation);
          namespace = namespace.substring(0, firstNSLocation);
        }
        if (name.charAt(0) == NamespaceReference.NAMESPACE_SEPARATOR) {
          name = name.substring(1);
        } else if (fLastUseParts.containsKey(namespace)) {
          name =
              new StringBuilder(fLastUseParts.get(namespace).getNamespace().getFullyQualifiedName())
                  .append(subnamespace)
                  .append(NamespaceReference.NAMESPACE_SEPARATOR)
                  .append(fullyQualifiedName.getName())
                  .toString();
        } else if (fLastNamespace != null) {
          name =
              new StringBuilder(fLastNamespace.getName())
                  .append(NamespaceReference.NAMESPACE_SEPARATOR)
                  .append(name)
                  .toString();
        }
      } else if (fLastUseParts.containsKey(name)) {
        name = fLastUseParts.get(name).getNamespace().getFullyQualifiedName();
        if (name.charAt(0) == NamespaceReference.NAMESPACE_SEPARATOR) {
          name = name.substring(1);
        }
      } else {
        if (fLastNamespace != null) {
          name =
              new StringBuilder(fLastNamespace.getName())
                  .append(NamespaceReference.NAMESPACE_SEPARATOR)
                  .append(name)
                  .toString();
        }
      }
      return name;
    } else if (nameNode instanceof SimpleReference) {
      return ((SimpleReference) nameNode).getName();
    }
    return null;
  }
Example #2
0
 public boolean visit(Assignment assignment) throws Exception {
   Expression left = assignment.getVariable();
   if (left instanceof FieldAccess) { // class variable ($this->a = .)
     FieldAccess fieldAccess = (FieldAccess) left;
     Expression dispatcher = fieldAccess.getDispatcher();
     if (dispatcher instanceof VariableReference
         && "$this".equals(((VariableReference) dispatcher).getName())) { // $NON-NLS-1$
       Expression field = fieldAccess.getField();
       if (field instanceof SimpleReference) {
         SimpleReference var = (SimpleReference) field;
         int modifiers = Modifiers.AccPublic;
         int offset = var.sourceStart();
         int length = var.sourceEnd() - offset;
         StringBuilder metadata = new StringBuilder();
         if (fCurrentQualifier != null) {
           metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier));
           metadata.append(";"); // $NON-NLS-1$
         }
         modifyDeclaration(
             assignment,
             new DeclarationInfo(
                 IModelElement.FIELD,
                 modifiers,
                 offset,
                 length,
                 offset,
                 length,
                 '$' + var.getName(),
                 metadata.length() == 0 ? null : metadata.toString(),
                 null,
                 fCurrentQualifier,
                 fCurrentParent));
       }
     }
   } else if (left instanceof VariableReference) {
     int modifiers = Modifiers.AccPublic | Modifiers.AccGlobal;
     if (!declarations.empty()
         && declarations.peek() instanceof MethodDeclaration
         && !methodGlobalVars.peek().contains(((VariableReference) left).getName())) {
       return visitGeneral(assignment);
     }
     int offset = left.sourceStart();
     int length = left.sourceEnd() - offset;
     modifyDeclaration(
         assignment,
         new DeclarationInfo(
             IModelElement.FIELD,
             modifiers,
             offset,
             length,
             offset,
             length,
             ((VariableReference) left).getName(),
             null,
             null,
             null,
             null));
   }
   return visitGeneral(assignment);
 }
Example #3
0
 public boolean visit(ConstantDeclaration declaration) throws Exception {
   int modifiers = Modifiers.AccConstant | Modifiers.AccPublic | Modifiers.AccFinal;
   if (fCurrentParent != null) {
     modifiers = modifiers | PHPCoreConstants.AccClassField;
   }
   modifiers = markAsDeprecated(modifiers, declaration);
   ConstantReference constantName = declaration.getConstantName();
   int offset = constantName.sourceStart();
   int length = constantName.sourceEnd();
   StringBuilder metadata = new StringBuilder();
   if (fCurrentQualifier != null) {
     metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier));
     metadata.append(";"); // $NON-NLS-1$
   }
   modifyDeclaration(
       declaration,
       new DeclarationInfo(
           IModelElement.FIELD,
           modifiers,
           offset,
           length,
           offset,
           length,
           ASTUtils.stripQuotes(constantName.getName()),
           metadata.length() == 0 ? null : metadata.toString(),
           encodeDocInfo(declaration),
           fCurrentQualifier,
           fCurrentParent));
   return visitGeneral(declaration);
 }
Example #4
0
  public boolean visit(PHPFieldDeclaration decl) throws Exception {
    // This is variable declaration:
    int modifiers = markAsDeprecated(decl.getModifiers(), decl);

    StringBuilder metadata = new StringBuilder();
    if (fCurrentQualifier != null) {
      metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier));
      metadata.append(";"); // $NON-NLS-1$
    }

    modifyDeclaration(
        decl,
        new DeclarationInfo(
            IModelElement.FIELD,
            modifiers,
            decl.sourceStart(),
            decl.sourceEnd() - decl.sourceStart(),
            decl.getNameStart(),
            decl.getNameEnd() - decl.getNameStart(),
            decl.getName(),
            metadata.length() == 0 ? null : metadata.toString(),
            encodeDocInfo(decl),
            fCurrentQualifier,
            fCurrentParent));

    return visitGeneral(decl);
  }
Example #5
0
  /**
   * Resolve class members that were defined using the @property tag
   *
   * @param type declaration for wich we add the magic variables
   */
  private void resolveMagicMembers(TypeDeclaration type) {
    if (type instanceof IPHPDocAwareDeclaration) {
      IPHPDocAwareDeclaration declaration = (IPHPDocAwareDeclaration) type;
      final PHPDocBlock doc = declaration.getPHPDoc();
      if (doc != null) {
        for (PHPDocTag docTag : doc.getTags()) {
          final int tagKind = docTag.getTagKind();
          if (tagKind == PHPDocTag.PROPERTY
              || tagKind == PHPDocTag.PROPERTY_READ
              || tagKind == PHPDocTag.PROPERTY_WRITE) {
            // http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.property.pkg.html
            final String[] split = WHITESPACE_SEPERATOR.split(docTag.getValue().trim());
            if (split.length < 2) {
              break;
            }

            String name = removeParenthesis(split);
            int offset = docTag.sourceStart();
            int length = docTag.sourceStart() + 9;

            Map<String, String> info = new HashMap<String, String>();
            info.put("v", split[0]); // $NON-NLS-1$

            StringBuilder metadata = new StringBuilder();
            if (fCurrentQualifier != null) {
              metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier));
              metadata.append(";"); // $NON-NLS-1$
            }

            modifyDeclaration(
                null,
                new DeclarationInfo(
                    IModelElement.FIELD,
                    Modifiers.AccPublic,
                    offset,
                    length,
                    offset,
                    length,
                    name,
                    metadata.length() == 0 ? null : metadata.toString(),
                    encodeDocInfo(info),
                    fCurrentQualifier,
                    fCurrentParent));

          } else if (tagKind == PHPDocTag.METHOD) {
            // http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.method.pkg.html
            String[] split = WHITESPACE_SEPERATOR.split(docTag.getValue().trim());
            if (split.length < 2) {
              break;
            }
            int methodModifiers = Modifiers.AccPublic;
            if (Constants.STATIC.equals(split[0].trim())) {
              if (split.length < 3) {
                break;
              }
              methodModifiers |= Modifiers.AccStatic;
              split = Arrays.copyOfRange(split, 1, split.length);
            }

            String name = removeParenthesis(split);
            int index = name.indexOf('(');
            if (index > 0) {
              name = name.substring(0, index);
            }
            int offset = docTag.sourceStart();
            int length = docTag.sourceStart() + 6;
            Map<String, String> info = new HashMap<String, String>();
            info.put("r", split[0]); // $NON-NLS-1$

            StringBuilder metadata = new StringBuilder();
            if (fCurrentQualifier != null) {
              metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier));
              metadata.append(";"); // $NON-NLS-1$
            }

            modifyDeclaration(
                null,
                new DeclarationInfo(
                    IModelElement.METHOD,
                    methodModifiers,
                    offset,
                    length,
                    offset,
                    length,
                    name,
                    metadata.length() == 0 ? null : metadata.toString(),
                    encodeDocInfo(info),
                    fCurrentQualifier,
                    fCurrentParent));
          }
        }
      }
    }
  }
Example #6
0
 protected String[] processSuperClasses(TypeDeclaration type) {
   ASTListNode superClasses = type.getSuperClasses();
   if (superClasses == null) {
     return new String[] {};
   }
   List<ASTNode> superClassNames = superClasses.getChilds();
   List<String> result = new ArrayList<String>(superClassNames.size());
   Iterator<ASTNode> iterator = superClassNames.iterator();
   while (iterator.hasNext()) {
     ASTNode nameNode = iterator.next();
     String name;
     if (nameNode instanceof FullyQualifiedReference) {
       FullyQualifiedReference fullyQualifiedName = (FullyQualifiedReference) nameNode;
       name = fullyQualifiedName.getFullyQualifiedName();
       if (fullyQualifiedName.getNamespace() != null) {
         String namespace = fullyQualifiedName.getNamespace().getName();
         String subnamespace = ""; // $NON-NLS-1$
         if (namespace.charAt(0) != NamespaceReference.NAMESPACE_SEPARATOR
             && namespace.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) > 0) {
           int firstNSLocation = namespace.indexOf(NamespaceReference.NAMESPACE_SEPARATOR);
           subnamespace = namespace.substring(firstNSLocation);
           namespace = namespace.substring(0, firstNSLocation);
         }
         if (name.charAt(0) == NamespaceReference.NAMESPACE_SEPARATOR) {
           name = name.substring(1);
         } else if (fLastUseParts.containsKey(namespace)) {
           name =
               new StringBuilder(
                       fLastUseParts.get(namespace).getNamespace().getFullyQualifiedName())
                   .append(subnamespace)
                   .append(NamespaceReference.NAMESPACE_SEPARATOR)
                   .append(fullyQualifiedName.getName())
                   .toString();
         } else if (fCurrentNamespace != null) {
           name =
               new StringBuilder(fCurrentNamespace.getName())
                   .append(NamespaceReference.NAMESPACE_SEPARATOR)
                   .append(name)
                   .toString();
         }
       } else if (fLastUseParts.containsKey(name)) {
         name = fLastUseParts.get(name).getNamespace().getFullyQualifiedName();
         if (name.charAt(0) == NamespaceReference.NAMESPACE_SEPARATOR) {
           name = name.substring(1);
         }
       } else {
         if (fCurrentNamespace != null) {
           name =
               new StringBuilder(fCurrentNamespace.getName())
                   .append(NamespaceReference.NAMESPACE_SEPARATOR)
                   .append(name)
                   .toString();
         }
       }
       result.add(name);
     } else if (nameNode instanceof SimpleReference) {
       result.add(((SimpleReference) nameNode).getName());
     }
   }
   return (String[]) result.toArray(new String[result.size()]);
 }
Example #7
0
  public boolean visit(TypeDeclaration type) throws Exception {
    if (type instanceof NamespaceDeclaration) {
      NamespaceDeclaration namespaceDecl = (NamespaceDeclaration) type;
      fCurrentNamespace = namespaceDecl;
      fLastUseParts.clear();
      if (namespaceDecl.isGlobal()) {
        return visitGeneral(type);
      }
      declarations.push(type);

      int modifiers = type.getModifiers() | Modifiers.AccNameSpace;
      fCurrentQualifier = type.getName();
      Integer count = fCurrentQualifierCounts.get(fCurrentQualifier);
      count = count != null ? count + 1 : 1;
      fCurrentQualifierCounts.put(fCurrentQualifier, count);

      modifiers = markAsDeprecated(modifiers, type);
      StringBuilder metadata = new StringBuilder();
      if (fCurrentQualifier != null) {
        metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier));
        metadata.append(";"); // $NON-NLS-1$
      }
      modifyDeclaration(
          type,
          new DeclarationInfo(
              IModelElement.PACKAGE_DECLARATION,
              modifiers,
              type.sourceStart(),
              type.sourceEnd() - type.sourceStart(),
              type.getNameStart(),
              type.getNameEnd() - type.getNameStart(),
              type.getName(),
              metadata.length() == 0 ? null : metadata.toString(),
              encodeDocInfo(type),
              null,
              null));
    } else {
      Declaration parentDeclaration = null;
      if (!declarations.empty()) {
        parentDeclaration = declarations.peek();
      }
      declarations.push(type);

      if (!(parentDeclaration instanceof NamespaceDeclaration)) {
        type.setModifier(Modifiers.AccGlobal);
      }

      // In case we are entering a nested element
      if (parentDeclaration instanceof MethodDeclaration) {
        if (fCurrentNamespace == null) {
          deferredDeclarations.add(type);
        } else {
          deferredNamespacedDeclarations.add(type);
        }
        return visitGeneral(type);
      }

      int modifiers = type.getModifiers();
      fCurrentParent = type.getName();

      String[] superClasses = processSuperClasses(type);
      StringBuilder metadata = new StringBuilder();
      if (fCurrentQualifier != null) {
        metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier));
        metadata.append(";"); // $NON-NLS-1$
      }
      for (int i = 0; i < superClasses.length; ++i) {
        metadata.append(superClasses[i]);
        if (i < superClasses.length - 1) {
          metadata.append(","); // $NON-NLS-1$
        }
      }
      modifiers = markAsDeprecated(modifiers, type);
      modifyDeclaration(
          type,
          new DeclarationInfo(
              IModelElement.TYPE,
              modifiers,
              type.sourceStart(),
              type.sourceEnd() - type.sourceStart(),
              type.getNameStart(),
              type.getNameEnd() - type.getNameStart(),
              type.getName(),
              metadata.length() == 0 ? null : metadata.toString(),
              encodeDocInfo(type),
              fCurrentQualifier,
              null));
    }

    for (PhpIndexingVisitorExtension visitor : extensions) {
      visitor.visit(type);
    }

    return visitGeneral(type);
  }
Example #8
0
  @SuppressWarnings("unchecked")
  public boolean visit(MethodDeclaration method) throws Exception {
    fNodes.push(method);
    methodGlobalVars.add(new HashSet<String>());
    int modifiers = method.getModifiers();
    PHPDocBlock doc = null;
    if (method instanceof IPHPDocAwareDeclaration) {
      IPHPDocAwareDeclaration declaration = (IPHPDocAwareDeclaration) method;
      doc = declaration.getPHPDoc();
    }
    Declaration parentDeclaration = null;
    if (!declarations.empty()) {
      parentDeclaration = declarations.peek();
    }
    declarations.push(method);

    // In case we are entering a nested element - just add to the deferred
    // list
    // and get out of the nested element visiting process
    if (parentDeclaration instanceof MethodDeclaration) {
      if (fCurrentNamespace == null) {
        deferredDeclarations.add(method);
      } else {
        deferredNamespacedDeclarations.add(method);
      }
      return visitGeneral(method);
    }

    if (parentDeclaration instanceof InterfaceDeclaration) {
      method.setModifier(Modifiers.AccAbstract);
    }

    String methodName = method.getName();

    // Determine whether this method represents constructor:
    if (methodName.equalsIgnoreCase(CONSTRUCTOR_NAME)
        || (parentDeclaration instanceof ClassDeclaration
            && methodName.equalsIgnoreCase(((ClassDeclaration) parentDeclaration).getName()))) {
      modifiers |= IPHPModifiers.Constructor;
    }

    if (parentDeclaration == null
        || (parentDeclaration instanceof TypeDeclaration
            && parentDeclaration == fCurrentNamespace)) {
      modifiers |= Modifiers.AccGlobal;
    }
    if (!Flags.isPrivate(modifiers)
        && !Flags.isProtected(modifiers)
        && !Flags.isPublic(modifiers)) {
      modifiers |= Modifiers.AccPublic;
    }

    modifiers = markAsDeprecated(modifiers, method);

    StringBuilder metadata = new StringBuilder();
    if (fCurrentQualifier != null) {
      metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier));
      metadata.append(";"); // $NON-NLS-1$
    }
    List<Argument> arguments = method.getArguments();
    if (arguments != null) {
      Iterator<Argument> i = arguments.iterator();
      while (i.hasNext()) {
        Argument arg = (Argument) i.next();

        String type = NULL_VALUE;
        if (arg instanceof FormalParameter) {
          FormalParameter fp = (FormalParameter) arg;
          if (fp.getParameterType() != null) {
            if (fp.getParameterType().getName() != null) {
              type = fp.getParameterType().getName();
            }
          }
        }
        if (type == NULL_VALUE && doc != null) {
          type = getParamType(doc, arg.getName(), type);
        }

        metadata.append(type);
        metadata.append(PARAMETER_SEPERATOR);
        metadata.append(arg.getName());
        metadata.append(PARAMETER_SEPERATOR);
        String defaultValue = NULL_VALUE;
        if (arg.getInitialization() != null) {
          if (arg.getInitialization() instanceof Literal) {
            Literal scalar = (Literal) arg.getInitialization();
            defaultValue = scalar.getValue();
          } else {
            defaultValue = DEFAULT_VALUE;
          }
        }
        metadata.append(defaultValue);
        if (i.hasNext()) {
          metadata.append(","); // $NON-NLS-1$
        }
      }
    }

    // Add method declaration:
    modifyDeclaration(
        method,
        new DeclarationInfo(
            IModelElement.METHOD,
            modifiers,
            method.sourceStart(),
            method.sourceEnd() - method.sourceStart(),
            method.getNameStart(),
            method.getNameEnd() - method.getNameStart(),
            methodName,
            metadata.length() == 0 ? null : metadata.toString(),
            encodeDocInfo(method),
            fCurrentQualifier,
            fCurrentParent));

    for (PhpIndexingVisitorExtension visitor : extensions) {
      visitor.visit(method);
    }

    return visitGeneral(method);
  }