Skip to content

Commit 5c3b170

Browse files
author
Joris Conijn
authored
refactor: reduce complexity (#27)
By using separate methods we make use of method naming to make it clearer what the actions and intentions are.
1 parent 856b6d8 commit 5c3b170

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ release: build
3333

3434
complexity-baseline:
3535
$(info Maintenability index)
36-
radon mi --min A --max A --show report2junit
36+
radon mi --min A --max A --show --sort report2junit
3737
$(info Cyclomatic complexity index)
3838
xenon --max-absolute A --max-modules A --max-average A report2junit
3939

report2junit/reports/cfn_nag.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ def compatible(cls, data: bytes) -> bool:
1919
return "filename" in entry and "file_results" in entry
2020

2121
def __create_report(self) -> TestSuite:
22-
source = self.parse(self.source)
22+
list(self.__findings())
23+
return TestSuite("cfn-nag findings", self.cases)
2324

24-
for file_findings in source:
25-
for violation in file_findings["file_results"]["violations"]:
26-
for i, resource_id in enumerate(violation["logical_resource_ids"]):
27-
self.failure(
28-
name=f"{violation['id']} - {violation['message']}",
29-
message=f"{file_findings['filename']}#L{violation['line_numbers'][i]}",
30-
classname=resource_id,
31-
)
25+
def __findings(self) -> map:
26+
return map(self.__parse_findings, self.parse(self.source))
3227

33-
return TestSuite("cfn-nag findings", self.cases)
28+
def __parse_findings(self, file_finding: dict) -> None:
29+
for violation in file_finding["file_results"]["violations"]:
30+
self.__parse_violation(file_finding["filename"], violation)
31+
32+
def __parse_violation(self, file_name: str, violation) -> None:
33+
for index, resource_id in enumerate(violation["logical_resource_ids"]):
34+
self.failure(
35+
name=f"{violation['id']} - {violation['message']}",
36+
message=f"{file_name}#L{violation['line_numbers'][index]}",
37+
classname=resource_id,
38+
)
3439

3540
def apply(self, callback: Callable[[TestSuite], None]) -> bool:
3641
callback(self.__create_report())

0 commit comments

Comments
 (0)