private String processReturnType(MethodDeclaration methodDeclaration) {
   PHPMethodDeclaration phpMethodDeclaration = (PHPMethodDeclaration) methodDeclaration;
   PHPDocBlock docBlock = phpMethodDeclaration.getPHPDoc();
   String type = VOID_RETURN_TYPE;
   if (phpMethodDeclaration.getReturnType() != null) {
     return phpMethodDeclaration.getReturnType().getName();
   } else if (docBlock != null) {
     for (PHPDocTag tag : docBlock.getTags(PHPDocTag.RETURN)) {
       if (tag.getTypeReferences().size() > 0) {
         return PHPModelUtils.appendTypeReferenceNames(tag.getTypeReferences());
       }
     }
   }
   return type;
 }
Exemplo n.º 2
0
 public boolean visit(PHPMethodDeclaration s) throws Exception {
   Map<String, String> parameters = createInitialParameters(s);
   parameters.put("name", s.getName());
   xmlWriter.startTag("PHPMethodDeclaration", parameters);
   return true;
 }
  @Override
  @SuppressWarnings("unchecked")
  public boolean visit(MethodDeclaration s) throws Exception {
    IProject project = sourceModule.getScriptProject().getProject();
    if (project == null || !project.isAccessible() || !project.hasNature(TwigNature.NATURE_ID)) {
      return false;
    }
    if (!methods.contains(s)) methods.add(s);

    if (s instanceof PHPMethodDeclaration) {

      PHPMethodDeclaration phpMethod = (PHPMethodDeclaration) s;

      if (inTwigExtension && phpMethod.getName().equals(TwigCoreConstants.GET_FILTERS)) {

        phpMethod.traverse(
            new PHPASTVisitor() {

              @Override
              public boolean visit(ArrayElement s) throws Exception {

                Expression key = s.getKey();
                Expression value = s.getValue();

                if (key == null | value == null) {
                  return false;
                }

                if (key.getClass() == Scalar.class
                    && value.getClass() == ClassInstanceCreation.class) {

                  Scalar name = (Scalar) key;
                  ClassInstanceCreation filterClass = (ClassInstanceCreation) value;

                  CallArgumentsList ctorParams = filterClass.getCtorParams();
                  Object child = ctorParams.getChilds().get(0);

                  if (child instanceof VariableReference
                      && ((VariableReference) child).getName().equals("$this")
                      && filterClass
                          .getClassName()
                          .toString()
                          .equals((TwigCoreConstants.TWIG_FILTER_METHOD))) {

                    if (ctorParams.getChilds().size() > 2
                        && ctorParams.getChilds().get(1) instanceof Scalar) {
                      Scalar internal = (Scalar) ctorParams.getChilds().get(1);
                      String elemName = name.getValue().replaceAll("['\"]", "");
                      Filter filter = new Filter(elemName);
                      filter.setInternalFunction(internal.getValue().replaceAll("['\"]", ""));
                      filter.setPhpClass(currentClass.getName());
                      filters.add(filter);
                    }
                  }

                  if (!(child instanceof Scalar)) {
                    return true;
                  }

                  Scalar internal = (Scalar) child;

                  if (filterClass
                      .getClassName()
                      .toString()
                      .equals(TwigCoreConstants.TWIG_FILTER_FUNCTION)) {

                    String elemName = name.getValue().replaceAll("['\"]", "");

                    Filter filter = new Filter(elemName);
                    filter.setInternalFunction(internal.getValue().replaceAll("['\"]", ""));
                    filter.setPhpClass(currentClass.getName());

                    filters.add(filter);
                  }
                }
                return true;
              }
            });

      } else if (inTwigExtension && TwigCoreConstants.GET_TESTS.equals(s.getName())) {

        phpMethod.traverse(
            new PHPASTVisitor() {

              @Override
              public boolean visit(ArrayElement s) throws Exception {

                Expression key = s.getKey();
                Expression value = s.getValue();

                if (key == null || value == null) return false;

                if (key.getClass() == Scalar.class
                    && value.getClass() == ClassInstanceCreation.class) {

                  Scalar name = (Scalar) key;
                  ClassInstanceCreation functionClass = (ClassInstanceCreation) value;

                  CallArgumentsList args = functionClass.getCtorParams();
                  if (!(args.getChilds().get(0) instanceof Scalar)) {
                    return true;
                  }
                  Scalar internalFunction = (Scalar) args.getChilds().get(0);

                  if (internalFunction == null) return true;

                  if (functionClass
                      .getClassName()
                      .toString()
                      .equals(TwigCoreConstants.TWIG_TEST_FUNCTION)) {

                    String elemName = name.getValue().replaceAll("['\"]", "");

                    JSONObject metadata = new JSONObject();
                    metadata.put(TwigType.PHPCLASS, currentClass.getName());

                    Test test = new Test(elemName);
                    test.setPhpClass(currentClass.getName());
                    test.setInternalFunction(internalFunction.getValue().replaceAll("['\"]", ""));
                    tests.add(test);
                  }
                }
                return true;
              }
            });

      } else if (inTwigExtension && TwigCoreConstants.GET_FUNCTIONS.equals(s.getName())) {

        phpMethod.traverse(
            new PHPASTVisitor() {
              @Override
              public boolean visit(ArrayElement s) throws Exception {

                Expression key = s.getKey();
                Expression value = s.getValue();

                if (key == null || value == null) {
                  return false;
                }

                if (key.getClass() == Scalar.class
                    && value.getClass() == ClassInstanceCreation.class) {

                  Scalar name = (Scalar) key;
                  ClassInstanceCreation functionClass = (ClassInstanceCreation) value;
                  CallArgumentsList args = functionClass.getCtorParams();
                  String functionClassName = functionClass.getClassName().toString();
                  int index = -1;

                  if (functionClassName.equals(TwigCoreConstants.TWIG_FUNCTION_FUNCTION)) {
                    index = 0;
                  } else if (functionClassName.equals(TwigCoreConstants.TWIG_FUNCTION_METHOD)) {
                    index = 1;
                  }

                  if (index > -1 && args.getChilds().get(index) instanceof Scalar) {

                    Scalar internalFunction = (Scalar) args.getChilds().get(index);

                    if (internalFunction == null) {
                      return true;
                    }

                    String elemName = name.getValue().replaceAll("['\"]", "");
                    JSONObject metadata = new JSONObject();
                    metadata.put(TwigType.PHPCLASS, currentClass.getName());

                    Function function = new Function(elemName);
                    function.setPhpClass(currentClass.getName());
                    function.setInternalFunction(
                        internalFunction.getValue().replaceAll("['\"]", ""));
                    functions.add(function);
                  }
                }
                return true;
              }
            });

      } else if (inTokenParser && TwigCoreConstants.PARSE_TOKEN_METHOD.equals(s.getName())) {

        inTagParseMethod = true;

      } else if (inTokenParser && TwigCoreConstants.PARSE_GET_TAG_METHOD.equals(s.getName())) {

        phpMethod.traverse(
            new PHPASTVisitor() {
              @Override
              public boolean visit(ReturnStatement s) throws Exception {
                if (s.getExpr().getClass() == Scalar.class) {
                  Scalar scalar = (Scalar) s.getExpr();
                  tag.setStartTag(scalar.getValue().replaceAll("['\"]", ""));
                }
                return false;
              }
            });
      }
    }

    return false;
  }
  @Override
  public boolean endvisit(ModuleDeclaration s) throws Exception {
    for (Test test : tests) {
      for (MethodDeclaration method : methods) {
        if (method.getName().equals(test.getInternalFunction())) {

          PHPMethodDeclaration phpMethod = (PHPMethodDeclaration) method;
          PHPDocBlock doc = phpMethod.getPHPDoc();

          if (doc != null) {
            test.addDoc(doc);
          }

          Logger.debugMSG(
              "indexing test tag: "
                  + test.getElementName()
                  + " with metadata: "
                  + test.getMetadata());

          ReferenceInfo info =
              new ReferenceInfo(
                  ITwigModelElement.TEST, 0, 0, test.getElementName(), test.getMetadata(), null);
          addReferenceInfo(info);
        }
      }
    }

    for (Function function : functions) {

      for (MethodDeclaration method : methods) {

        if (method.getName().equals(function.getInternalFunction())) {

          PHPMethodDeclaration phpMethod = (PHPMethodDeclaration) method;
          PHPDocBlock doc = phpMethod.getPHPDoc();

          if (doc != null) {
            function.addDoc(doc);
          }

          function.addArgs(method.getArguments());

          Logger.debugMSG(
              "indexing function: "
                  + function.getElementName()
                  + " with metadata: "
                  + function.getMetadata());
          ReferenceInfo info =
              new ReferenceInfo(
                  ITwigModelElement.FUNCTION,
                  0,
                  0,
                  function.getElementName(),
                  function.getMetadata(),
                  null);
          addReferenceInfo(info);
        }
      }
    }

    for (Filter filter : filters) {

      for (MethodDeclaration method : methods) {

        if (method.getName().equals(filter.getInternalFunction())) {

          PHPMethodDeclaration phpMethod = (PHPMethodDeclaration) method;
          PHPDocBlock doc = phpMethod.getPHPDoc();

          if (doc != null) {
            filter.addDoc(doc);
          }

          filter.addArgs(method.getArguments());

          Logger.debugMSG(
              "indexing filter: "
                  + filter.getElementName()
                  + " with metadata: "
                  + filter.getMetadata());
          ReferenceInfo info =
              new ReferenceInfo(
                  ITwigModelElement.FILTER,
                  0,
                  0,
                  filter.getElementName(),
                  filter.getMetadata(),
                  null);
          addReferenceInfo(info);
        }
      }
    }

    return true;
  }