Skip to content

Commit

Permalink
Issue checkstyle#7475: resolve Cognitive Complexity cases above 20
Browse files Browse the repository at this point in the history
  • Loading branch information
romani committed Jan 18, 2020
1 parent 911afa5 commit 9a7a8e1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
32 changes: 20 additions & 12 deletions src/main/java/com/puppycrawl/tools/checkstyle/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -786,24 +786,32 @@ else if (hasConfigurationFile) {
final String msg = "Could not find config XML file '%s'.";
result.add(String.format(Locale.ROOT, msg, configurationFile));
}

// validate optional parameters
if (propertiesFile != null && !propertiesFile.exists()) {
result.add(String.format(Locale.ROOT,
"Could not find file '%s'.", propertiesFile));
}
if (checkerThreadsNumber < 1) {
result.add("Checker threads number must be greater than zero");
}
if (treeWalkerThreadsNumber < 1) {
result.add("TreeWalker threads number must be greater than zero");
}
result.addAll(validateOptionalCliParametersIfConfigDefined());
}
else {
result.add("Must specify a config XML file.");
}

return result;
}

/**
* Validates optional command line parameters that might be used with config file.
* @return list of violations
*/
private List<String> validateOptionalCliParametersIfConfigDefined() {
final List<String> result = new ArrayList<>();
if (propertiesFile != null && !propertiesFile.exists()) {
result.add(String.format(Locale.ROOT,
"Could not find file '%s'.", propertiesFile));
}
if (checkerThreadsNumber < 1) {
result.add("Checker threads number must be greater than zero");
}
if (treeWalkerThreadsNumber < 1) {
result.add("TreeWalker threads number must be greater than zero");
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,8 @@ private void finishImportList() {
final String fullImportIdent = importObject.getImportFullPath();

if (importGroup.equals(currentGroup)) {
if (isSeparatedByExtraEmptyLine(previousImportObjectFromCurrentGroup,
importObject)) {
log(importObject.getStartLineNumber(), MSG_SEPARATED_IN_GROUP,
fullImportIdent);
}
validateExtraEmptyLine(previousImportObjectFromCurrentGroup,
importObject, fullImportIdent);
if (isAlphabeticalOrderBroken(previousImportFromCurrentGroup, fullImportIdent)) {
log(importObject.getStartLineNumber(), MSG_LEX,
fullImportIdent, previousImportFromCurrentGroup);
Expand All @@ -579,10 +576,8 @@ private void finishImportList() {
if (customImportOrderRules.size() > currentGroupNumber + 1) {
final String nextGroup = getNextImportGroup(currentGroupNumber + 1);
if (importGroup.equals(nextGroup)) {
if (isEmptyLineMissed(previousImportObjectFromCurrentGroup, importObject)) {
log(importObject.getStartLineNumber(), MSG_LINE_SEPARATOR,
fullImportIdent);
}
validateMissedEmptyLine(previousImportObjectFromCurrentGroup,
importObject, fullImportIdent);
currentGroup = nextGroup;
currentGroupNumber = customImportOrderRules.indexOf(nextGroup);
previousImportFromCurrentGroup = fullImportIdent;
Expand All @@ -601,6 +596,32 @@ private void finishImportList() {
}
}

/**
* Log violation if empty line is missed.
* @param previousImport previous import from current group.
* @param importObject current import.
* @param fullImportIdent full import identifier.
*/
private void validateMissedEmptyLine(ImportDetails previousImport,
ImportDetails importObject, String fullImportIdent) {
if (isEmptyLineMissed(previousImport, importObject)) {
log(importObject.getStartLineNumber(), MSG_LINE_SEPARATOR, fullImportIdent);
}
}

/**
* Log violation if extra empty line is present.
* @param previousImport previous import from current group.
* @param importObject current import.
* @param fullImportIdent full import identifier.
*/
private void validateExtraEmptyLine(ImportDetails previousImport,
ImportDetails importObject, String fullImportIdent) {
if (isSeparatedByExtraEmptyLine(previousImport, importObject)) {
log(importObject.getStartLineNumber(), MSG_SEPARATED_IN_GROUP, fullImportIdent);
}
}

/**
* Get first import group.
*
Expand Down

0 comments on commit 9a7a8e1

Please sign in to comment.