Skip to content

Commit ab54ac0

Browse files
committed
Add GlobalFunctionCallMatcherInspection
1 parent 2b7eb64 commit ab54ac0

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
@@ -3,6 +3,7 @@
33
import com.cedricziel.idea.typo3.extensionScanner.codeInspection.ClassConstantMatcherInspection;
44
import com.cedricziel.idea.typo3.extensionScanner.codeInspection.ClassNameMatcherInspection;
55
import com.cedricziel.idea.typo3.extensionScanner.codeInspection.ConstantMatcherInspection;
6+
import com.cedricziel.idea.typo3.extensionScanner.codeInspection.FunctionCallMatcherInspection;
67
import com.cedricziel.idea.typo3.tca.codeInspection.InvalidQuantityInspection;
78
import com.intellij.codeInspection.InspectionToolProvider;
89
import org.jetbrains.annotations.NotNull;
@@ -21,6 +22,7 @@ public Class[] getInspectionClasses() {
2122
ClassConstantMatcherInspection.class,
2223
ClassNameMatcherInspection.class,
2324
ConstantMatcherInspection.class,
25+
FunctionCallMatcherInspection.class,
2426
};
2527
}
2628
}
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.FunctionReference;
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 FunctionCallMatcherInspection 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 "Global function 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.FUNCTION_CALL).accepts(element)) {
47+
return;
48+
}
49+
50+
Set<String> constants = getRemovedGlobalFuntions(element);
51+
FunctionReference constantReference = (FunctionReference) element;
52+
if (constants.contains(constantReference.getFQN())) {
53+
problemsHolder.registerProblem(element, "Global function removed with TYPO3 9, consider using an alternative");
54+
}
55+
}
56+
};
57+
}
58+
59+
private Set<String> getRemovedGlobalFuntions(PhpPsiElement element) {
60+
Set<PsiElement> elements = new HashSet<>();
61+
PsiFile[] constantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "FunctionCallMatcher.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)