refactor: address sonar complexity issue in pip inspector tree parser
@@ -58,15 +58,32 @@ public class PipInspectorTreeParser {
}
continue;
}
-
Dependency currentDependency = parseDependencyFromLine(trimmedLine, sourcePath);
+ adjustForIndentLevel(history, line);
+ project = addDependencyToGraph(graph, history, project, currentDependency);
+ history.add(currentDependency);
+ }
+
+ adviseIfUnresolvedPackages(unResolvedPackageCount);
+
+ if (project != null) {
+ CodeLocation codeLocation = new CodeLocation(graph, project.getExternalId());
+ parseResult = new NameVersionCodeLocation(project.getName(), project.getVersion(), codeLocation);
+ }
+
+ return Optional.ofNullable(parseResult);
+ }
+
+ private void adjustForIndentLevel(DependencyHistory history, String line) {
int lineLevel = getLineLevel(line);
try {
history.clearDependenciesDeeperThan(lineLevel);
} catch (IllegalStateException e) {
logger.warn(String.format("Problem parsing line '%s': %s", line, e.getMessage()));
}
+ }
+ private Dependency addDependencyToGraph(MutableDependencyGraph graph, DependencyHistory history, Dependency project, Dependency currentDependency) {
if (project == null) {
project = currentDependency;
} else if (project.equals(history.getLastDependency())) {
@@ -76,21 +93,14 @@ public class PipInspectorTreeParser {
} else {
graph.addChildWithParents(currentDependency, history.getLastDependency());
}
-
- history.add(currentDependency);
+ return project;
}
+ private void adviseIfUnresolvedPackages(int unResolvedPackageCount) {
if (unResolvedPackageCount > 0) {
logger.error("The Pip inspector was unable to resolve {} packages. Please check to be sure all packages have been installed with 'pip install'. Refer to 'Python support' in the Detect documentation for important information regarding Python projects.",
unResolvedPackageCount);
}
-
- if (project != null) {
- CodeLocation codeLocation = new CodeLocation(graph, project.getExternalId());
- parseResult = new NameVersionCodeLocation(project.getName(), project.getVersion(), codeLocation);
- }
-
- return Optional.ofNullable(parseResult);
}
private boolean parseErrorsFromLine(String trimmedLine) {
