Skip to content

Commit 02db1b8

Browse files
Merge pull request #779 from Iamwade/uct-inspections-imported-non-api-interface
UCT-723: Added ImportedNonApiInterface inspection
2 parents ec9680c + 7a7ea93 commit 02db1b8

File tree

10 files changed

+117
-14
lines changed

10 files changed

+117
-14
lines changed

resources/META-INF/plugin.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,13 @@
424424
enabledByDefault="false"
425425
level="WARNING"
426426
implementationClass="com.magento.idea.magento2uct.inspections.php.api.ImportedNonApiClass"/>
427+
<localInspection language="PHP" groupPath="UCT"
428+
shortName="ImportedNonApiInterface"
429+
bundle="uct.bundle.inspection" key="inspection.displayName.ImportedNonApiInterface"
430+
groupBundle="uct.bundle.inspection" groupKey="inspection.api.group.name"
431+
enabledByDefault="false"
432+
level="WARNING"
433+
implementationClass="com.magento.idea.magento2uct.inspections.php.api.ImportedNonApiInterface"/>
427434
<!-- \UCT inspection -->
428435

429436
<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>[1322] The imported interface is not marked as an API.</p>
4+
<!-- tooltip end -->
5+
</body>
6+
</html>
-79.1 KB
Binary file not shown.

resources/uct/bundle/inspection.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ inspection.displayName.UsedNonExistentType=Used non-existent Adobe Commerce type
2626
inspection.displayName.UsedNonExistentConstant=Used non-existent Adobe Commerce constant
2727
inspection.displayName.UsedNonExistentProperty=Used non-existent Adobe Commerce property
2828
inspection.displayName.ImportedNonApiClass=Imported non Adobe Commerce API class
29+
inspection.displayName.ImportedNonApiInterface=Imported non Adobe Commerce API interface
2930
customCode.warnings.deprecated.1131=[1131] Extending from @deprecated class ''{0}''
3031
customCode.warnings.deprecated.1132=[1132] Importing @deprecated class ''{0}''
3132
customCode.warnings.deprecated.1134=[1134] Using @deprecated class ''{0}''
@@ -50,3 +51,4 @@ customCode.critical.existence.1110=[1110] Used type ''{0}'' that is removed in t
5051
customCode.critical.existence.1214=[1214] Used constant ''{0}'' that is removed in the ''{1}''
5152
customCode.critical.existence.1514=[1514] Used property ''{0}'' that is removed in the ''{1}''
5253
customCode.critical.api.1122=[1122] Imported class ''{0}'' is not marked as an API
54+
customCode.critical.api.1322=[1322] Imported interface ''{0}'' is not marked as an API

