Search in sources :

Example 6 with PhpPsiElement

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

the class SuspiciousOperatorFormattingStrategy method apply.

public static void apply(@NotNull AssignmentExpression expression, @NotNull ProblemsHolder holder) {
    final PhpPsiElement value = expression.getValue();
    if (value instanceof UnaryExpression) {
        /* previous should be "...=[!+-] ..." */
        final PsiElement previous = value.getPrevSibling();
        if (null == previous || PhpTokenTypes.opASGN != previous.getNode().getElementType()) {
            return;
        }
        final PsiElement valueOperator = ((UnaryExpression) value).getOperation();
        if (null == valueOperator || !(valueOperator.getNextSibling() instanceof PsiWhiteSpace)) {
            return;
        }
        /* analyze statement */
        final IElementType valueOperation = valueOperator.getNode().getElementType();
        if (mapping.containsKey(valueOperation)) {
            holder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(messagePattern.replace("%o%", mapping.get(valueOperation))));
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) UnaryExpression(com.jetbrains.php.lang.psi.elements.UnaryExpression) 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)

Example 7 with PhpPsiElement

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

Example 8 with PhpPsiElement

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

the class ClassConstantMatcherInspection 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_CONSTANT_REFERENCE).accepts(element)) {
                return;
            }
            Set<String> constants = getDeprecatedClassConstants(element);
            ClassConstantReference classConstantReference = (ClassConstantReference) element;
            if (constants.contains(classConstantReference.getText())) {
                problemsHolder.registerProblem(element, "Deprecated class constant");
            }
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) ClassConstantReference(com.jetbrains.php.lang.psi.elements.ClassConstantReference) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with PhpPsiElement

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

the class ClassNameMatcherInspection method getDeprecatedClasses.

private Set<String> getDeprecatedClasses(PhpPsiElement element) {
    Set<PsiElement> elements = new HashSet<>();
    PsiFile[] classNameMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "ClassNameMatcher.php", GlobalSearchScope.allScope(element.getProject()));
    for (PsiFile file : classNameMatcherFiles) {
        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) 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) ClassReference(com.jetbrains.php.lang.psi.elements.ClassReference) 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) PsiFile(com.intellij.psi.PsiFile) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet)

Example 10 with PhpPsiElement

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

the class FunctionCallMatcherInspection method getRemovedGlobalFuntions.

private Set<String> getRemovedGlobalFuntions(PhpPsiElement element) {
    Set<PsiElement> elements = new HashSet<>();
    PsiFile[] constantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "FunctionCallMatcher.php", GlobalSearchScope.allScope(element.getProject()));
    for (PsiFile file : constantMatcherFiles) {
        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) 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) FunctionReference(com.jetbrains.php.lang.psi.elements.FunctionReference) 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) PsiFile(com.intellij.psi.PsiFile) PhpPsiElement(com.jetbrains.php.lang.psi.elements.PhpPsiElement) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet)

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