/**
   * Search for magic variables using the @property tag
   *
   * @param types
   * @param variableName
   * @param cache
   */
  private void resolveMagicClassVariableDeclaration(
      IType[] types, String variableName, IModelAccessCache cache) {
    for (IType type : types) {
      resolveMagicClassVariableDeclaration(variableName, type, cache);
      try {
        if (evaluated.isEmpty()
            && type.getSuperClasses() != null
            && type.getSuperClasses().length > 0) {

          ITypeHierarchy hierarchy = null;
          if (cache != null) {
            hierarchy = cache.getSuperTypeHierarchy(type, null);
          }
          IType[] superClasses = PHPModelUtils.getSuperClasses(type, hierarchy);

          for (int i = 0; i < superClasses.length /* && evaluated.isEmpty() */; i++) {
            IType superClass = superClasses[i];
            resolveMagicClassVariableDeclaration(variableName, superClass, cache);
          }
        }
      } catch (ModelException e) {
        e.printStackTrace();
      }
    }
  }
  private void initRelativeNamespace(ISourceModule sourceModule, int offset, String lastWord) {
    String nsName = lastWord;
    String fullName = lastWord;
    nsPrefix = null;
    if (lastWord.lastIndexOf(NamespaceReference.NAMESPACE_SEPARATOR) > 0) {
      nsPrefix =
          lastWord.substring(0, lastWord.lastIndexOf(NamespaceReference.NAMESPACE_SEPARATOR));
      nsName = nsName.substring(0, nsName.lastIndexOf(NamespaceReference.NAMESPACE_SEPARATOR) + 1);

      try {
        namespaces = PHPModelUtils.getNamespaceOf(nsName, sourceModule, offset, null, null);
      } catch (ModelException e) {
        if (DLTKCore.DEBUG) {
          e.printStackTrace();
        }
      }
    } else {
      namespaces = PhpModelAccess.NULL_TYPES;
    }
    if (lastWord.startsWith(NamespaceReference.NAMESPACE_DELIMITER)) {
      nsPrefix = null;
    } else {

      currentNS = null;
      try {
        IModelElement enclosingElement = getEnclosingElement();
        if (enclosingElement != null) {
          IType type = (IType) enclosingElement.getAncestor(IModelElement.TYPE);
          if (type != null && type.getParent() instanceof IType) {
            type = (IType) type.getParent();
          }
          if (type != null && (PHPFlags.isNamespace(type.getFlags()))) {
            currentNS = type;
            fullName =
                NamespaceReference.NAMESPACE_SEPARATOR
                    + currentNS.getElementName()
                    + NamespaceReference.NAMESPACE_SEPARATOR
                    + lastWord;
          } else {

          }
        }
      } catch (ModelException e1) {
        e1.printStackTrace();
      }
      if (currentNS != null) {
        if (nsPrefix == null) {
          nsPrefix = currentNS.getElementName();
        } else {
          nsPrefix = currentNS.getElementName() + NamespaceReference.NAMESPACE_SEPARATOR + nsPrefix;
        }
      }
    }

    IDLTKSearchScope scope = SearchEngine.createSearchScope(sourceModule.getScriptProject());
    if (fullName.startsWith(NamespaceReference.NAMESPACE_DELIMITER)) {
      fullName = fullName.substring(1);
    }
    possibleNamespaces =
        PhpModelAccess.getDefault()
            .findNamespaces(null, fullName, MatchRule.PREFIX, 0, 0, scope, null);
  }
  public IGoal[] init() {
    PHPDocClassVariableGoal typedGoal = (PHPDocClassVariableGoal) goal;
    TypeContext context = (TypeContext) typedGoal.getContext();
    String variableName = typedGoal.getVariableName();
    int offset = typedGoal.getOffset();

    IModelAccessCache cache = context.getCache();
    IType[] types =
        PHPTypeInferenceUtils.getModelElements(context.getInstanceType(), context, offset, cache);
    Map<PHPDocBlock, IField> docs = new HashMap<PHPDocBlock, IField>();
    // remove array index from field name
    if (variableName.endsWith("]")) { // $NON-NLS-1$
      int index = variableName.indexOf("["); // $NON-NLS-1$
      if (index != -1) {
        variableName = variableName.substring(0, index);
      }
    }
    if (types != null) {
      for (IType type : types) {
        try {
          // we look in whole hiearchy
          ITypeHierarchy superHierarchy;
          if (cache != null) {
            superHierarchy = cache.getSuperTypeHierarchy(type, null);
          } else {
            superHierarchy = type.newSupertypeHierarchy(null);
          }
          IType[] superTypes = superHierarchy.getAllTypes();
          for (IType superType : superTypes) {
            IField[] typeField = PHPModelUtils.getTypeField(superType, variableName, true);
            if (typeField.length > 0) {
              PHPDocBlock docBlock = PHPModelUtils.getDocBlock(typeField[0]);
              if (docBlock != null) {
                docs.put(docBlock, typeField[0]);
              }
            }
          }
        } catch (ModelException e) {
          if (DLTKCore.DEBUG) {
            e.printStackTrace();
          }
        }
      }
    }

    for (Entry<PHPDocBlock, IField> entry : docs.entrySet()) {
      PHPDocBlock doc = entry.getKey();
      IField typeField = entry.getValue();
      IType currentNamespace = PHPModelUtils.getCurrentNamespace(typeField);

      IModelElement space =
          currentNamespace != null ? currentNamespace : typeField.getSourceModule();

      for (PHPDocTag tag : doc.getTags(PHPDocTag.VAR)) {
        // do it like for
        // PHPDocumentationContentAccess#handleBlockTags(List tags):
        // variable name can be optional, but if present keep only
        // the good ones
        if (tag.getVariableReference() != null
            && !tag.getVariableReference().getName().equals(variableName)) {
          continue;
        }

        evaluated.addAll(
            Arrays.asList(
                PHPEvaluationUtils.evaluatePHPDocType(
                    tag.getTypeReferences(), space, tag.sourceStart(), null)));
      }
    }

    return IGoal.NO_GOALS;
  }
