Java Code Examples for com.jetbrains.php.lang.parser.PhpElementTypes#ARRAY_VALUE

The following examples show how to use com.jetbrains.php.lang.parser.PhpElementTypes#ARRAY_VALUE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReturnArraySignatureRegistrarMatcher.java    From idea-php-toolbox with MIT License 6 votes vote down vote up
@Override
public boolean matches(@NotNull LanguageMatcherParameter parameter) {

    PsiElement parent = parameter.getElement().getParent();
    if(!(parent instanceof StringLiteralExpression)) {
        return false;
    }

    PsiElement arrayValue = parent.getParent();
    if(arrayValue == null || arrayValue.getNode().getElementType() != PhpElementTypes.ARRAY_VALUE) {
        return false;
    }

    PsiElement arrayCreation = arrayValue.getParent();
    if(!(arrayCreation instanceof ArrayCreationExpression)) {
        return false;
    }

    PsiElement phpReturn = arrayCreation.getParent();
    if(!(phpReturn instanceof PhpReturn)) {
        return false;
    }

    return PhpMatcherUtil.isMachingReturnArray(parameter.getSignatures(), phpReturn);
}
 
Example 2
Source File: SubscriberIndexUtil.java    From idea-php-shopware-plugin with MIT License 6 votes vote down vote up
/**
 * foo => 'goo'
 * foo => ['goo', ... ]
 */
@Nullable
public static String getMethodNameForEventValue(@NotNull PhpPsiElement value) {
    if(value instanceof StringLiteralExpression) {
        return ((StringLiteralExpression) value).getContents();
    }

    if(value instanceof ArrayCreationExpression) {
        PhpPsiElement firstPsiChild = value.getFirstPsiChild();
        if(firstPsiChild != null && firstPsiChild.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE) {
            StringLiteralExpression stringLiteral = ObjectUtils.tryCast(firstPsiChild.getFirstPsiChild(), StringLiteralExpression.class);
            if(stringLiteral != null) {
                return stringLiteral.getContents();
            }
        }

        return null;
    }

    return null;
}
 
Example 3
Source File: ShopwareSubscriperMethodInspection.java    From idea-php-shopware-plugin with MIT License 6 votes vote down vote up
private static String getHashKey(@NotNull StringLiteralExpression psiElement) {

        PsiElement arrayValue = psiElement.getParent();
        if(arrayValue != null && arrayValue.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE) {
            PsiElement arrayHash = arrayValue.getParent();

            if(arrayHash instanceof ArrayHashElement) {
                PhpPsiElement key = ((ArrayHashElement) arrayHash).getKey();
                if(key instanceof StringLiteralExpression) {
                    String contents = ((StringLiteralExpression) key).getContents();
                    if(StringUtils.isNotBlank(contents)) {
                        return contents;
                    }
                }
            }

        }

        return null;
    }
 
Example 4
Source File: DrupalPattern.java    From idea-php-drupal-symfony2-bridge with MIT License 6 votes vote down vote up
public static boolean isAfterArrayKey(PsiElement psiElement, String arrayKeyName) {

        PsiElement literal = psiElement.getContext();
        if(!(literal instanceof StringLiteralExpression)) {
            return false;
        }

        PsiElement arrayValue = literal.getParent();
        if(arrayValue.getNode().getElementType() != PhpElementTypes.ARRAY_VALUE) {
            return false;
        }

        PsiElement arrayHashElement = arrayValue.getParent();
        if(!(arrayHashElement instanceof ArrayHashElement)) {
            return false;
        }

        PsiElement arrayKey = ((ArrayHashElement) arrayHashElement).getKey();
        String keyString = PhpElementsUtil.getStringValue(arrayKey);

        return arrayKeyName.equals(keyString);
    }
 
Example 5
Source File: ReturnArraySignatureRegistrarMatcher.java    From idea-php-toolbox with MIT License 6 votes vote down vote up
@Override
public boolean matches(@NotNull LanguageMatcherParameter parameter) {

    PsiElement parent = parameter.getElement().getParent();
    if(!(parent instanceof StringLiteralExpression)) {
        return false;
    }

    PsiElement arrayValue = parent.getParent();
    if(arrayValue == null || arrayValue.getNode().getElementType() != PhpElementTypes.ARRAY_VALUE) {
        return false;
    }

    PsiElement arrayCreation = arrayValue.getParent();
    if(!(arrayCreation instanceof ArrayCreationExpression)) {
        return false;
    }

    PsiElement phpReturn = arrayCreation.getParent();
    if(!(phpReturn instanceof PhpReturn)) {
        return false;
    }

    return PhpMatcherUtil.isMachingReturnArray(parameter.getSignatures(), phpReturn);
}
 
