|
| 1 | +package com.cedricziel.idea.typo3.extensionScanner.codeInspection; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.daemon.GroupNames; |
| 4 | +import com.intellij.codeInspection.ProblemsHolder; |
| 5 | +import com.intellij.patterns.PlatformPatterns; |
| 6 | +import com.intellij.psi.PsiElement; |
| 7 | +import com.intellij.psi.PsiElementVisitor; |
| 8 | +import com.intellij.psi.PsiFile; |
| 9 | +import com.intellij.psi.search.FilenameIndex; |
| 10 | +import com.intellij.psi.search.GlobalSearchScope; |
| 11 | +import com.intellij.psi.util.PsiTreeUtil; |
| 12 | +import com.jetbrains.php.lang.inspections.PhpInspection; |
| 13 | +import com.jetbrains.php.lang.parser.PhpElementTypes; |
| 14 | +import com.jetbrains.php.lang.psi.elements.ConstantReference; |
| 15 | +import com.jetbrains.php.lang.psi.elements.PhpPsiElement; |
| 16 | +import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; |
| 17 | +import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor; |
| 18 | +import org.jetbrains.annotations.Nls; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +public class ConstantMatcherInspection extends PhpInspection { |
| 27 | + @Nls |
| 28 | + @NotNull |
| 29 | + @Override |
| 30 | + public String getGroupDisplayName() { |
| 31 | + return GroupNames.BUGS_GROUP_NAME; |
| 32 | + } |
| 33 | + |
| 34 | + @NotNull |
| 35 | + public String getDisplayName() { |
| 36 | + return "Constant removed with TYPO3 9"; |
| 37 | + } |
| 38 | + |
| 39 | + @NotNull |
| 40 | + @Override |
| 41 | + public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) { |
| 42 | + return new PhpElementVisitor() { |
| 43 | + @Override |
| 44 | + public void visitPhpElement(PhpPsiElement element) { |
| 45 | + |
| 46 | + if (!PlatformPatterns.psiElement(PhpElementTypes.CLASS_REFERENCE).accepts(element)) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + Set<String> constants = getRemovedConstantsFQNs(element); |
| 51 | + ConstantReference constantReference = (ConstantReference) element; |
| 52 | + if (constants.contains(constantReference.getFQN())) { |
| 53 | + problemsHolder.registerProblem(element, "Constant removed with TYPO3 9, consider using an alternative"); |
| 54 | + } |
| 55 | + } |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + private Set<String> getRemovedConstantsFQNs(PhpPsiElement element) { |
| 60 | + Set<PsiElement> elements = new HashSet<>(); |
| 61 | + PsiFile[] constantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "ConstantMatcher.php", GlobalSearchScope.allScope(element.getProject())); |
| 62 | + for (PsiFile file : constantMatcherFiles) { |
| 63 | + |
| 64 | + Collections.addAll( |
| 65 | + elements, |
| 66 | + PsiTreeUtil.collectElements(file, el -> PlatformPatterns |
| 67 | + .psiElement(StringLiteralExpression.class) |
| 68 | + .withParent( |
| 69 | + PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY) |
| 70 | + .withAncestor( |
| 71 | + 4, |
| 72 | + PlatformPatterns.psiElement(PhpElementTypes.RETURN) |
| 73 | + ) |
| 74 | + ) |
| 75 | + .accepts(el) |
| 76 | + ) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + return elements.stream() |
| 81 | + .map(stringLiteral -> "\\" + ((StringLiteralExpression)stringLiteral).getContents()) |
| 82 | + .collect(Collectors.toSet()); |
| 83 | + } |
| 84 | +} |
0 commit comments