Search in sources :

Example 1 with ArrayHashElement

use of com.jetbrains.php.lang.psi.elements.ArrayHashElement in project yii2support by nvlad.

the class PhpUtil method getArrayKeys.

@NotNull
public static Collection<String> getArrayKeys(ArrayCreationExpression array) {
    final HashSet<String> result = new HashSet<>();
    Iterable<ArrayHashElement> items = array.getHashElements();
    for (ArrayHashElement item : items) {
        if (item.getKey() != null && item.getKey() instanceof StringLiteralExpression) {
            result.add(((StringLiteralExpression) item.getKey()).getContents());
        }
    }
    return result;
}
Also used : StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ArrayHashElement(com.jetbrains.php.lang.psi.elements.ArrayHashElement) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ArrayHashElement

use of com.jetbrains.php.lang.psi.elements.ArrayHashElement in project phpinspectionsea by kalessil.

the class PackedHashtableOptimizationInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        /* TODO: docs, http://blog.jpauli.tech/2016/04/08/hashtables.html#packed-hashtable-optimization */
        @Override
        public void visitPhpArrayCreationExpression(@NotNull ArrayCreationExpression expression) {
            /* requires PHP7 */
            if (PhpLanguageLevel.get(holder.getProject()).below(PhpLanguageLevel.PHP700)) {
                return;
            }
            /* requires at least 3 children - let array to grow enough */
            final PsiElement[] children = expression.getChildren();
            if (children.length < 3) {
                return;
            }
            /* false-positives: test classes */
            if (this.isTestContext(expression)) {
                return;
            }
            /* step 1: collect indexes and verify array structure */
            final List<PhpPsiElement> indexes = new ArrayList<>();
            for (final PsiElement pairCandidate : children) {
                if (pairCandidate instanceof ArrayHashElement) {
                    final PhpPsiElement key = ((ArrayHashElement) pairCandidate).getKey();
                    if ((key instanceof StringLiteralExpression && key.getFirstPsiChild() == null) || OpenapiTypesUtil.isNumber(key)) {
                        indexes.add(key);
                        continue;
                    }
                }
                break;
            }
            if (indexes.size() != children.length) {
                indexes.clear();
                return;
            }
            /* step 2: analyze collected indexes */
            // if string literal is not numeric => stop
            boolean hasStringIndexes = false;
            boolean hasIncreasingIndexes = true;
            int lastIndex = Integer.MIN_VALUE;
            for (PhpPsiElement index : indexes) {
                final String numericIndex;
                final int integerIndex;
                /* extract text representation of the index */
                if (index instanceof StringLiteralExpression) {
                    hasStringIndexes = true;
                    numericIndex = ((StringLiteralExpression) index).getContents();
                    /* '01' and etc cases can not be converted */
                    if (numericIndex.length() > 1 && '0' == numericIndex.charAt(0)) {
                        indexes.clear();
                        return;
                    }
                } else {
                    numericIndex = index.getText().replaceAll("\\s+", "");
                }
                /* try converting into integer */
                try {
                    integerIndex = Integer.parseInt(numericIndex);
                } catch (NumberFormatException error) {
                    indexes.clear();
                    return;
                }
                if (integerIndex < lastIndex) {
                    hasIncreasingIndexes = false;
                }
                lastIndex = integerIndex;
            }
            /* report if criteria are met */
            if (!hasIncreasingIndexes) {
                holder.registerProblem(expression.getFirstChild(), MessagesPresentationUtil.prefixWithEa(messageReorder));
            }
            if (hasIncreasingIndexes && hasStringIndexes) {
                holder.registerProblem(expression.getFirstChild(), MessagesPresentationUtil.prefixWithEa(messageUseNumericKeys));
            }
        }
    };
}
Also used : ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ArrayList(java.util.ArrayList) ArrayHashElement(com.jetbrains.php.lang.psi.elements.ArrayHashElement) NotNull(org.jetbrains.annotations.NotNull) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ArrayHashElement

use of com.jetbrains.php.lang.psi.elements.ArrayHashElement in project phpinspectionsea by kalessil.

the class DuplicateArrayKeysInspector method buildVisitor.

@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PhpElementVisitor() {

        @Override
        public void visitPhpArrayCreationExpression(@NotNull ArrayCreationExpression expression) {
            final Map<String, PsiElement> processed = new HashMap<>();
            for (final ArrayHashElement pair : expression.getHashElements()) {
                final PhpPsiElement key = pair.getKey();
                if (key instanceof StringLiteralExpression && key.getFirstPsiChild() == null) {
                    final PsiElement value = pair.getValue();
                    if (value != null) {
                        final String literal = ((StringLiteralExpression) key).getContents();
                        if (processed.containsKey(literal)) {
                            final boolean isPairDuplicated = !(value instanceof ArrayCreationExpression) && OpenapiEquivalenceUtil.areEqual(value, processed.get(literal));
                            if (isPairDuplicated) {
                                holder.registerProblem(pair, MessagesPresentationUtil.prefixWithEa(messageDuplicatePair), ProblemHighlightType.LIKE_UNUSED_SYMBOL);
                            } else {
                                holder.registerProblem(key, MessagesPresentationUtil.prefixWithEa(messageDuplicateKey));
                            }
                        }
                        processed.put(literal, value);
                    }
                }
            }
            processed.clear();
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) ArrayCreationExpression(com.jetbrains.php.lang.psi.elements.ArrayCreationExpression) HashMap(java.util.HashMap) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) ArrayHashElement(com.jetbrains.php.lang.psi.elements.ArrayHashElement) NotNull(org.jetbrains.annotations.NotNull) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ArrayHashElement (com.jetbrains.php.lang.psi.elements.ArrayHashElement)3 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)3 NotNull (org.jetbrains.annotations.NotNull)3 PsiElement (com.intellij.psi.PsiElement)2 ArrayCreationExpression (com.jetbrains.php.lang.psi.elements.ArrayCreationExpression)2 PhpPsiElement (com.jetbrains.php.lang.psi.elements.PhpPsiElement)2 PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)1 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1