|
8 | 8 | import com.intellij.codeInspection.ProblemHighlightType;
|
9 | 9 | import com.intellij.codeInspection.ProblemsHolder;
|
10 | 10 | import com.intellij.openapi.project.Project;
|
11 |
| -import com.intellij.psi.PsiElementVisitor; |
12 |
| -import com.jetbrains.php.lang.inspections.PhpInspection; |
13 | 11 | import com.jetbrains.php.lang.psi.elements.Field;
|
14 | 12 | import com.jetbrains.php.lang.psi.elements.PhpClass;
|
15 | 13 | import com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl;
|
16 |
| -import com.jetbrains.php.lang.psi.resolve.types.PhpTypeAnalyserVisitor; |
17 | 14 | import com.magento.idea.magento2uct.inspections.UctProblemsHolder;
|
| 15 | +import com.magento.idea.magento2uct.inspections.php.OverriddenFieldInspection; |
| 16 | +import com.magento.idea.magento2uct.packages.IssueSeverityLevel; |
18 | 17 | import com.magento.idea.magento2uct.packages.SupportedIssue;
|
19 |
| -import com.magento.idea.magento2uct.settings.UctSettingsService; |
20 | 18 | import com.magento.idea.magento2uct.versioning.VersionStateManager;
|
21 | 19 | import org.jetbrains.annotations.NotNull;
|
22 | 20 |
|
23 |
| -public class OverridingDeprecatedConstant extends PhpInspection { |
| 21 | +public class OverridingDeprecatedConstant extends OverriddenFieldInspection { |
24 | 22 |
|
25 | 23 | @Override
|
26 |
| - @SuppressWarnings("PMD.CognitiveComplexity") |
27 |
| - public @NotNull PsiElementVisitor buildVisitor( |
| 24 | + protected void execute( |
| 25 | + final Project project, |
28 | 26 | final @NotNull ProblemsHolder problemsHolder,
|
29 |
| - final boolean isOnTheFly |
| 27 | + final Field field, |
| 28 | + final Field overriddenField, |
| 29 | + final PhpClass parentClass |
30 | 30 | ) {
|
31 |
| - return new PhpTypeAnalyserVisitor() { |
32 |
| - |
33 |
| - @Override |
34 |
| - public void visitPhpField(final Field field) { |
35 |
| - final Project project = field.getProject(); |
36 |
| - final UctSettingsService settings = UctSettingsService.getInstance(project); |
37 |
| - |
38 |
| - if (!settings.isEnabled() || !settings.isIssueLevelSatisfiable( |
39 |
| - SupportedIssue.OVERRIDING_DEPRECATED_CONSTANT.getLevel()) |
40 |
| - ) { |
41 |
| - return; |
42 |
| - } |
43 |
| - super.visitPhpField(field); |
44 |
| - |
45 |
| - if (!(field instanceof ClassConstImpl)) { |
46 |
| - return; |
47 |
| - } |
48 |
| - final PhpClass phpClass = field.getContainingClass(); |
| 31 | + if (!VersionStateManager.getInstance(project).isDeprecated(overriddenField.getFQN())) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + if (problemsHolder instanceof UctProblemsHolder) { |
| 36 | + ((UctProblemsHolder) problemsHolder).setReservedErrorCode( |
| 37 | + SupportedIssue.OVERRIDING_DEPRECATED_CONSTANT.getCode() |
| 38 | + ); |
| 39 | + } |
| 40 | + problemsHolder.registerProblem( |
| 41 | + field, |
| 42 | + SupportedIssue.OVERRIDING_DEPRECATED_CONSTANT.getMessage( |
| 43 | + parentClass.getFQN() |
| 44 | + .concat("::") |
| 45 | + .concat(overriddenField.getName()) |
| 46 | + ), |
| 47 | + ProblemHighlightType.LIKE_DEPRECATED |
| 48 | + ); |
| 49 | + } |
49 | 50 |
|
50 |
| - if (phpClass == null) { |
51 |
| - return; |
52 |
| - } |
53 |
| - PhpClass parentClass = phpClass.getSuperClass(); |
54 |
| - boolean isFound = false; |
55 |
| - final ClassConstImpl constant = (ClassConstImpl) field; |
| 51 | + @Override |
| 52 | + protected boolean isTypeValid(final Field field) { |
| 53 | + return field instanceof ClassConstImpl; |
| 54 | + } |
56 | 55 |
|
57 |
| - while (parentClass != null && !isFound) { |
58 |
| - for (final Field ownField : parentClass.getOwnFields()) { |
59 |
| - if (ownField instanceof ClassConstImpl |
60 |
| - && ownField.getName().equals(constant.getName()) |
61 |
| - && VersionStateManager.getInstance(project) |
62 |
| - .isDeprecated(ownField.getFQN()) |
63 |
| - ) { |
64 |
| - if (problemsHolder instanceof UctProblemsHolder) { |
65 |
| - ((UctProblemsHolder) problemsHolder).setReservedErrorCode( |
66 |
| - SupportedIssue.OVERRIDING_DEPRECATED_CONSTANT.getCode() |
67 |
| - ); |
68 |
| - } |
69 |
| - problemsHolder.registerProblem( |
70 |
| - constant, |
71 |
| - SupportedIssue.OVERRIDING_DEPRECATED_CONSTANT.getMessage( |
72 |
| - parentClass.getFQN() |
73 |
| - .concat("::") |
74 |
| - .concat(ownField.getName()) |
75 |
| - ), |
76 |
| - ProblemHighlightType.LIKE_DEPRECATED |
77 |
| - ); |
78 |
| - isFound = true; |
79 |
| - break; |
80 |
| - } |
81 |
| - } |
82 |
| - parentClass = parentClass.getSuperClass(); |
83 |
| - } |
84 |
| - } |
85 |
| - }; |
| 56 | + @Override |
| 57 | + protected IssueSeverityLevel getSeverityLevel() { |
| 58 | + return SupportedIssue.OVERRIDING_DEPRECATED_CONSTANT.getLevel(); |
86 | 59 | }
|
87 | 60 | }
|
0 commit comments