Skip to content

Commit e1e8970

Browse files
committed
Add quickfix to migrate legacy classes
1 parent 0b7ac60 commit e1e8970

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.cedricziel.idea.typo3.codeInspection;
2+
3+
import com.cedricziel.idea.typo3.codeInspection.quickfix.LegacyClassesForIdeQuickFix;
4+
import com.cedricziel.idea.typo3.index.php.LegacyClassesForIDEIndex;
5+
import com.intellij.codeInsight.daemon.GroupNames;
6+
import com.intellij.codeInspection.ProblemHighlightType;
7+
import com.intellij.codeInspection.ProblemsHolder;
8+
import com.intellij.psi.PsiElementVisitor;
9+
import com.jetbrains.php.lang.inspections.PhpInspection;
10+
import com.jetbrains.php.lang.psi.elements.ClassConstantReference;
11+
import com.jetbrains.php.lang.psi.elements.ClassReference;
12+
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;
13+
import org.jetbrains.annotations.Nls;
14+
import org.jetbrains.annotations.NotNull;
15+
16+
public class LegacyClassesForIDEInspection extends PhpInspection {
17+
@Nls
18+
@NotNull
19+
@Override
20+
public String getDisplayName() {
21+
return "Legacy class used";
22+
}
23+
24+
@Nls
25+
@NotNull
26+
@Override
27+
public String getGroupDisplayName() {
28+
return GroupNames.BUGS_GROUP_NAME;
29+
}
30+
31+
@NotNull
32+
@Override
33+
public String getShortName() {
34+
return "LegacyClassesForIDE";
35+
}
36+
37+
@NotNull
38+
@Override
39+
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
40+
return new PhpElementVisitor() {
41+
@Override
42+
public void visitPhpClassReference(ClassReference classReference) {
43+
if (classReference.getFQN() != null && LegacyClassesForIDEIndex.isLegacyClass(classReference.getProject(), classReference.getFQN())) {
44+
problemsHolder.registerProblem(classReference, "Legacy class usage", ProblemHighlightType.LIKE_DEPRECATED, new LegacyClassesForIdeQuickFix());
45+
}
46+
47+
super.visitPhpClassReference(classReference);
48+
}
49+
50+
@Override
51+
public void visitPhpClassConstantReference(ClassConstantReference constantReference) {
52+
super.visitPhpClassConstantReference(constantReference);
53+
}
54+
};
55+
}
56+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public Class[] getInspectionClasses() {
1818
MissingRenderTypeInspection.class,
1919
MissingTableInspection.class,
2020
InvalidQuantityInspection.class,
21+
// Code Migration
22+
LegacyClassesForIDEInspection.class,
2123
// Extension Scanner
2224
ClassConstantMatcherInspection.class,
2325
ClassNameMatcherInspection.class,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.cedricziel.idea.typo3.codeInspection.quickfix;
2+
3+
import com.cedricziel.idea.typo3.index.php.LegacyClassesForIDEIndex;
4+
import com.intellij.codeInspection.ProblemDescriptor;
5+
import com.intellij.openapi.project.DumbService;
6+
import com.intellij.openapi.project.Project;
7+
import com.intellij.psi.PsiElement;
8+
import com.intellij.util.IncorrectOperationException;
9+
import com.jetbrains.php.lang.inspections.quickfix.PhpQuickFixBase;
10+
import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
11+
import com.jetbrains.php.lang.psi.elements.ClassReference;
12+
import org.jetbrains.annotations.Nls;
13+
import org.jetbrains.annotations.NotNull;
14+
15+
public class LegacyClassesForIdeQuickFix extends PhpQuickFixBase {
16+
@Nls
17+
@NotNull
18+
@Override
19+
public String getFamilyName() {
20+
return getName();
21+
}
22+
23+
@Nls
24+
@NotNull
25+
@Override
26+
public String getName() {
27+
return "Migrate class usage";
28+
}
29+
30+
@Override
31+
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
32+
33+
PsiElement psiElement = descriptor.getPsiElement();
34+
if (DumbService.isDumb(project)) {
35+
showIsInDumpModeMessage(project, psiElement);
36+
return;
37+
}
38+
39+
if (psiElement instanceof ClassReference) {
40+
ClassReference classReference = (ClassReference) psiElement;
41+
String fqn = classReference.getFQN();
42+
if (fqn != null) {
43+
String replacementFQN = LegacyClassesForIDEIndex.findReplacementClass(project, fqn);
44+
if (replacementFQN != null) {
45+
try {
46+
classReference.replace(PhpPsiElementFactory.createClassReference(project, replacementFQN));
47+
} catch (IncorrectOperationException e) {
48+
showErrorMessage(project, "Could not replace class reference", psiElement);
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<body>
3+
<p>The class used is aliased for backwards compatibility. Migrate the usage to be on top of the changes.</p>
4+
<!-- tooltip end -->
5+
<p></p>
6+
</body>
7+
</html>

0 commit comments

Comments
 (0)