Exemple #4
0
  protected void reportAlias(
      ICompletionReporter reporter,
      String suffix,
      AbstractCompletionContext abstractContext,
      IModuleSource module,
      final Map<String, UsePart> result)
      throws BadLocationException {
    SourceRange replacementRange = getReplacementRange(abstractContext);
    String prefix = abstractContext.getPrefixWithoutProcessing();
    IDLTKSearchScope scope = createSearchScope();
    for (Iterator iterator = result.keySet().iterator(); iterator.hasNext(); ) {
      String name = (String) iterator.next();
      String fullName = result.get(name).getNamespace().getFullyQualifiedName();
      if (fullName.startsWith("\\")) {
        fullName = fullName.substring(1);
      }
      IType[] elements =
          PhpModelAccess.getDefault()
              .findTypes(null, fullName, MatchRule.PREFIX, 0, 0, scope, null);
      try {
        for (int i = 0; i < elements.length; i++) {
          String elementName = elements[i].getElementName();
          if (!PHPFlags.isNamespace(elements[i].getFlags())) {
            reportAlias(
                reporter,
                scope,
                module,
                replacementRange,
                elements[i],
                elementName,
                elementName.replace(fullName, name),
                suffix);
          } else {
            String nsname = prefix.replace(name, fullName);
            if (nsname.startsWith(elementName + SPLASH)
                && nsname.lastIndexOf(SPLASH) == elementName.length()) {
              // namespace strategy will handle this case
              continue;
            }
            IType[] typesOfNS = elements[i].getTypes();

            for (int j = 0; j < typesOfNS.length; j++) {
              reportAlias(
                  reporter,
                  scope,
                  module,
                  replacementRange,
                  typesOfNS[j],
                  elementName + SPLASH + typesOfNS[j].getElementName(),
                  (elementName + SPLASH + typesOfNS[j].getElementName()).replace(fullName, name),
                  suffix);
            }
          }
        }

        elements =
            PhpModelAccess.getDefault().findTypes(fullName, MatchRule.EXACT, 0, 0, scope, null);

        for (int i = 0; i < elements.length; i++) {
          String elementName = elements[i].getElementName();
          if (!PHPFlags.isNamespace(elements[i].getFlags())) {
            reportAlias(
                reporter, scope, module, replacementRange, elements[i], elementName, name, suffix);
          } else {
            String nsname = prefix.replace(name, fullName);
            if (nsname.startsWith(elementName + SPLASH)
                && nsname.lastIndexOf(SPLASH) == elementName.length()) {
              // namespace strategy will handle this case
              continue;
            }
            IType[] typesOfNS = elements[i].getTypes();

            for (int j = 0; j < typesOfNS.length; j++) {
              reportAlias(
                  reporter,
                  scope,
                  module,
                  replacementRange,
                  typesOfNS[j],
                  elementName + SPLASH + typesOfNS[j].getElementName(),
                  (elementName + SPLASH + typesOfNS[j].getElementName()).replace(fullName, name),
                  suffix);
            }
          }
        }
      } catch (ModelException e) {
        e.printStackTrace();
      }
    }
  }