Skip to content

Commit 48f7c34

Browse files
authored
Merge pull request #759 from bohdan-harniuk/uct-inspection-extending-from-non-existent-class
UCT-705: Developed ExtendedNonExistentClass inspection
2 parents fd54d22 + 54c767d commit 48f7c34

File tree

7 files changed

+186
-50
lines changed

7 files changed

+186
-50
lines changed

resources/META-INF/plugin.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,13 @@
368368
enabledByDefault="false"
369369
level="ERROR"
370370
implementationClass="com.magento.idea.magento2uct.inspections.php.existence.ImplementedNonExistentInterface"/>
371+
<localInspection language="PHP" groupPath="UCT"
372+
shortName="ExtendedNonExistentClass"
373+
bundle="uct.bundle.inspection" key="inspection.displayName.ExtendedNonExistentClass"
374+
groupBundle="uct.bundle.inspection" groupKey="inspection.existence.group.name"
375+
enabledByDefault="false"
376+
level="ERROR"
377+
implementationClass="com.magento.idea.magento2uct.inspections.php.existence.ExtendedNonExistentClass"/>
371378
<!-- \UCT inspection -->
372379

373380
<internalFileTemplate name="Magento Composer JSON"/>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<body>
3+
<p>[1111] The extended class is no longer present in the codebase.</p>
4+
<!-- tooltip end -->
5+
</body>
6+
</html>

resources/uct/bundle/inspection.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ inspection.displayName.ImportingNonExistentClass=Importing non-existent Adobe Co
1717
inspection.displayName.ImportingNonExistentInterface=Importing non-existent Adobe Commerce interface
1818
inspection.displayName.InheritedNonExistentInterface=Inherited non-existent Adobe Commerce interface
1919
inspection.displayName.ImplementedNonExistentInterface=Implemented non-existent Adobe Commerce interface
20+
inspection.displayName.ExtendedNonExistentClass=Extended non-existent Adobe Commerce class
2021
customCode.warnings.deprecated.1131=[1131] Extending from @deprecated class ''{0}''
2122
customCode.warnings.deprecated.1132=[1132] Importing @deprecated class ''{0}''
2223
customCode.warnings.deprecated.1134=[1134] Using @deprecated class ''{0}''
@@ -37,3 +38,5 @@ customCode.critical.existence.1317=[1317] Inherited non-existent interface ''{0}
3738
customCode.critical.existence.1317.changelog=[1317] Inherited interface ''{0}'' that is removed in the ''{1}''
3839
customCode.critical.existence.1318=[1318] Implemented non-existent interface ''{0}''
3940
customCode.critical.existence.1318.changelog=[1318] Implemented interface ''{0}'' that is removed in the ''{1}''
41+
customCode.critical.existence.1111=[1111] Extended non-existent class ''{0}''
42+
customCode.critical.existence.1111.changelog=[1111] Extended class ''{0}'' that is removed in the ''{1}''
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2uct.inspections.php;
7+
8+
import com.intellij.codeInspection.ProblemsHolder;
9+
import com.intellij.openapi.project.Project;
10+
import com.intellij.psi.PsiElementVisitor;
11+
import com.jetbrains.php.lang.inspections.PhpInspection;
12+
import com.jetbrains.php.lang.psi.elements.ExtendsList;
13+
import com.jetbrains.php.lang.psi.elements.PhpClass;
14+
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;
15+
import com.magento.idea.magento2uct.packages.IssueSeverityLevel;
16+
import com.magento.idea.magento2uct.settings.UctSettingsService;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
public abstract class ExtendInspection extends PhpInspection {
20+
21+
@Override
22+
public @NotNull
23+
PsiElementVisitor buildVisitor(
24+
final @NotNull ProblemsHolder problemsHolder,
25+
final boolean isOnTheFly
26+
) {
27+
return new PhpElementVisitor() {
28+
29+
@Override
30+
public void visitPhpClass(final PhpClass clazz) {
31+
final Project project = clazz.getProject();
32+
final UctSettingsService settings = UctSettingsService.getInstance(project);
33+
34+
if (clazz.isInterface()
35+
|| !settings.isEnabled()
36+
|| !settings.isIssueLevelSatisfiable(getSeverityLevel())
37+
) {
38+
return;
39+
}
40+
final PhpClass parentClass = clazz.getSuperClass();
41+
42+
if (parentClass == null || parentClass.isInterface()) {
43+
return;
44+
}
45+
final ExtendsList extendsList = clazz.getExtendsList();
46+
47+
execute(project, problemsHolder, parentClass, extendsList);
48+
}
49+
};
50+
}
51+
52+
/**
53+
* Implement this method to specify inspection logic.
54+
*
55+
* @param project Project
56+
* @param problemsHolder ProblemsHolder
57+
* @param parentClass PhpClass
58+
* @param childExtends ExtendsList
59+
*/
60+
protected abstract void execute(
61+
final Project project,
62+
final @NotNull ProblemsHolder problemsHolder,
63+
final PhpClass parentClass,
64+
final ExtendsList childExtends
65+
);
66+
67+
/**
68+
* Implement this method to specify issue severity level for target inspection.
69+
*
70+
* @return IssueSeverityLevel
71+
*/
72+
protected abstract IssueSeverityLevel getSeverityLevel();
73+
}

