Skip to content

Commit fe89e9b

Browse files
Static fixes, enhanced version state manager
1 parent b47e5ed commit fe89e9b

File tree

9 files changed

+82
-42
lines changed

9 files changed

+82
-42
lines changed

src/com/magento/idea/magento2uct/execution/GenerateUctReportCommand.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ public void execute() {
157157
summary.trackProcessFinished();
158158
summary.setProcessedModules(scanner.getModuleCount());
159159
outputUtil.printSummary(summary);
160+
161+
if (summary.getProcessedModules() == 0) {
162+
process.destroyProcess();
163+
return;
164+
}
160165
reportBuilder.addSummary(summary);
161166
});
162167

@@ -173,8 +178,8 @@ public void execute() {
173178
psiDocumentManager.commitDocument(document);
174179
}
175180
outputUtil.printReportFile(report.getVirtualFile().getPath());
176-
process.destroyProcess();
177181
}
182+
process.destroyProcess();
178183
});
179184
});
180185
}

src/com/magento/idea/magento2uct/execution/output/ReportBuilder.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ public void addSummary(final Summary summary) {
8686
*
8787
* @return JsonFile
8888
*/
89-
@SuppressWarnings({"PMD.NPathComplexity", "PMD.CyclomaticComplexity"})
89+
@SuppressWarnings({
90+
"PMD.NPathComplexity",
91+
"PMD.CyclomaticComplexity",
92+
"PMD.CognitiveComplexity",
93+
})
9094
public JsonFile build() {
9195
if (report.getIssues().isEmpty() || report.getSummary() == null) {
9296
return null;
@@ -129,7 +133,7 @@ public JsonFile build() {
129133
WriteCommandAction.runWriteCommandAction(project, () -> {
130134
savedFile.add((JsonFile) ideaDirectory.add(reportFile));
131135

132-
if (savedFile.size() == 0) {
136+
if (savedFile.isEmpty()) {
133137
return;
134138
}
135139
final JsonObject topNode = (JsonObject) savedFile.get(0).getTopLevelValue();
@@ -143,14 +147,14 @@ public JsonFile build() {
143147
for (final Issue issue : report.getIssues()) {
144148
final JsonObject issueObject = jsonElementGenerator.createObject("\"lineNumber\": "
145149
+ issue.getLine() + ","
146-
+ "\"level\": \"" + issue.getLevel() + "\","
150+
+ "\"level\": \"" + issue.getLevel() + "\","//NOPMD
147151
+ "\"message\": \"" + JSONObject.escape(issue.getMessage()) + "\","
148152
+ "\"code\": \"" + issue.getCode() + "\","
149153
+ "\"fileName\": \"" + JSONObject.escape(issue.getFilename()) + "\","
150154
+ "\"validationType\": \"" + issue.getValidationType() + "\""
151155
);
152156
if (issuesValueBuilder.length() > 0) {
153-
issuesValueBuilder.append(",");
157+
issuesValueBuilder.append(',');
154158
}
155159
issuesValueBuilder.append(issueObject.getText());
156160
}
@@ -163,7 +167,7 @@ public JsonFile build() {
163167
+ "\"totalWarnings\": " + summary.getPhpWarnings() + ","
164168
+ "\"totalErrors\": " + summary.getPhpErrors() + ","
165169
+ "\"totalCriticalErrors\": " + summary.getPhpCriticalErrors() + ","
166-
+ "\"complexityScore\": " + summary.getComplexityScore() + ""
170+
+ "\"complexityScore\": " + summary.getComplexityScore()
167171
);
168172
JsonPsiUtil.addProperty(
169173
topNode,
@@ -180,7 +184,7 @@ public JsonFile build() {
180184
);
181185
});
182186

183-
return savedFile.size() > 0 ? savedFile.get(0) : reportFile;
187+
return savedFile.isEmpty() ? reportFile : savedFile.get(0);
184188
}
185189

186190
/**

src/com/magento/idea/magento2uct/execution/output/UctReportOutputUtil.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@ public void printIssue(final @NotNull ProblemDescriptor descriptor, final int co
8181
*/
8282
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
8383
public void printSummary(final Summary summary) {
84-
if (!summary.hasProblems()) {
84+
if (summary.getProcessedModules() == 0) {
8585
stdout.print(stdout.wrapInfo("Couldn't find modules to analyse").concat("\n"));
8686
return;
8787
}
88+
if (!summary.hasProblems()) {
89+
stdout.print("\n" + stdout.wrapInfo("No problems found").concat("\n"));
90+
}
8891
printNavigateToDetailedErrorsLink();
8992

9093
final Map<String, String> summaryMap = new LinkedHashMap<>();
@@ -116,16 +119,13 @@ public void printSummary(final Summary summary) {
116119
printSummarySeparator(longestKey + 2, longestValue + 2);
117120

118121
for (final Map.Entry<String, String> summaryEntry : summaryMap.entrySet()) {
119-
final StringBuilder header = new StringBuilder(summaryEntry.getKey());
122+
String header = summaryEntry.getKey();
120123
final String value = " " + summaryEntry.getValue();
121124

122125
if (header.length() < longestKey) {
123-
header.append(" ".repeat(longestKey - header.length()));
126+
header += " ".repeat(longestKey - header.length());//NOPMD
124127
}
125-
stdout.print(
126-
" " + stdout.wrapSummary(header.toString())
127-
.concat(" ").concat(value).concat("\n")
128-
);
128+
stdout.print(" " + stdout.wrapSummary(header).concat(" ").concat(value).concat("\n"));
129129
}
130130
printSummarySeparator(longestKey + 2, longestValue + 2);
131131
}

src/com/magento/idea/magento2uct/execution/scanner/ModuleScanner.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
import com.magento.idea.magento2plugin.magento.files.ModuleXml;
1919
import com.magento.idea.magento2plugin.magento.packages.Package;
2020
import com.magento.idea.magento2uct.execution.scanner.data.ComponentData;
21+
import com.magento.idea.magento2uct.execution.scanner.filter.ModuleScannerFilter;
2122
import java.util.ArrayList;
2223
import java.util.Arrays;
2324
import java.util.Iterator;
2425
import java.util.List;
25-
26-
import com.magento.idea.magento2uct.execution.scanner.filter.ModuleScannerFilter;
2726
import org.jetbrains.annotations.NotNull;
2827

2928
public final class ModuleScanner implements Iterable<ComponentData> {

src/com/magento/idea/magento2uct/execution/scanner/filter/ExcludeMagentoBundledFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public boolean isExcluded(final ComponentData component) {
4949
private @Nullable String extractVendorFromModuleName(final @NotNull String moduleName) {
5050
String[] moduleNamePats = moduleName.split("_");
5151

52-
if (moduleNamePats.length > 1) {//NOPMD
52+
if (moduleNamePats.length > 1) { //NOPMD
5353
return moduleNamePats[0].toLowerCase(Locale.ROOT);
5454
}
5555
moduleNamePats = moduleName.split("/");

src/com/magento/idea/magento2uct/ui/ConfigurationDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private void onOK() {
131131

132132
settingsService.setCurrentVersion(
133133
currentVersionValue.isEmpty()
134-
? null
134+
? null//NOPMD
135135
: SupportedVersion.getVersion(currentVersionValue)
136136
);
137137
final SupportedVersion targetVersionValue = SupportedVersion.getVersion(

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

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
public final class VersionStateManager {
1818

19-
private final UctSettingsService settingsService;
20-
2119
private static VersionStateManager instance;
2220
private final DeprecationStateIndex deprecationStateIndex;
21+
private final Boolean isSetIgnoreFlag;
22+
private final SupportedVersion currentVersion;
23+
private final SupportedVersion targetVersion;
2324

2425
/**
2526
* Get instance of the version state manager.
@@ -28,31 +29,63 @@ public final class VersionStateManager {
2829
*
2930
* @return VersionStateManager
3031
*/
31-
public synchronized static VersionStateManager getInstance(final @NotNull Project project) {//NOPMD
32-
if (instance == null) {
32+
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
33+
public static synchronized VersionStateManager getInstance(
34+
final @NotNull Project project
35+
) { //NOPMD
36+
final UctSettingsService settingsService = UctSettingsService.getInstance(project);
37+
38+
if (instance == null
39+
|| !instance.isValidFor(settingsService.shouldIgnoreCurrentVersion(),
40+
settingsService.getCurrentVersion(),
41+
settingsService.getTargetVersion()
42+
)) {
3343
instance = new VersionStateManager(project);
3444
}
3545
return instance;
3646
}
3747

48+
/**
49+
* Check if specified FQN exists in the deprecation index.
50+
*
51+
* @param fqn String
52+
*
53+
* @return boolean
54+
*/
55+
public boolean isDeprecated(final @NotNull String fqn) {
56+
return deprecationStateIndex.has(fqn);
57+
}
58+
3859
/**
3960
* Version state manager constructor.
4061
*/
4162
private VersionStateManager(final @NotNull Project project) {
42-
settingsService = UctSettingsService.getInstance(project);
63+
final UctSettingsService settingsService = UctSettingsService.getInstance(project);
4364
deprecationStateIndex = new DeprecationStateIndex();
65+
isSetIgnoreFlag = settingsService.shouldIgnoreCurrentVersion();
66+
currentVersion = settingsService.getCurrentVersion();
67+
targetVersion = settingsService.getTargetVersion();
68+
4469
compute(deprecationStateIndex);
4570
}
4671

4772
/**
48-
* Check if specified FQN exists in the deprecation index.
73+
* Check if current instance is valid for settings.
4974
*
50-
* @param fqn String
75+
* @param isSetIgnoreFlag boolean
76+
* @param currentVersion SupportedVersion
77+
* @param targetVersion SupportedVersion
5178
*
5279
* @return boolean
5380
*/
54-
public boolean isDeprecated(final @NotNull String fqn) {
55-
return deprecationStateIndex.has(fqn);
81+
private boolean isValidFor(
82+
final Boolean isSetIgnoreFlag,
83+
final SupportedVersion currentVersion,
84+
final SupportedVersion targetVersion
85+
) {
86+
return this.isSetIgnoreFlag.equals(isSetIgnoreFlag)
87+
&& this.currentVersion.equals(currentVersion)
88+
&& this.targetVersion.equals(targetVersion);
5689
}
5790

5891
/**
@@ -61,23 +94,14 @@ public boolean isDeprecated(final @NotNull String fqn) {
6194
* @param index VersionStateIndex
6295
*/
6396
private void compute(final VersionStateIndex index) {
64-
final Boolean hasIgnoreFlagStoredValue = settingsService.shouldIgnoreCurrentVersion();
65-
boolean hasIgnoringFlag = false;
66-
67-
if (hasIgnoreFlagStoredValue != null) {
68-
hasIgnoringFlag = hasIgnoreFlagStoredValue;
69-
}
70-
final SupportedVersion targetVersion = settingsService.getTargetVersion();
71-
7297
if (targetVersion == null) {
7398
return;
7499
}
75100
final List<SupportedVersion> versionsToLoad = new LinkedList<>();
76-
final SupportedVersion currentVersion = settingsService.getCurrentVersion();
77101

78102
for (final SupportedVersion version : SupportedVersion.values()) {
79103
if (version.compareTo(targetVersion) <= 0) {
80-
if (hasIgnoringFlag) {
104+
if (isSetIgnoreFlag != null && isSetIgnoreFlag) {
81105
// If current version is NULL, it is less than minimum supported version.
82106
if (currentVersion == null || version.compareTo(currentVersion) > 0) {
83107
versionsToLoad.add(version);

src/com/magento/idea/magento2uct/versioning/indexes/IndexRepository.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,21 @@ public void put(final @NotNull Map<K, V> data, final String version) {
110110
@Override
111111
public @Nullable Map<K, V> load(final @NotNull String resourceName)
112112
throws IOException, ClassNotFoundException {
113-
try (
114-
InputStream inputStream = getClass().getResourceAsStream(resourceName);
115-
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)
116-
) {
113+
ObjectInputStream objectInputStream = null;
114+
115+
try (InputStream inputStream = getClass().getResourceAsStream(resourceName)) { //NOPMD
116+
if (inputStream == null) {
117+
return null;
118+
}
119+
objectInputStream = new ObjectInputStream(inputStream);
120+
117121
return (HashMap<K, V>) objectInputStream.readObject();
118122
} catch (ClassCastException exception) {
119123
return null;
124+
} finally {
125+
if (objectInputStream != null) {
126+
objectInputStream.close();
127+
}
120128
}
121129
}
122130
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void load(final @NotNull List<SupportedVersion> versions) {
7474
.replace("%key", registrationInfo.getKey());
7575
try {
7676
putIndexData(version.getVersion(), storage.load(RESOURCE_PATH + indexName));
77-
} catch (IOException | ClassNotFoundException exception) {//NOPMD
77+
} catch (IOException | ClassNotFoundException exception) { //NOPMD
7878
// Just go for the next version.
7979
}
8080
}

0 commit comments

Comments
 (0)