Search in sources :

Example 1 with PhpPsiElement

use of com.jetbrains.php.lang.psi.elements.PhpPsiElement in project idea-php-typo3-plugin by cedricziel.

the class ClassConstantMatcherInspection method getDeprecatedClassConstants.

private Set<String> getDeprecatedClassConstants(PhpPsiElement element) {
    Set<PsiElement> elements = new HashSet<>();
    PsiFile[] classConstantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "ClassConstantMatcher.php", GlobalSearchScope.allScope(element.getProject()));
    for (PsiFile file : classConstantMatcherFiles) {
        Collections.addAll(elements, PsiTreeUtil.collectElements(file, el -> PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withAncestor(4, PlatformPatterns.psiElement(PhpElementTypes.RETURN))).accepts(el)));
    }
    return elements.stream().map(stringLiteral -> ((StringLiteralExpression) stringLiteral).getContents()).collect(Collectors.toSet());
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) GroupNames(com.intellij.codeInsight.daemon.GroupNames) FilenameIndex(com.intellij.psi.search.FilenameIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ClassConstantReference(com.jetbrains.php.lang.psi.elements.ClassConstantReference) Set(java.util.Set) Collectors(java.util.stream.Collectors) PlatformPatterns(com.intellij.patterns.PlatformPatterns) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) HashSet(java.util.HashSet) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Nls(org.jetbrains.annotations.Nls) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) PhpInspection(com.jetbrains.php.lang.inspections.PhpInspection) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) Collections(java.util.Collections) PhpElementTypes(com.jetbrains.php.lang.parser.PhpElementTypes) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) StringLiteralExpression(com.jetbrains.php.lang.psi.elements.StringLiteralExpression) PsiFile(com.intellij.psi.PsiFile) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet)

Example 2 with PhpPsiElement

use of com.jetbrains.php.lang.psi.elements.PhpPsiElement in project idea-php-typo3-plugin by cedricziel.

the class ClassNameMatcherInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
    return new PhpElementVisitor() {

        @Override
        public void visitPhpElement(PhpPsiElement element) {
            if (!PlatformPatterns.psiElement(PhpElementTypes.CLASS_REFERENCE).accepts(element)) {
                return;
            }
            Set<String> constants = getDeprecatedClasses(element);
            ClassReference classReference = (ClassReference) element;
            if (constants.contains(classReference.getFQN())) {
                problemsHolder.registerProblem(element, "Class removed with TYPO3 9, consider using an alternative");
            }
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PhpPsiElement

use of com.jetbrains.php.lang.psi.elements.PhpPsiElement in project idea-php-typo3-plugin by cedricziel.

the class ConstantMatcherInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
    return new PhpElementVisitor() {

        @Override
        public void visitPhpElement(PhpPsiElement element) {
            if (!PlatformPatterns.psiElement(PhpElementTypes.CONSTANT_REF).accepts(element)) {
                return;
            }
            ConstantReference constantReference = (ConstantReference) element;
            Set<String> constants = getRemovedConstantsFQNs(constantReference);
            if (constants.contains(constantReference.getFQN())) {
                problemsHolder.registerProblem(element, "Constant removed with TYPO3 9, consider using an alternative");
            }
        }
    };
}
Also used : ConstantReference(com.jetbrains.php.lang.psi.elements.ConstantReference) PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with PhpPsiElement

use of com.jetbrains.php.lang.psi.elements.PhpPsiElement 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 5 with PhpPsiElement

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

the class DropMethodFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement expression = descriptor.getPsiElement().getParent();
    if (expression instanceof Method && !project.isDisposed()) {
        /* delete preceding PhpDoc */
        final PhpPsiElement previous = ((Method) expression).getPrevPsiSibling();
        if (previous instanceof PhpDocComment) {
            previous.delete();
        }
        /* delete space after the method */
        final PsiElement nextExpression = expression.getNextSibling();
        if (nextExpression instanceof PsiWhiteSpace) {
            nextExpression.delete();
        }
        /* delete the method itself */
        expression.delete();
    }
}
Also used : PhpDocComment(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment) Method(com.jetbrains.php.lang.psi.elements.Method) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

PhpPsiElement (com.jetbrains.php.lang.psi.elements.PhpPsiElement)13 NotNull (org.jetbrains.annotations.NotNull)10 PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)9 PsiElement (com.intellij.psi.PsiElement)8 StringLiteralExpression (com.jetbrains.php.lang.psi.elements.StringLiteralExpression)5 GroupNames (com.intellij.codeInsight.daemon.GroupNames)3 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)3 PlatformPatterns (com.intellij.patterns.PlatformPatterns)3 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)3 PsiFile (com.intellij.psi.PsiFile)3 FilenameIndex (com.intellij.psi.search.FilenameIndex)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)3 PhpInspection (com.jetbrains.php.lang.inspections.PhpInspection)3 PhpElementTypes (com.jetbrains.php.lang.parser.PhpElementTypes)3 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 Nls (org.jetbrains.annotations.Nls)3