src/com/magento/idea/magento2uct/inspections/php/deprecation/ExtendingDeprecatedClass.java

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,50 @@
88
import com.intellij.codeInspection.ProblemHighlightType;
99
import com.intellij.codeInspection.ProblemsHolder;
1010
import com.intellij.openapi.project.Project;
11-
import com.intellij.psi.PsiElementVisitor;
12-
import com.jetbrains.php.lang.inspections.PhpInspection;
1311
import com.jetbrains.php.lang.psi.elements.ClassReference;
1412
import com.jetbrains.php.lang.psi.elements.ExtendsList;
1513
import com.jetbrains.php.lang.psi.elements.PhpClass;
16-
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;
1714
import com.magento.idea.magento2uct.inspections.UctProblemsHolder;
15+
import com.magento.idea.magento2uct.inspections.php.ExtendInspection;
16+
import com.magento.idea.magento2uct.packages.IssueSeverityLevel;
1817
import com.magento.idea.magento2uct.packages.SupportedIssue;
19-
import com.magento.idea.magento2uct.settings.UctSettingsService;
2018
import com.magento.idea.magento2uct.versioning.VersionStateManager;
2119
import org.jetbrains.annotations.NotNull;
2220

23-
public class ExtendingDeprecatedClass extends PhpInspection {
21+
public class ExtendingDeprecatedClass extends ExtendInspection {
2422

2523
@Override
26-
@SuppressWarnings({"PMD.CognitiveComplexity", "PMD.AvoidDeeplyNestedIfStmts"})
27-
public @NotNull PsiElementVisitor buildVisitor(
24+
protected void execute(
25+
final Project project,
2826
final @NotNull ProblemsHolder problemsHolder,
29-
final boolean isOnTheFly
27+
final PhpClass parentClass,
28+
final ExtendsList childExtends
3029
) {
31-
return new PhpElementVisitor() {
32-
33-
@Override
34-
public void visitPhpClass(final PhpClass clazz) {
35-
final Project project = clazz.getProject();
36-
final UctSettingsService settings = UctSettingsService.getInstance(project);
37-
38-
if (!settings.isEnabled() || !settings.isIssueLevelSatisfiable(
39-
SupportedIssue.EXTENDING_DEPRECATED_CLASS.getLevel())
40-
) {
41-
return;
42-
}
43-
PhpClass parentClass = clazz.getSuperClass();
44-
45-
if (parentClass == null) {
46-
return;
47-
}
48-
final ExtendsList list = clazz.getExtendsList();
49-
final String parentClassFqn = parentClass.getFQN();
50-
51-
while (parentClass != null) {
52-
if (VersionStateManager.getInstance(project)
53-
.isDeprecated(parentClass.getFQN())) {
54-
for (final ClassReference classReference : list.getReferenceElements()) {
55-
if (parentClassFqn.equals(classReference.getFQN())) {
56-
if (problemsHolder instanceof UctProblemsHolder) {
57-
((UctProblemsHolder) problemsHolder).setReservedErrorCode(
58-
SupportedIssue.EXTENDING_DEPRECATED_CLASS.getCode()
59-
);
60-
}
61-
problemsHolder.registerProblem(
62-
classReference,
63-
SupportedIssue.EXTENDING_DEPRECATED_CLASS.getMessage(
64-
parentClass.getFQN()
65-
),
66-
ProblemHighlightType.LIKE_DEPRECATED
67-
);
68-
}
69-
}
70-
}
71-
parentClass = parentClass.getSuperClass();
30+
final String parentFqn = parentClass.getFQN();
31+
32+
if (!VersionStateManager.getInstance(project).isDeprecated(parentFqn)) {
33+
return;
34+
}
35+
for (final ClassReference classReference : childExtends.getReferenceElements()) {
36+
if (parentFqn.equals(classReference.getFQN())) {
37+
if (problemsHolder instanceof UctProblemsHolder) {
38+
((UctProblemsHolder) problemsHolder).setReservedErrorCode(
39+
SupportedIssue.EXTENDING_DEPRECATED_CLASS.getCode()
40+
);
7241
}
42+
problemsHolder.registerProblem(
43+
classReference,
44+
SupportedIssue.EXTENDING_DEPRECATED_CLASS.getMessage(
45+
parentClass.getFQN()
46+
),
47+
ProblemHighlightType.LIKE_DEPRECATED
48+
);
7349
}
74-
};
50+
}
51+
}
52+
53+
@Override
54+
protected IssueSeverityLevel getSeverityLevel() {
55+
return SupportedIssue.EXTENDING_DEPRECATED_CLASS.getLevel();
7556
}
7657
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2uct.inspections.php.existence;
7+
8+
import com.intellij.codeInspection.ProblemHighlightType;
9+
import com.intellij.codeInspection.ProblemsHolder;
10+
import com.intellij.openapi.project.Project;
11+
import com.jetbrains.php.lang.psi.elements.ClassReference;
12+
import com.jetbrains.php.lang.psi.elements.ExtendsList;
13+
import com.jetbrains.php.lang.psi.elements.PhpClass;
14+
import com.magento.idea.magento2uct.inspections.UctProblemsHolder;
15+
import com.magento.idea.magento2uct.inspections.php.ExtendInspection;
16+
import com.magento.idea.magento2uct.packages.IssueSeverityLevel;
17+
import com.magento.idea.magento2uct.packages.SupportedIssue;
18+
import com.magento.idea.magento2uct.versioning.VersionStateManager;
19+
import org.jetbrains.annotations.NotNull;
20+
21+
public class ExtendedNonExistentClass extends ExtendInspection {
22+
23+
@Override
24+
protected void execute(
25+
final Project project,
26+
final @NotNull ProblemsHolder problemsHolder,
27+
final PhpClass parentClass,
28+
final ExtendsList childExtends
29+
) {
30+
final String parentFqn = parentClass.getFQN();
31+
32+
if (VersionStateManager.getInstance(project).isExists(parentFqn)) {
33+
return;
34+
}
35+
final String removedIn = VersionStateManager.getInstance(project).getRemovedInVersion();
36+
final String message = removedIn.isEmpty()
37+
? SupportedIssue.EXTENDED_NON_EXISTENT_CLASS.getMessage(parentFqn)
38+
: SupportedIssue.EXTENDED_NON_EXISTENT_CLASS.getChangelogMessage(
39+
parentFqn, removedIn);
40+
41+
if (problemsHolder instanceof UctProblemsHolder) {
42+
((UctProblemsHolder) problemsHolder).setReservedErrorCode(
43+
SupportedIssue.EXTENDED_NON_EXISTENT_CLASS.getCode()
44+
);
45+
}
46+
47+
for (final ClassReference reference : childExtends.getReferenceElements()) {
48+
if (parentFqn.equals(reference.getFQN())) {
49+
problemsHolder.registerProblem(reference, message, ProblemHighlightType.ERROR);
50+
}
51+
}
52+
}
53+
54+
@Override
55+
protected IssueSeverityLevel getSeverityLevel() {
56+
return SupportedIssue.EXTENDED_NON_EXISTENT_CLASS.getLevel();
57+
}
58+
}

src/com/magento/idea/magento2uct/packages/SupportedIssue.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.magento.idea.magento2uct.inspections.php.deprecation.UsingDeprecatedConstant;
2222
import com.magento.idea.magento2uct.inspections.php.deprecation.UsingDeprecatedInterface;
2323
import com.magento.idea.magento2uct.inspections.php.deprecation.UsingDeprecatedProperty;
24+
import com.magento.idea.magento2uct.inspections.php.existence.ExtendedNonExistentClass;
2425
import com.magento.idea.magento2uct.inspections.php.existence.ImplementedNonExistentInterface;
2526
import com.magento.idea.magento2uct.inspections.php.existence.ImportingNonExistentClass;
2627
import com.magento.idea.magento2uct.inspections.php.existence.ImportingNonExistentInterface;
@@ -131,6 +132,13 @@ public enum SupportedIssue {
131132
"customCode.critical.existence.1318",
132133
ImplementedNonExistentInterface.class,
133134
"customCode.critical.existence.1318.changelog"
135+
),
136+
EXTENDED_NON_EXISTENT_CLASS(
137+
1111,
138+
IssueSeverityLevel.CRITICAL,
139+
"customCode.critical.existence.1111",
140+
ExtendedNonExistentClass.class,
141+
"customCode.critical.existence.1111.changelog"
134142
);
135143

136144
private final int code;

0 commit comments

Comments
 (0)