Example 6
Source File: PhpElementsUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
/**
 *
 * array('key' => '<cursor>')
 *
 * Helper for to get find value getArrayPath
 * @param psiElement array value as leaf
 * @param key key name in current content
 * @return parent array creation element
 */
@Nullable
private static ArrayCreationExpression getArrayPathValue(PsiElement psiElement, String key) {

    PsiElement arrayValue = psiElement.getContext();
    if(arrayValue == null) {
        return null;
    }

    if(arrayValue.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE) {
        PsiElement arrayHashElement = arrayValue.getContext();
        if(arrayHashElement instanceof ArrayHashElement) {
            PhpPsiElement arrayKey = ((ArrayHashElement) arrayHashElement).getKey();
            if(arrayKey instanceof StringLiteralExpression && ((StringLiteralExpression) arrayKey).getContents().equals(key)) {
                PsiElement innerArrayKey = arrayKey.getParent();
                if(innerArrayKey != null && innerArrayKey.getNode().getElementType() == PhpElementTypes.ARRAY_KEY) {
                    PsiElement innerArrayHashElement = innerArrayKey.getParent();
                    if(innerArrayHashElement instanceof ArrayHashElement) {
                        PsiElement arrayCreation = innerArrayHashElement.getParent();
                        if(arrayCreation instanceof ArrayCreationExpression) {
                            return (ArrayCreationExpression) arrayCreation;
                        }
                    }
                }
            }
        }
    }

    return null;
}
 
Example 7
Source File: PhpArrayCallbackGotoCompletion.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
private PhpClass findClassCallback(@NotNull PsiElement position) {
    ArrayCreationExpression arrayCreation = PsiTreeUtil.getParentOfType(position, ArrayCreationExpression.class);
    if(arrayCreation == null) {
        return null;
    }

    PhpPsiElement arrayValue = arrayCreation.getFirstPsiChild();
    if(arrayValue == null || arrayValue.getNode().getElementType() != PhpElementTypes.ARRAY_VALUE) {
        return null;
    }

    PhpPsiElement variable = arrayValue.getFirstPsiChild();
    if(!(variable instanceof Variable)) {
        return null;
    }

    PsiElement resolve = ((Variable) variable).resolve();
    if(resolve instanceof PhpClass) {
        return (PhpClass) resolve;
    }

    for (String s : ((Variable) variable).getType().filterPrimitives().getTypes()) {
        Collection<PhpClass> anyByFQN = PhpIndex.getInstance(position.getProject()).getAnyByFQN(s);
        if(anyByFQN.size() > 0) {
            return anyByFQN.iterator().next();
        }
    }

    return null;
}
 
Example 8
Source File: ThemeUtil.java    From idea-php-shopware-plugin with MIT License 5 votes vote down vote up
public static void collectThemeJsFieldReferences(@NotNull StringLiteralExpression element, @NotNull ThemeAssetVisitor visitor) {

        PsiElement arrayValue = element.getParent();
        if(arrayValue.getNode().getElementType() != PhpElementTypes.ARRAY_VALUE) {
            return;
        }

        PsiElement arrayCreation = arrayValue.getParent();
        if(!(arrayCreation instanceof ArrayCreationExpression)) {
            return;
        }

        PsiElement classField = arrayCreation.getParent();
        if(!(classField instanceof Field)) {
            return;
        }

        if(!"javascript".equals(((Field) classField).getName())) {
            return;
        }


        PhpClass phpClass = PsiTreeUtil.getParentOfType(classField, PhpClass.class);
        if(phpClass == null || !PhpElementsUtil.isInstanceOf(phpClass, "\\Shopware\\Components\\Theme")) {
            return;
        }

        visitThemeAssetsFile(phpClass, visitor);
    }
 
Example 9
Source File: PhpElementsUtil.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
/**
 *
 * array('key' => '<cursor>')
 *
 * Helper for to get find value getArrayPath
 * @param psiElement array value as leaf
 * @param key key name in current content
 * @return parent array creation element
 */
