Skip to content

Commit 2bf50ba

Browse files
authored
Merge pull request #776 from bohdan-harniuk/uct-inspection-using-non-existent-reference
UCT-704: Inspection using non existent reference
2 parents 0b81f44 + 89bed55 commit 2bf50ba

19 files changed

+238
-319
lines changed

resources/META-INF/plugin.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@
396396
enabledByDefault="false"
397397
level="ERROR"
398398
implementationClass="com.magento.idea.magento2uct.inspections.php.existence.CalledNonExistentMethod"/>
399+
<localInspection language="PHP" groupPath="UCT"
400+
shortName="UsedNonExistentType"
401+
bundle="uct.bundle.inspection" key="inspection.displayName.UsedNonExistentType"
402+
groupBundle="uct.bundle.inspection" groupKey="inspection.existence.group.name"
403+
enabledByDefault="false"
404+
level="ERROR"
405+
implementationClass="com.magento.idea.magento2uct.inspections.php.existence.UsedNonExistentType"/>
399406
<!-- \UCT inspection -->
400407

401408
<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>[1110] The used type is no longer present in the codebase.</p>
4+
<!-- tooltip end -->
5+
</body>
6+
</html>

resources/uct/bundle/inspection.properties

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ inspection.displayName.ExtendedNonExistentClass=Extended non-existent Adobe Comm
2121
inspection.displayName.OverriddenNonExistentConstant=Overridden non-existent Adobe Commerce constant
2222
inspection.displayName.OverriddenNonExistentProperty=Overridden non-existent Adobe Commerce property
2323
inspection.displayName.CalledNonExistentMethod=Call non-existent Adobe Commerce method
24+
inspection.displayName.UsedNonExistentType=Used non-existent Adobe Commerce type
2425
customCode.warnings.deprecated.1131=[1131] Extending from @deprecated class ''{0}''
2526
customCode.warnings.deprecated.1132=[1132] Importing @deprecated class ''{0}''
2627
customCode.warnings.deprecated.1134=[1134] Using @deprecated class ''{0}''
@@ -33,19 +34,12 @@ customCode.warnings.deprecated.1338=[1338] Implemented @deprecated interface ''{
3334
customCode.warnings.deprecated.1439=[1439] Call @deprecated method ''{0}''
3435
customCode.warnings.deprecated.1534=[1534] Using @deprecated property ''{0}''
3536
customCode.warnings.deprecated.1535=[1535] Overriding @deprecated property ''{0}''
36-
customCode.critical.existence.1112=[1112] Imported non-existent class ''{0}''
37-
customCode.critical.existence.1112.changelog=[1112] Imported class ''{0}'' that is removed in the ''{1}''
38-
customCode.critical.existence.1312=[1312] Imported non-existent interface ''{0}''
39-
customCode.critical.existence.1312.changelog=[1312] Imported interface ''{0}'' that is removed in the ''{1}''
40-
customCode.critical.existence.1317=[1317] Inherited non-existent interface ''{0}''
41-
customCode.critical.existence.1317.changelog=[1317] Inherited interface ''{0}'' that is removed in the ''{1}''
42-
customCode.critical.existence.1318=[1318] Implemented non-existent interface ''{0}''
43-
customCode.critical.existence.1318.changelog=[1318] Implemented interface ''{0}'' that is removed in the ''{1}''
44-
customCode.critical.existence.1111=[1111] Extended non-existent class ''{0}''
45-
customCode.critical.existence.1111.changelog=[1111] Extended class ''{0}'' that is removed in the ''{1}''
46-
customCode.critical.existence.1215=[1215] Overridden non-existent constant ''{0}''
47-
customCode.critical.existence.1215.changelog=[1215] Overridden constant ''{0}'' that is removed in the ''{1}''
48-
customCode.critical.existence.1515=[1515] Overridden non-existent property ''{0}''
49-
customCode.critical.existence.1515.changelog=[1515] Overridden property ''{0}'' that is removed in the ''{1}''
50-
customCode.critical.existence.1410=[1410] Called non-existent method ''{0}''
51-
customCode.critical.existence.1410.changelog=[1410] Called method ''{0}'' that is removed in the ''{1}''
37+
customCode.critical.existence.1112=[1112] Imported class ''{0}'' that is removed in the ''{1}''
38+
customCode.critical.existence.1312=[1312] Imported interface ''{0}'' that is removed in the ''{1}''
39+
customCode.critical.existence.1317=[1317] Inherited interface ''{0}'' that is removed in the ''{1}''
40+
customCode.critical.existence.1318=[1318] Implemented interface ''{0}'' that is removed in the ''{1}''
41+
customCode.critical.existence.1111=[1111] Extended class ''{0}'' that is removed in the ''{1}''
42+
customCode.critical.existence.1215=[1215] Overridden constant ''{0}'' that is removed in the ''{1}''
43+
customCode.critical.existence.1515=[1515] Overridden property ''{0}'' that is removed in the ''{1}''
44+
customCode.critical.existence.1410=[1410] Called method ''{0}'' that is removed in the ''{1}''
45+
customCode.critical.existence.1110=[1110] Used type ''{0}'' that is removed in the ''{1}''
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.PsiElement;
11+
import com.intellij.psi.PsiElementVisitor;
12+
import com.jetbrains.php.lang.inspections.PhpInspection;
13+
import com.jetbrains.php.lang.psi.elements.ClassReference;
14+
import com.jetbrains.php.lang.psi.elements.Method;
15+
import com.jetbrains.php.lang.psi.elements.PhpClass;
16+
import com.jetbrains.php.lang.psi.resolve.types.PhpTypeAnalyserVisitor;
17+
import com.magento.idea.magento2plugin.magento.packages.MagentoPhpClass;
18+
import com.magento.idea.magento2uct.packages.IssueSeverityLevel;
19+
import com.magento.idea.magento2uct.settings.UctSettingsService;
20+
import org.jetbrains.annotations.NotNull;
21+
22+
public abstract class UsedTypeInspection extends PhpInspection {
23+
24+
@Override
25+
public @NotNull PsiElementVisitor buildVisitor(
26+
final @NotNull ProblemsHolder problemsHolder,
27+
final boolean isOnTheFly
28+
) {
29+
return new PhpTypeAnalyserVisitor() {
30+
31+
@Override
32+
public void visitPhpClassReference(final ClassReference reference) {
33+
final Project project = reference.getProject();
34+
final UctSettingsService settings = UctSettingsService.getInstance(project);
35+
36+
if (!settings.isEnabled()
37+
|| !settings.isIssueLevelSatisfiable(getSeverityLevel())) {
38+
return;
39+
}
40+
PsiElement resolved = reference.resolve();
41+
42+
if (resolved instanceof Method
43+
&& MagentoPhpClass.CONSTRUCT_METHOD_NAME
44+
.equals(((Method) resolved).getName())) {
45+
resolved = ((Method) resolved).getContainingClass();
46+
}
47+
48+
if (!(resolved instanceof PhpClass)) {
49+
return;
50+
}
51+
final PhpClass phpClass = (PhpClass) resolved;
52+
53+
execute(project, problemsHolder, phpClass, reference);
54+
}
55+
};
56+
}
57+
58+
/**
59+
* Implement this method to specify inspection logic.
60+
*
61+
* @param project Project
62+
* @param problemsHolder ProblemsHolder
63+
* @param phpClass PhpClass
64+
* @param reference ClassReference
65+
*/
66+
protected abstract void execute(
67+
final Project project,
68+
final @NotNull ProblemsHolder problemsHolder,
69+
final PhpClass phpClass,
70+
final ClassReference reference
71+
);
72+
73+
/**
74+
* Implement this method to specify issue severity level for target inspection.
75+
*
76+
* @return IssueSeverityLevel
77+
*/
78+
protected abstract IssueSeverityLevel getSeverityLevel();
79+
}

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

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,27 @@
77

88
import com.intellij.codeInspection.ProblemHighlightType;
99
import com.intellij.codeInspection.ProblemsHolder;
10+
import com.intellij.openapi.project.Project;
1011
import com.jetbrains.php.lang.psi.elements.ClassReference;
11-
import com.jetbrains.php.lang.psi.elements.Field;
12+
import com.jetbrains.php.lang.psi.elements.PhpClass;
1213
import com.magento.idea.magento2uct.inspections.UctProblemsHolder;
14+
import com.magento.idea.magento2uct.inspections.php.UsedTypeInspection;
1315
import com.magento.idea.magento2uct.packages.IssueSeverityLevel;
1416
import com.magento.idea.magento2uct.packages.SupportedIssue;
17+
import com.magento.idea.magento2uct.versioning.VersionStateManager;
1518
import org.jetbrains.annotations.NotNull;
1619

17-
public class UsingDeprecatedClass extends UsingDeprecatedType {
20+
public class UsingDeprecatedClass extends UsedTypeInspection {
1821

1922
@Override
20-
protected void registerProblem(
23+
protected void execute(
24+
final Project project,
2125
final @NotNull ProblemsHolder problemsHolder,
22-
final Field field,
23-
final String fieldClass,
24-
final boolean isInterface
26+
final PhpClass phpClass,
27+
final ClassReference reference
2528
) {
26-
if (isInterface) {
27-
return;
28-
}
29-
if (problemsHolder instanceof UctProblemsHolder) {
30-
((UctProblemsHolder) problemsHolder).setReservedErrorCode(
31-
SupportedIssue.USING_DEPRECATED_CLASS.getCode()
32-
);
33-
}
34-
problemsHolder.registerProblem(
35-
field,
36-
SupportedIssue.USING_DEPRECATED_CLASS.getMessage(fieldClass),
37-
ProblemHighlightType.LIKE_DEPRECATED
38-
);
39-
}
40-
41-
@Override
42-
protected void registerReferenceProblem(
43-
final @NotNull ProblemsHolder problemsHolder,
44-
final ClassReference reference,
45-
final String deprecatedType,
46-
final boolean isInterface
47-
) {
48-
if (isInterface) {
29+
if (phpClass.isInterface()
30+
|| !VersionStateManager.getInstance(project).isDeprecated(phpClass.getFQN())) {
4931
return;
5032
}
5133
if (problemsHolder instanceof UctProblemsHolder) {
@@ -55,7 +37,7 @@ protected void registerReferenceProblem(
5537
}
5638
problemsHolder.registerProblem(
5739
reference,
58-
SupportedIssue.USING_DEPRECATED_CLASS.getMessage(deprecatedType),
40+
SupportedIssue.USING_DEPRECATED_CLASS.getMessage(phpClass.getFQN()),
5941
ProblemHighlightType.LIKE_DEPRECATED
6042
);
6143
}

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

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,38 @@
77

88
import com.intellij.codeInspection.ProblemHighlightType;
99
import com.intellij.codeInspection.ProblemsHolder;
10+
import com.intellij.openapi.project.Project;
1011
import com.jetbrains.php.lang.psi.elements.ClassReference;
11-
import com.jetbrains.php.lang.psi.elements.Field;
12+
import com.jetbrains.php.lang.psi.elements.PhpClass;
1213
import com.magento.idea.magento2uct.inspections.UctProblemsHolder;
14+
import com.magento.idea.magento2uct.inspections.php.UsedTypeInspection;
1315
import com.magento.idea.magento2uct.packages.IssueSeverityLevel;
1416
import com.magento.idea.magento2uct.packages.SupportedIssue;
17+
import com.magento.idea.magento2uct.versioning.VersionStateManager;
1518
import org.jetbrains.annotations.NotNull;
1619

17-
public class UsingDeprecatedInterface extends UsingDeprecatedType {
20+
public class UsingDeprecatedInterface extends UsedTypeInspection {
1821

1922
@Override
20-
protected void registerProblem(
23+
protected void execute(
24+
final Project project,
2125
final @NotNull ProblemsHolder problemsHolder,
22-
final Field field,
23-
final String fieldInterface,
24-
final boolean isInterface
26+
final PhpClass phpClass,
27+
final ClassReference reference
2528
) {
26-
if (!isInterface) {
29+
if (!phpClass.isInterface()
30+
|| !VersionStateManager.getInstance(project).isDeprecated(phpClass.getFQN())) {
2731
return;
2832
}
29-
if (problemsHolder instanceof UctProblemsHolder) {
30-
((UctProblemsHolder) problemsHolder).setReservedErrorCode(
31-
SupportedIssue.USING_DEPRECATED_INTERFACE.getCode()
32-
);
33-
}
34-
problemsHolder.registerProblem(
35-
field,
36-
SupportedIssue.USING_DEPRECATED_INTERFACE.getMessage(fieldInterface),
37-
ProblemHighlightType.LIKE_DEPRECATED
38-
);
39-
}
4033

41-
@Override
42-
protected void registerReferenceProblem(
43-
final @NotNull ProblemsHolder problemsHolder,
44-
final ClassReference reference,
45-
final String deprecatedType,
46-
final boolean isInterface
47-
) {
48-
if (!isInterface) {
49-
return;
50-
}
5134
if (problemsHolder instanceof UctProblemsHolder) {
5235
((UctProblemsHolder) problemsHolder).setReservedErrorCode(
5336
SupportedIssue.USING_DEPRECATED_INTERFACE.getCode()
5437
);
5538
}
5639
problemsHolder.registerProblem(
5740
reference,
58-
SupportedIssue.USING_DEPRECATED_INTERFACE.getMessage(deprecatedType),
41+
SupportedIssue.USING_DEPRECATED_INTERFACE.getMessage(phpClass.getFQN()),
5942
ProblemHighlightType.LIKE_DEPRECATED
6043
);
6144
}

0 commit comments

Comments
 (0)