protected void getGoalFromStaticDeclaration(
      String variableName, final List<IGoal> subGoals, final IType declaringType, IType realType)
      throws ModelException {
    ISourceModule sourceModule = declaringType.getSourceModule();
    ModuleDeclaration moduleDeclaration = SourceParserUtil.getModuleDeclaration(sourceModule);
    TypeDeclaration typeDeclaration =
        PHPModelUtils.getNodeByClass(moduleDeclaration, declaringType);

    // try to search declarations of type "self::$var =" or
    // "$this->var ="
    ClassDeclarationSearcher searcher;
    if (realType != null) {
      searcher =
          new ClassDeclarationSearcher(
              sourceModule, typeDeclaration, 0, 0, variableName, realType, declaringType);
    } else {
      searcher = new ClassDeclarationSearcher(sourceModule, typeDeclaration, 0, 0, variableName);
    }
    try {
      moduleDeclaration.traverse(searcher);
      Map<ASTNode, IContext> staticDeclarations = searcher.getStaticDeclarations();
      for (ASTNode node : staticDeclarations.keySet()) {
        IContext context = staticDeclarations.get(node);
        if (context instanceof MethodContext) {
          MethodContext methodContext = (MethodContext) context;
          methodContext.setCurrentType(realType);
        }
        subGoals.add(new ExpressionTypeGoal(context, node));
      }
    } catch (Exception e) {
      if (DLTKCore.DEBUG) {
        e.printStackTrace();
      }
    }
  }
Example #2
0
  protected void addAlias(ICompletionReporter reporter, String suffix) throws BadLocationException {
    if (aliasAdded) {
      return;
    }
    aliasAdded = true;
    ICompletionContext context = getContext();
    AbstractCompletionContext abstractContext = (AbstractCompletionContext) context;
    if (!abstractContext.getCompletionRequestor().isContextInformationMode()) {
      // get types for alias
      String prefix = abstractContext.getPrefixWithoutProcessing();
      boolean exactMatch = false;
      if (prefix.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) == 0) {
        return;
      } else if (prefix.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) > 0) {
        prefix = prefix.substring(0, prefix.indexOf(NamespaceReference.NAMESPACE_SEPARATOR));
        exactMatch = true;
      } else {

      }

      if (prefix.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) < 0) {
        IModuleSource module = reporter.getModule();
        org.eclipse.dltk.core.ISourceModule sourceModule =
            (org.eclipse.dltk.core.ISourceModule) module.getModelElement();
        ModuleDeclaration moduleDeclaration = SourceParserUtil.getModuleDeclaration(sourceModule);
        final int offset = abstractContext.getOffset();
        IType namespace = PHPModelUtils.getCurrentNamespace(sourceModule, offset);

        final Map<String, UsePart> result =
            PHPModelUtils.getAliasToNSMap(prefix, moduleDeclaration, offset, namespace, exactMatch);
        reportAlias(reporter, suffix, abstractContext, module, result);
      }
    }
  }
  public IGoal[] init() {
    ClassVariableDeclarationGoal typedGoal = (ClassVariableDeclarationGoal) goal;
    IType[] types = typedGoal.getTypes();

    if (types == null) {
      TypeContext context = (TypeContext) typedGoal.getContext();
      types = PHPTypeInferenceUtils.getModelElements(context.getInstanceType(), context);
    }
    if (types == null) {
      return null;
    }

    IContext context = typedGoal.getContext();
    IModelAccessCache cache = null;
    if (context instanceof IModelCacheContext) {
      cache = ((IModelCacheContext) context).getCache();
    }

    String variableName = typedGoal.getVariableName();

    final List<IGoal> subGoals = new LinkedList<IGoal>();
    for (final IType type : types) {
      try {
        ITypeHierarchy hierarchy = null;
        if (cache != null) {
          hierarchy = cache.getSuperTypeHierarchy(type, null);
        }
        IField[] fields =
            PHPModelUtils.getTypeHierarchyField(type, hierarchy, variableName, true, null);
        Map<IType, IType> fieldDeclaringTypeSet = new HashMap<IType, IType>();
        for (IField field : fields) {
          IType declaringType = field.getDeclaringType();
          if (declaringType != null) {
            fieldDeclaringTypeSet.put(declaringType, type);
            ISourceModule sourceModule = declaringType.getSourceModule();
            ModuleDeclaration moduleDeclaration =
                SourceParserUtil.getModuleDeclaration(sourceModule);
            TypeDeclaration typeDeclaration =
                PHPModelUtils.getNodeByClass(moduleDeclaration, declaringType);

            if (typeDeclaration != null && field instanceof SourceRefElement) {
              SourceRefElement sourceRefElement = (SourceRefElement) field;
              ISourceRange sourceRange = sourceRefElement.getSourceRange();

              ClassDeclarationSearcher searcher =
                  new ClassDeclarationSearcher(
                      sourceModule,
                      typeDeclaration,
                      sourceRange.getOffset(),
                      sourceRange.getLength(),
                      null,
                      type,
                      declaringType);
              try {
                moduleDeclaration.traverse(searcher);
                if (searcher.getResult() != null) {
                  subGoals.add(new ExpressionTypeGoal(searcher.getContext(), searcher.getResult()));
                }
              } catch (Exception e) {
                if (DLTKCore.DEBUG) {
                  e.printStackTrace();
                }
              }
            }
          }
        }

        if (subGoals.size() == 0) {
          getGoalFromStaticDeclaration(variableName, subGoals, type, null);
        }
        fieldDeclaringTypeSet.remove(type);
        if (subGoals.size() == 0 && !fieldDeclaringTypeSet.isEmpty()) {
          for (Iterator iterator = fieldDeclaringTypeSet.keySet().iterator();
              iterator.hasNext(); ) {
            IType fieldDeclaringType = (IType) iterator.next();
            getGoalFromStaticDeclaration(
                variableName,
                subGoals,
                fieldDeclaringType,
                fieldDeclaringTypeSet.get(fieldDeclaringType));
          }
        }
      } catch (CoreException e) {
        if (DLTKCore.DEBUG) {
          e.printStackTrace();
        }
      }
    }

    resolveMagicClassVariableDeclaration(types, variableName, cache);

    return subGoals.toArray(new IGoal[subGoals.size()]);
  }