@Nullable
private static ArrayCreationExpression getArrayPathValue(PsiElement psiElement, String key) {

    PsiElement arrayValue = psiElement.getContext();
    if(arrayValue == null) {
        return null;
    }

    if(arrayValue.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE) {
        PsiElement arrayHashElement = arrayValue.getContext();
        if(arrayHashElement instanceof ArrayHashElement) {
            PhpPsiElement arrayKey = ((ArrayHashElement) arrayHashElement).getKey();
            if(arrayKey instanceof StringLiteralExpression && ((StringLiteralExpression) arrayKey).getContents().equals(key)) {
                PsiElement innerArrayKey = arrayKey.getParent();
                if(innerArrayKey != null && innerArrayKey.getNode().getElementType() == PhpElementTypes.ARRAY_KEY) {
                    PsiElement innerArrayHashElement = innerArrayKey.getParent();
                    if(innerArrayHashElement instanceof ArrayHashElement) {
                        PsiElement arrayCreation = innerArrayHashElement.getParent();
                        if(arrayCreation instanceof ArrayCreationExpression) {
                            return (ArrayCreationExpression) arrayCreation;
                        }
                    }
                }
            }
        }
    }

    return null;
}
 
Example 10
Source File: PhpArrayCallbackGotoCompletion.java    From idea-php-toolbox with MIT License 5 votes vote down vote up
@Nullable
private PhpClass findClassCallback(@NotNull PsiElement position) {
    ArrayCreationExpression arrayCreation = PsiTreeUtil.getParentOfType(position, ArrayCreationExpression.class);
    if(arrayCreation == null) {
        return null;
    }

    PhpPsiElement arrayValue = arrayCreation.getFirstPsiChild();
    if(arrayValue == null || arrayValue.getNode().getElementType() != PhpElementTypes.ARRAY_VALUE) {
        return null;
    }

    PhpPsiElement variable = arrayValue.getFirstPsiChild();
    if(!(variable instanceof Variable)) {
        return null;
    }

    PsiElement resolve = ((Variable) variable).resolve();
    if(resolve instanceof PhpClass) {
        return (PhpClass) resolve;
    }

    for (String s : ((Variable) variable).getType().filterPrimitives().getTypes()) {
        Collection<PhpClass> anyByFQN = PhpIndex.getInstance(position.getProject()).getAnyByFQN(s);
        if(anyByFQN.size() > 0) {
            return anyByFQN.iterator().next();
        }
    }

    return null;
}
 
Example 11
Source File: FormGotoCompletionRegistrar.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
private GotoCompletionProvider createTranslationGotoCompletionWithLabelSwitch(@NotNull PsiElement origin, @NotNull ArrayCreationExpression choices, Processor<ArrayCreationExpression> processor) {
    PsiElement choicesArrayValue = choices.getParent();
    if(choicesArrayValue.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE) {
        PsiElement choicesValueHash = choicesArrayValue.getParent();
        if(choicesValueHash instanceof ArrayHashElement) {
            PhpPsiElement transKey = ((ArrayHashElement) choicesValueHash).getKey();
            String stringValue = PhpElementsUtil.getStringValue(transKey);

            if("choices".equals(stringValue)) {
                PsiElement choicesKey = transKey.getParent();
                if(choicesKey.getNode().getElementType() == PhpElementTypes.ARRAY_KEY) {
                    PsiElement formOptionsHash = choicesKey.getParent();
                    if(formOptionsHash instanceof ArrayHashElement) {
                        PsiElement arrayCreation = formOptionsHash.getParent();
                        if(arrayCreation instanceof ArrayCreationExpression) {
                            if(processor.process((ArrayCreationExpression) arrayCreation)) {
                                return createTranslationGotoCompletion(origin, arrayCreation);
                            }
                        }
                    }
                }
            }
        }
    }

    return null;
}
 