src/com/magento/idea/magento2uct/inspections/php/ImportInspection.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,15 @@ private boolean isInterface(final PhpUse use) {
6767

6868
if (phpReference != null) {
6969
final PsiElement element = phpReference.resolve();
70+
71+
if (element == null) {
72+
return use.getFQN().contains("Interface");
73+
}
74+
7075
return element instanceof PhpClass && ((PhpClass) element).isInterface();
7176
}
7277

73-
return false;
78+
return use.getFQN().contains("Interface");
7479
}
7580
};
7681
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.api;
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.PhpUse;
12+
import com.magento.idea.magento2uct.inspections.UctProblemsHolder;
13+
import com.magento.idea.magento2uct.inspections.php.ImportInspection;
14+
import com.magento.idea.magento2uct.packages.IssueSeverityLevel;
15+
import com.magento.idea.magento2uct.packages.SupportedIssue;
16+
import com.magento.idea.magento2uct.versioning.VersionStateManager;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
public class ImportedNonApiInterface extends ImportInspection {
20+
21+
@Override
22+
protected void execute(
23+
final Project project,
24+
final @NotNull ProblemsHolder problemsHolder,
25+
final PhpUse use,
26+
final boolean isInterface
27+
) {
28+
if (!isInterface || VersionStateManager.getInstance(project).isApi(use.getFQN())) {
29+
return;
30+
}
31+
final String message = SupportedIssue.IMPORTED_NON_API_INTERFACE.getMessage(use.getFQN());
32+
33+
if (problemsHolder instanceof UctProblemsHolder) {
34+
((UctProblemsHolder) problemsHolder).setReservedErrorCode(
35+
SupportedIssue.IMPORTED_NON_API_INTERFACE.getCode()
36+
);
37+
}
38+
problemsHolder.registerProblem(use, message, ProblemHighlightType.WARNING);
39+
}
40+
41+
@Override
42+
protected IssueSeverityLevel getSeverityLevel() {
43+
return SupportedIssue.IMPORTED_NON_API_INTERFACE.getLevel();
44+
}
45+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.magento.idea.magento2uct.bundles.UctInspectionBundle;
1111
import com.magento.idea.magento2uct.inspections.UctProblemsHolder;
1212
import com.magento.idea.magento2uct.inspections.php.api.ImportedNonApiClass;
13+
import com.magento.idea.magento2uct.inspections.php.api.ImportedNonApiInterface;
1314
import com.magento.idea.magento2uct.inspections.php.deprecation.CallingDeprecatedMethod;
1415
import com.magento.idea.magento2uct.inspections.php.deprecation.ExtendingDeprecatedClass;
1516
import com.magento.idea.magento2uct.inspections.php.deprecation.ImplementedDeprecatedInterface;
@@ -184,6 +185,12 @@ public enum SupportedIssue {
184185
IssueSeverityLevel.ERROR,
185186
"customCode.critical.api.1122",
186187
ImportedNonApiClass.class
188+
),
189+
IMPORTED_NON_API_INTERFACE(
190+
1322,
191+
IssueSeverityLevel.ERROR,
192+
"customCode.critical.api.1322",
193+
ImportedNonApiInterface.class
187194
);
188195

189196
private final int code;

src/com/magento/idea/magento2uct/versioning/VersionStateManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private VersionStateManager(final @NotNull Project project) {
110110
existenceStateIndex = new ExistenceStateIndex();
111111
compute(existenceStateIndex);
112112

113-
apiCoverageStateIndex = new ApiCoverageStateIndex(existenceStateIndex.getAllData());
113+
apiCoverageStateIndex = new ApiCoverageStateIndex(existenceStateIndex.getIndexData());
114114
compute(apiCoverageStateIndex);
115115
}
116116

src/com/magento/idea/magento2uct/versioning/indexes/data/ApiCoverageStateIndex.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,12 @@ public ApiCoverageStateIndex() {
4040
/**
4141
* Api coverage state index constructor.
4242
*
43-
* @param existenceVersioningData Map
43+
* @param targetVersionCodebaseData Map
4444
*/
45-
public ApiCoverageStateIndex(final Map<String, Map<String, Boolean>> existenceVersioningData) {
45+
public ApiCoverageStateIndex(final Map<String, Boolean> targetVersionCodebaseData) {
4646
versioningData = new LinkedHashMap<>();
4747
targetVersionData = new HashMap<>();
48-
codebaseSet = new HashMap<>(
49-
VersioningDataOperationsUtil.unionVersionData(existenceVersioningData)
50-
);
48+
codebaseSet = new HashMap<>(targetVersionCodebaseData);
5149
}
5250

5351
/**

src/com/magento/idea/magento2uct/versioning/processors/util/VersioningDataOperationsUtil.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
import com.google.common.collect.Sets;
99
import com.intellij.openapi.util.Pair;
10+
import com.magento.idea.magento2uct.packages.SupportedVersion;
1011
import java.util.HashMap;
12+
import java.util.LinkedList;
1113
import java.util.List;
1214
import java.util.Map;
1315
import java.util.Set;
@@ -62,18 +64,33 @@ public static Map<String, Boolean> getDiff(
6264
*
6365
* @return Pair[Map[String, Boolean], Map[String, String]]
6466
*/
67+
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
6568
public static Pair<Map<String, Boolean>, Map<String, String>> unionVersionDataWithChangelog(
6669
final Map<String, Map<String, Boolean>> versioningData,
6770
final List<String> excludeFromChangelog,
6871
final boolean shouldKeepNew
6972
) {
7073
final Map<String, Boolean> union = new HashMap<>();
71-
final Map<String, Boolean> removedUnion = new HashMap<>();
74+
Map<String, Boolean> removedUnion = new HashMap<>();
7275
final Map<String, String> changelog = new HashMap<>();
76+
final List<String> versions = new LinkedList<>(versioningData.keySet());
77+
78+
versions.sort((key1, key2) -> {
79+
final SupportedVersion version1 = SupportedVersion.getVersion(key1);
80+
final SupportedVersion version2 = SupportedVersion.getVersion(key2);
81+
82+
if (version1 == null || version2 == null) {
83+
return 0;
84+
}
85+
86+
return version1.compareTo(version2);
87+
});
88+
89+
for (final String version : versions) {
90+
final Map<String, Boolean> vData = versioningData.get(version);
7391

74-
for (final Map.Entry<String, Map<String, Boolean>> vData : versioningData.entrySet()) {
7592
final Set<Map.Entry<String, Boolean>> removedSet = Sets.filter(
76-
vData.getValue().entrySet(),
93+
vData.entrySet(),
7794
entry -> !entry.getValue()
7895
);
7996
final Map<String, Boolean> removedData = removedSet.stream().collect(
@@ -85,7 +102,7 @@ public static Pair<Map<String, Boolean>, Map<String, String>> unionVersionDataWi
85102
removedUnion.putAll(removedData);
86103

87104
final Sets.SetView<Map.Entry<String, Boolean>> newDataSet = Sets.difference(
88-
vData.getValue().entrySet(),
105+
vData.entrySet(),
89106
removedSet
90107
);
91108
final Map<String, Boolean> newData = newDataSet.stream().collect(Collectors.toMap(
@@ -94,11 +111,27 @@ public static Pair<Map<String, Boolean>, Map<String, String>> unionVersionDataWi
94111
));
95112
union.putAll(newData);
96113

97-
if (!excludeFromChangelog.contains(vData.getKey())) {
114+
final Sets.SetView<Map.Entry<String, Boolean>> returnedSet = Sets.intersection(
115+
newData.entrySet(),
116+
removedUnion.entrySet()
117+
);
118+
119+
if (!returnedSet.isEmpty()) {
120+
final Sets.SetView<Map.Entry<String, Boolean>> updatedRemovedUnionSet =
121+
Sets.difference(removedUnion.entrySet(), returnedSet);
122+
removedUnion = new HashMap<>(updatedRemovedUnionSet.stream().collect(
123+
Collectors.toMap(
124+
Map.Entry::getKey,
125+
Map.Entry::getValue
126+
)
127+
));
128+
}
129+
130+
if (!excludeFromChangelog.contains(version)) {
98131
if (shouldKeepNew) {
99-
newData.forEach((key, value) -> changelog.put(key, vData.getKey()));
132+
newData.forEach((key, value) -> changelog.put(key, version));
100133
} else {
101-
removedData.forEach((key, value) -> changelog.put(key, vData.getKey()));
134+
removedData.forEach((key, value) -> changelog.put(key, version));
102135
}
103136
}
104137
}

0 commit comments

Comments
 (0)