@Nullable
  public static String getArrayHashValue(
      ArrayCreationExpression arrayCreationExpression, String keyName) {
    ArrayHashElement translationArrayHashElement =
        PsiElementUtils.getChildrenOfType(
            arrayCreationExpression,
            PlatformPatterns.psiElement(ArrayHashElement.class)
                .withFirstChild(
                    PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY)
                        .withText(
                            PlatformPatterns.string()
                                .oneOf("'" + keyName + "'", "\"" + keyName + "\""))));

    if (translationArrayHashElement == null) {
      return null;
    }

    if (!(translationArrayHashElement.getValue() instanceof StringLiteralExpression)) {
      return null;
    }

    StringLiteralExpression valueString =
        (StringLiteralExpression) translationArrayHashElement.getValue();
    if (valueString == null) {
      return null;
    }

    return valueString.getContents();
  }
  /** array('foo' => 'bar', 'foo1' => 'bar', 1 => 'foo') */
  public static HashMap<String, String> getArrayKeyValueMap(
      @NotNull ArrayCreationExpression arrayCreationExpression) {
    HashMap<String, String> keys = new HashMap<String, String>();

    for (ArrayHashElement arrayHashElement : arrayCreationExpression.getHashElements()) {
      PhpPsiElement child = arrayHashElement.getKey();
      if (child != null
          && ((child instanceof StringLiteralExpression)
              || PhpPatterns.psiElement(PhpElementTypes.NUMBER).accepts(child))) {

        String key;
        if (child instanceof StringLiteralExpression) {
          key = ((StringLiteralExpression) child).getContents();
        } else {
          key = child.getText();
        }

        if (StringUtils.isBlank(key)) {
          continue;
        }

        String value = null;

        if (arrayHashElement.getValue() instanceof StringLiteralExpression) {
          value = ((StringLiteralExpression) arrayHashElement.getValue()).getContents();
        }

        keys.put(key, value);
      }
    }

    return keys;
  }
Exemplo n.º 3
0
  private void parseTypes2(
      Map<String, Collection<String>> map, Iterable<ArrayHashElement> elements, String parent) {
    Collection<String> types = map.get(parent);
    if (types == null) {
      types = new ArrayList<String>();
      map.put(parent, types);
    }

    for (ArrayHashElement element : elements) {
      PhpPsiElement key = element.getKey();
      if (key instanceof StringLiteralExpression) {
        // key
        String keyName = ((StringLiteralExpression) key).getContents();
        types.add(keyName);

        String fullKeyName = parent.length() > 0 ? (parent + "." + keyName) : keyName;

        // value
        PhpPsiElement val = element.getValue();
        if (val instanceof ArrayCreationExpression) { // recursive
          Iterable<ArrayHashElement> subElements =
              ((ArrayCreationExpression) val).getHashElements();
          parseTypes2(map, subElements, fullKeyName);

        } else if (val instanceof FieldReference) { // reference to a field, where it's defined
          String classFqn = ((ClassReference) ((FieldReference) val).getClassReference()).getFQN();
          for (PhpClass phpClass :
              PhpIndex.getInstance(element.getProject()).getClassesByFQN(classFqn)) {
            Field field = phpClass.findFieldByName(((FieldReference) val).getNameCS(), false);
            if (field.getDefaultValue() instanceof ArrayCreationExpression) {
              Iterable<ArrayHashElement> subElements =
                  ((ArrayCreationExpression) field.getDefaultValue()).getHashElements();
              parseTypes2(map, subElements, fullKeyName);
            }
          }

        } else { // get value type
          parseValueType(val);

          // try annotation
          PsiElement el2 = element;
          while (el2 != null
              && (el2 instanceof LeafPsiElement
                      && ((LeafPsiElement) el2).getElementType() == PhpTokenTypes.opCOMMA
                  || el2 instanceof PsiWhiteSpace)) {
            el2 = el2.getNextSibling();
          }
          if (el2 instanceof PsiComment) {
            System.out.println("Comment for " + fullKeyName + ": " + el2.getText());
          }
        }
      }
    }
  }
  @Nullable
  public static PhpPsiElement getArrayValue(
      ArrayCreationExpression arrayCreationExpression, String name) {

    for (ArrayHashElement arrayHashElement : arrayCreationExpression.getHashElements()) {
      PhpPsiElement child = arrayHashElement.getKey();
      if (child instanceof StringLiteralExpression) {
        if (((StringLiteralExpression) child).getContents().equals(name)) {
          return arrayHashElement.getValue();
        }
      }
    }

    return null;
  }
  /**
   * Gets array key-value as single PsiElement map
   *
   * <p>['foo' => $bar]
   */
  @NotNull
  public static Map<String, PsiElement> getArrayValueMap(
      @NotNull ArrayCreationExpression arrayCreationExpression) {
    Map<String, PsiElement> keys = new HashMap<String, PsiElement>();

    for (ArrayHashElement arrayHashElement : arrayCreationExpression.getHashElements()) {
      PhpPsiElement child = arrayHashElement.getKey();
      if (child instanceof StringLiteralExpression) {
        PhpPsiElement value = arrayHashElement.getValue();
        if (value != null) {
          keys.put(((StringLiteralExpression) child).getContents(), value);
        }
      }
    }

    return keys;
  }