Example 12
Source File: EventMethodCallInspection.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void visitElement(PsiElement element) {
    super.visitElement(element);

    if(!(element instanceof StringLiteralExpression)) {
        return;
    }

    PsiElement arrayValue = element.getParent();
    if(arrayValue != null && arrayValue.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE) {
        PhpReturn phpReturn = PsiTreeUtil.getParentOfType(arrayValue, PhpReturn.class);
        if(phpReturn != null) {
            Method method = PsiTreeUtil.getParentOfType(arrayValue, Method.class);
            if(method != null) {
                String name = method.getName();
                if("getSubscribedEvents".equals(name)) {
                    PhpClass containingClass = method.getContainingClass();
                    if(containingClass != null && PhpElementsUtil.isInstanceOf(containingClass, "\\Symfony\\Component\\EventDispatcher\\EventSubscriberInterface")) {
                        String contents = ((StringLiteralExpression) element).getContents();
                        if(StringUtils.isNotBlank(contents) && containingClass.findMethodByName(contents) == null) {
                            registerMethodProblem(element, holder, containingClass);
                        }
                    }
                }
            }
        }
    }
}
 
Example 13
Source File: FormQueryBuilderRepositoryDetector.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Nullable
@Override
public String getRepository(@NotNull QueryBuilderRepositoryDetectorParameter parameter) {

    MethodReference qbMethodRef = parameter.getMethodReferenceByName("createQueryBuilder");
    if(qbMethodRef == null) {
        return null;
    }

    /*
    $builder->add('field_1', 'foo', array(
        'class' => 'Repository',
        'query_builder' => function (EntityRepository $er) {
            return $er->createQueryBuilder('u')
                ->orderBy('u.field_1', 'ASC');
        },
    ));
    */

    Function parentOfType = PsiTreeUtil.getParentOfType(qbMethodRef, Function.class);
    if(parentOfType != null && parentOfType.isClosure()) {
        PsiElement closure = parentOfType.getParent();
        if(closure.getNode().getElementType() == PhpElementTypes.CLOSURE) {
            PsiElement arrayValue = closure.getParent();
            if(arrayValue.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE) {
                PsiElement arrayHash = arrayValue.getParent();
                if(arrayHash instanceof ArrayHashElement) {
                    PsiElement arrayCreation = arrayHash.getParent();
                    if(arrayCreation instanceof ArrayCreationExpression) {
                        String aClass = PhpElementsUtil.getArrayValueString((ArrayCreationExpression) arrayCreation, "class");
                        if(aClass != null && StringUtils.isNotBlank(aClass)) {

                            // finally we found our class key
                            PhpClass phpClass = EntityHelper.resolveShortcutName(parameter.getProject(), aClass);
                            if(phpClass != null) {
                                return phpClass.getPresentableFQN();
                            }

                        }
                    }
                }
            }

        }
    }


    return null;
}
 
Example 14
Source File: EventDispatcherSubscriberUtil.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
/**
 * Extract method name for subscribe
 *
 * 'pre.foo1' => 'foo'
 * 'pre.foo1' => ['onStoreOrder', 0]
 * 'pre.foo2' => [['onStoreOrder', 0]]
 */
@NotNull
private static Collection<PsiElement> getSubscriberMethods(@NotNull ArrayHashElement arrayHashElement) {

    // support string, constants and array values
    PhpPsiElement value = arrayHashElement.getValue();
    if(value == null) {
        Collections.emptySet();
    }

    // 'pre.foo' => [...]
    if(!(value instanceof ArrayCreationExpression)) {
        return new ArrayList<>(Collections.singletonList(value));
    }

    Collection<PsiElement> psiElements = new HashSet<>();

    // 'pre.foo' => [<caret>]
    PsiElement firstChild = value.getFirstPsiChild();
    if(firstChild != null && firstChild.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE) {
        PhpPsiElement firstPsiChild = ((PhpPsiElement) firstChild).getFirstPsiChild();
        if(firstPsiChild instanceof StringLiteralExpression) {
            // 'pre.foo' => ['method']
            psiElements.add(firstPsiChild);
        } else if(firstPsiChild instanceof ArrayCreationExpression) {

            // 'pre.foo' => [['method', ...], ['method2', ...]]
            for (PsiElement psiElement : PsiElementUtils.getChildrenOfTypeAsList(firstPsiChild, PlatformPatterns.psiElement().withElementType(PhpElementTypes.ARRAY_VALUE))) {
                if(!(psiElement instanceof PhpPsiElement)) {
                    continue;
                }

                PhpPsiElement prioValue = ((PhpPsiElement) psiElement).getFirstPsiChild();
                if(prioValue instanceof StringLiteralExpression) {
                    psiElements.add(prioValue);
                }
            }
        }
    }

    return psiElements;
}