Skip to content

Commit 284eb37

Browse files
committed
Add inspections for deprecated class constants and removed classes in TYPO3 v9
1 parent e0e5afa commit 284eb37

File tree

3 files changed

+172
-1
lines changed

3 files changed

+172
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.cedricziel.idea.typo3.codeInspection;
22

3+
import com.cedricziel.idea.typo3.extensionScanner.codeInspection.ClassConstantMatcherInspection;
4+
import com.cedricziel.idea.typo3.extensionScanner.codeInspection.ClassNameMatcherInspection;
35
import com.cedricziel.idea.typo3.tca.codeInspection.InvalidQuantityInspection;
46
import com.intellij.codeInspection.InspectionToolProvider;
57
import org.jetbrains.annotations.NotNull;
@@ -13,7 +15,10 @@ public Class[] getInspectionClasses() {
1315
MissingColumnTypeInspection.class,
1416
MissingRenderTypeInspection.class,
1517
MissingTableInspection.class,
16-
InvalidQuantityInspection.class
18+
InvalidQuantityInspection.class,
19+
// Extension Scanner
20+
ClassConstantMatcherInspection.class,
21+
ClassNameMatcherInspection.class,
1722
};
1823
}
1924
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.ClassConstantReference;
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 ClassConstantMatcherInspection 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 "Deprecated class constant";
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_CONSTANT_REFERENCE).accepts(element)) {
47+
return;
48+
}
49+
50+
Set<String> constants = getDeprecatedClassConstants(element);
51+
ClassConstantReference classConstantReference = (ClassConstantReference) element;
52+
if (constants.contains(classConstantReference.getText())) {
53+
problemsHolder.registerProblem(element, "Deprecated class constant");
54+
}
55+
}
56+
};
57+
}
58+
59+
private Set<String> getDeprecatedClassConstants(PhpPsiElement element) {
60+
Set<PsiElement> elements = new HashSet<>();
61+
PsiFile[] classConstantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "ClassConstantMatcher.php", GlobalSearchScope.allScope(element.getProject()));
62+
for (PsiFile file : classConstantMatcherFiles) {
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().map(stringLiteral -> ((StringLiteralExpression)stringLiteral).getContents()).collect(Collectors.toSet());
81+
}
82+
}
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.ClassReference;
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 ClassNameMatcherInspection 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 "Class 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 = getDeprecatedClasses(element);
51+
ClassReference classReference = (ClassReference) element;
52+
if (constants.contains(classReference.getFQN())) {
53+
problemsHolder.registerProblem(element, "Class removed with TYPO3 9, consider using an alternative");
54+
}
55+
}
56+
};
57+
}
58+
59+
private Set<String> getDeprecatedClasses(PhpPsiElement element) {
60+
Set<PsiElement> elements = new HashSet<>();
61+
PsiFile[] classNameMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "ClassNameMatcher.php", GlobalSearchScope.allScope(element.getProject()));
62+
for (PsiFile file : classNameMatcherFiles) {
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)