Skip to content

Commit 2b7eb64

Browse files
committed
Add ConstantMatcherExpression
1 parent 284eb37 commit 2b7eb64

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/main/java/com/cedricziel/idea/typo3/codeInspection/TYPO3InspectionToolProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.cedricziel.idea.typo3.extensionScanner.codeInspection.ClassConstantMatcherInspection;
44
import com.cedricziel.idea.typo3.extensionScanner.codeInspection.ClassNameMatcherInspection;
5+
import com.cedricziel.idea.typo3.extensionScanner.codeInspection.ConstantMatcherInspection;
56
import com.cedricziel.idea.typo3.tca.codeInspection.InvalidQuantityInspection;
67
import com.intellij.codeInspection.InspectionToolProvider;
78
import org.jetbrains.annotations.NotNull;
@@ -19,6 +20,7 @@ public Class[] getInspectionClasses() {
1920
// Extension Scanner
2021
ClassConstantMatcherInspection.class,
2122
ClassNameMatcherInspection.class,
23+
ConstantMatcherInspection.class,
2224
};
2325
}
2426
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)