/**
   * Gets all the values of the entries that include the module specified.
   *
   * @param index - index to use.
   * @param module - module.
   * @return map from module to the list of entries defined in that module
   */
  public static Map<IModule, List<IncludePHPEntryValue>> getIncludes(
      IElementsIndex index, IModule module) {
    Map<IModule, List<IncludePHPEntryValue>> candidates =
        new HashMap<IModule, List<IncludePHPEntryValue>>();

    String moduleShortName = module.getShortName();

    // collecting initial candidates
    Set<IModule> modules = index.getModules();
    for (IModule currentModule : modules) {
      List<IElementEntry> moduleEntries = index.getModuleEntries(currentModule);
      for (IElementEntry entry : moduleEntries) {
        Object entryValue = entry.getValue();
        if (entryValue instanceof IncludePHPEntryValue) {
          IncludePHPEntryValue includeValue = (IncludePHPEntryValue) entryValue;
          if (includeValue.getIncludePath().endsWith(moduleShortName)) {
            List<IncludePHPEntryValue> moduleValues = candidates.get(currentModule);
            if (moduleValues == null) {
              moduleValues = new ArrayList<IncludePHPEntryValue>(1);
              candidates.put(currentModule, moduleValues);
            }

            moduleValues.add(includeValue);
          }
        }
      }
    }

    Map<IModule, List<IncludePHPEntryValue>> result =
        new HashMap<IModule, List<IncludePHPEntryValue>>();

    // filtering candidates
    for (Entry<IModule, List<IncludePHPEntryValue>> entry : candidates.entrySet()) {
      IModule currentModule = entry.getKey();
      List<IncludePHPEntryValue> values = entry.getValue();
      for (IncludePHPEntryValue value : values) {
        String includePathString = value.getIncludePath();
        IPath includePath = new Path(includePathString);
        IModule resolvedModule =
            currentModule.getBuildPath().resolveRelativePath(currentModule, includePath);
        if (resolvedModule != null && resolvedModule.equals(module)) {
          List<IncludePHPEntryValue> moduleValues = result.get(currentModule);
          if (moduleValues == null) {
            moduleValues = new ArrayList<IncludePHPEntryValue>(1);
            result.put(currentModule, moduleValues);
          }

          moduleValues.add(value);
        }
      }
    }

    return result;
  }
  /**
   * Constructs include path from one module to another.
   *
   * @param from - module to construct include path from.
   * @param toBuildPath - destination build-path.
   * @param toPath - path inside destination build-path.
   * @return constructed include path
   */
  public static ConstructedIncludePath constructIncludePath(
      IModule from, IBuildPath toBuildPath, IPath toPath) {
    IBuildPath fromBuildPath = from.getBuildPath();
    Set<IBuildPath> fromDependencies = fromBuildPath.getDependencies();

    // if "from" build-path directly depends from "to" build-path
    if (fromBuildPath.equals(toBuildPath) || fromDependencies.contains(toBuildPath)) {
      String includePath = constructPathFromRoot(toPath);
      return new ConstructedIncludePath(includePath, null, null);
    } else {
      // for local modules using its project-based build-path instead of native module build-path
      if (toBuildPath instanceof ProjectBuildPath
          || toBuildPath instanceof WorkspaceFolderBuildpath) {
        IProject project = null;
        if (toBuildPath instanceof ProjectBuildPath) {
          project = ((ProjectBuildPath) toBuildPath).getProject();
        } else {
          project = ((WorkspaceFolderBuildpath) toBuildPath).getFolder().getProject();
        }
        IBuildPath projectBuildPath =
            BuildPathManager.getInstance().getBuildPathByResource(project);
        if (projectBuildPath != null) {
          String includePath = constructPathFromRoot(toPath);
          return new ConstructedIncludePath(includePath, fromBuildPath, projectBuildPath);
        }
      }

      // in other case, using original build-paths for reporting unsatisfied state
      String includePath = constructPathFromRoot(toPath);
      return new ConstructedIncludePath(includePath, fromBuildPath, toBuildPath);
    }
  }
  /**
   * Constructs include path from one module to another.
   *
   * @param from - module to construct include path from.
   * @param to - module to construct include path to.
   * @return constructed include path
   */
  public static ConstructedIncludePath constructIncludePath(IModule from, IModule to) {
    IBuildPath fromBuildPath = from.getBuildPath();
    IBuildPath toBuildPath = to.getBuildPath();
    Set<IBuildPath> fromDependencies = fromBuildPath.getDependencies();
    if (fromDependencies.equals(toBuildPath)) {
      String includePath = constructPathFromRoot(to);
      return new ConstructedIncludePath(includePath, null, null);
    }
    // if "from" build-path directly depends from "to" build-path
    if (fromDependencies.contains(toBuildPath)) {
      String includePath = constructPathFromRoot(to);
      return new ConstructedIncludePath(includePath, null, null);
    } else {
      // for local modules using its project-based build-path instead of native module build-path
      if (to instanceof LocalModule) {
        IFile file = ((LocalModule) to).getFile();
        if (!file.isSynchronized(1)) {
          try {
            file.refreshLocal(1, new NullProgressMonitor());
            if (file.exists()) {
              IProject project = file.getProject();
              IBuildPath projectBuildPath =
                  BuildPathManager.getInstance().getBuildPathByResource(project);
              if (projectBuildPath != null) {
                IModule alternativeToModule = projectBuildPath.getModule(file);
                if (alternativeToModule != null) {
                  String includePath = constructPathFromRoot(alternativeToModule);
                  return new ConstructedIncludePath(includePath, fromBuildPath, projectBuildPath);
                }
              }
            }
          } catch (CoreException e) {
            IdeLog.logWarning(
                PHPEditorPlugin.getDefault(),
                "PHP Refactoring - Error while constructing an include-path (constructIncludePath)", //$NON-NLS-1$
                e,
                PHPEditorPlugin.DEBUG_SCOPE);
          }
        }
      }

      // in other case, using original build-paths for reporting unsatisfied state
      String includePath = constructPathFromRoot(to);
      return new ConstructedIncludePath(includePath, fromBuildPath, toBuildPath);
    }
  }
  /**
   * Constructs path from the root of a build path to a module.
   *
   * @param module - module.
   * @return include path.
   */
  private static String constructPathFromRoot(IModule module) {
    IPath path = module.getPath();
    StringBuilder result = new StringBuilder();
    String[] segments = path.segments();
    if (segments.length == 0) {
      return null;
    }

    for (int i = 0; i < segments.length - 1; i++) {
      result.append(segments[i]);
      result.append('/');
    }

    result.append(segments[segments.length - 1]);

    return result.toString();
  }
Example #5
0
 /**
  * Finds a PHPDoc comment above the offset in the module specified.
  *
  * @param module
  * @param offset
  * @param isParameter Indicate that the docs we are looking for are for a parameter.
  * @return comment contents or null if not found.
  */
 public static PHPDocBlock findFunctionPHPDocComment(
     IModule module, IDocument document, int offset, boolean isParameter) {
   try {
     Reader innerReader;
     if (document != null) {
       innerReader = new StringReader(document.get()); // $codepro.audit.disable closeWhereCreated
     } else {
       innerReader =
           new InputStreamReader(
               module.getContents(),
               EncodingUtils.getModuleEncoding(module)); // $codepro.audit.disable
       // closeWhereCreated
     }
     BufferedReader reader = new BufferedReader(innerReader);
     return innerParsePHPDoc(offset, reader, isParameter);
   } catch (Exception ex) {
     return null;
   }
 }