package com.cedricziel.idea.typo3.extensionScanner.codeInspection;

import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.FilenameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.inspections.PhpInspection;
import com.jetbrains.php.lang.parser.PhpElementTypes;
import com.jetbrains.php.lang.psi.elements.ClassConstantReference;
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class ClassConstantMatcherInspection extends PhpInspection {
    @Nls
    @NotNull
    @Override
    public String getGroupDisplayName() {
        return GroupNames.BUGS_GROUP_NAME;
    }

    @NotNull
    public String getDisplayName() {
        return "Deprecated class constant";
    }

    @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");
                }
            }
        };
    }

    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());
    }
}

@@ -76,427 +76,207 @@
 com.
-intellij.codeInsight.daemon.GroupNames;%0Aimport com.intellij.codeInspection.ProblemsHolder;%0Aimport com.intellij.patterns.PlatformPatterns;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.intellij.psi.PsiElementVisitor;%0Aimport com.intellij.psi.PsiFile;%0Aimport com.intellij.psi.search.FilenameIndex;%0Aimport com.intellij.psi.search.GlobalSearchScope;%0Aimport com.intellij.psi.util.PsiTreeUtil;%0Aimport com.jetbrains.php.lang.i
+cedricziel.idea.typo3.codeInspection.PluginEnabledPhpInspection;%0Aimport com.cedricziel.idea.typo3.util.DeprecationUtility;%0Aimport com.intellij.codeInsight.daemon.GroupNames;%0Aimport com.intellij.codeI
 nspe
@@ -284,77 +284,66 @@
 tion
-s
 .P
-hpInspection;%0Aimport com.jetbrains.php.lang.parser.PhpElementTypes
+roblemsHolder;%0Aimport com.intellij.psi.PsiElementVisitor
 ;%0Aim
@@ -445,355 +445,114 @@
 psi.
-elements.PhpPsiElement;%0Aimport com.jetbrains.php.lang.psi.elements.StringLiteralExpression;%0Aimport com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;%0Aimport org.jetbrains.annotations.Nls;%0Aimport org.jetbrains.annotations.NotNull;%0A%0Aimport java.util.Collections;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0Aimport java.util.stream.Collectors
+visitors.PhpElementVisitor;%0Aimport org.jetbrains.annotations.Nls;%0Aimport org.jetbrains.annotations.NotNull
 ;%0A%0Ap
@@ -602,16 +602,29 @@
 extends 
+PluginEnabled
 PhpInspe
@@ -921,16 +921,20 @@
 or build
+Real
 Visitor(
@@ -1023,24 +1023,25 @@
 Visitor() %7B%0A
+%0A
             
@@ -1086,294 +1086,8 @@
 tPhp
-Element(PhpPsiElement element) %7B%0A%0A                if (!PlatformPatterns.psiElement(PhpElementTypes.CLASS_CONSTANT_REFERENCE).accepts(element)) %7B%0A                    return;%0A                %7D%0A%0A                Set%3CString%3E constants = getDeprecatedClassConstants(element);%0A                
 Clas
@@ -1104,18 +1104,18 @@
 eference
- c
+(C
 lassCons
@@ -1132,17 +1132,9 @@
 nce 
-= (ClassC
+c
 onst
@@ -1151,16 +1151,9 @@
 ce) 
-element;
+%7B
 %0A   
@@ -1173,33 +1173,70 @@
 if (
-constants.contains(classC
+DeprecationUtility.isDeprecated(problemsHolder.getProject(), c
 onst
@@ -1251,18 +1251,8 @@
 ence
-.getText()
 )) %7B
@@ -1303,23 +1303,33 @@
 Problem(
-element
+constantReference
 , %22Depre
@@ -1370,29 +1370,16 @@
       %7D%0A
-            %7D
 %0A       
@@ -1383,1249 +1383,102 @@
     
-%7D;%0A
     
-%7D%0A%0A
     
-private Set%3CString%3E getDeprecatedClassConstants(PhpPsiElement element) %7B%0A        Set%3CPsiElement%3E elements = new HashSet%3C%3E();%0A        PsiFile%5B%5D classConstantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), %22ClassConstantMatcher.php%22, GlobalSearchScope.allScope(element.getProject()));%0A        for (PsiFile file : classConstantMatcherFiles) %7B%0A%0A            Collections.addAll(%0A                    elements,%0A                    PsiTreeUtil.collectElements(file, el -%3E PlatformPatterns%0A                            .psiElement(StringLiteralExpression.class)%0A                            .withParent(%0A                                    PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY)%0A                                            .withAncestor(%0A                                                    4,%0A                                                    PlatformPatterns.psiElement(PhpElementTypes.RETURN)%0A                                            )%0A                            )%0A                            .accepts(el)%0A                    )%0A            );%0A        %7D%0A%0A        return elements.stream().map(stringLiteral -%3E ((StringLiteralExpression)stringLiteral).getContents()).collect(Collectors.toSet())
+super.visitPhpClassConstantReference(constantReference);%0A            %7D%0A        %7D
 ;%0A    %7D%0A
 %7D%0A
@@ -1475,10 +1475,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Migrate ClassConstantMatcherInspection to use DeprecationUtility