-
Notifications
You must be signed in to change notification settings - Fork 12
Cyclomatic complexity #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
68d32b6
move naming logic from Flow Model to ParseFlows, and add metadata typ…
RubenHalman 99f8bba
cyclomatic complexity
RubenHalman e6d7d20
cyclomatic complexity
RubenHalman 5175814
fix tests and add new rule to default rules
RubenHalman f049f3e
use parse as part of core
RubenHalman bc44216
remove flow model changes and apply loop count to rule
RubenHalman b28e238
revert unnessecary stylistic changes
RubenHalman c74ef13
fix: prefer flow parametic polymorphism on constructor
junners 828d2b6
test: add unit tests for cyclomatic complexity
junners 2a99a21
docs: add cyclomatic complexity to default docs
junners c4377da
docs: change verbiage on cyclomatic complexity
junners File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { RuleCommon } from "../models/RuleCommon"; | ||
import * as core from "../internals/internals"; | ||
|
||
export class CyclomaticComplexity extends RuleCommon implements core.IRuleDefinition { | ||
constructor() { | ||
super( | ||
{ | ||
name: "CyclomaticComplexity", | ||
label: "Cyclomatic Complexity", | ||
description: `The number of loops and decision rules, plus the number of decisions. | ||
Use a combination of 1) subflows and 2) breaking flows into multiple concise trigger ordered flows, | ||
to reduce the cyclomatic complexity within a single flow, ensuring maintainability and simplicity.`, | ||
supportedTypes: core.FlowType.backEndTypes, | ||
docRefs: [ | ||
{ | ||
label: `Cyclomatic complexity is a software metric used to indicate the complexity of a program. | ||
It is a quantitative measure of the number of linearly independent paths through a program's source code.`, | ||
path: "https://en.wikipedia.org/wiki/Cyclomatic_complexity", | ||
}, | ||
], | ||
isConfigurable: true, | ||
autoFixable: false, | ||
}, | ||
{ severity: "note" } | ||
); | ||
} | ||
|
||
private defaultThreshold: number = 25; | ||
|
||
private cyclomaticComplexityUnit: number = 0; | ||
|
||
public execute(flow: core.Flow, options?: { threshold: number }): core.RuleResult { | ||
// Set Threshold | ||
const threshold = options?.threshold || this.defaultThreshold; | ||
|
||
// Calculate Cyclomatic Complexity based on the number of decision rules and loops, adding the number of decisions plus 1. | ||
let cyclomaticComplexity = 1; | ||
|
||
const flowDecisions = flow?.elements?.filter( | ||
(node) => node.subtype === "decisions" | ||
) as core.FlowElement[]; | ||
const flowLoops = flow?.elements?.filter((node) => node.subtype === "loops"); | ||
|
||
for (const decision of flowDecisions || []) { | ||
const rules = decision.element["rules"]; | ||
if (Array.isArray(rules)) { | ||
cyclomaticComplexity += rules.length + 1; | ||
} else { | ||
cyclomaticComplexity += 1; | ||
} | ||
} | ||
cyclomaticComplexity += flowLoops?.length ?? 0; | ||
|
||
this.cyclomaticComplexityUnit = cyclomaticComplexity; // for unit testing | ||
|
||
const results: core.ResultDetails[] = []; | ||
if (cyclomaticComplexity > threshold) { | ||
results.push( | ||
new core.ResultDetails( | ||
new core.FlowAttribute(`${cyclomaticComplexity}`, "CyclomaticComplexity", `>${threshold}`) | ||
) | ||
); | ||
} | ||
return new core.RuleResult(this, results); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import * as path from "path"; | ||
import { describe, it, expect } from "@jest/globals"; | ||
|
||
import * as core from "../src"; | ||
import { CyclomaticComplexity } from "../src/main/rules/CyclomaticComplexity"; | ||
|
||
describe("CyclomaticComplexity ", () => { | ||
const example_uri = path.join(__dirname, "./xmlfiles/Cyclomatic_Complexity.flow-meta.xml"); | ||
const other_uri = path.join(__dirname, "./xmlfiles/SOQL_Query_In_A_Loop.flow-meta.xml"); | ||
const defaultConfig = { | ||
rules: { | ||
CyclomaticComplexity: { | ||
severity: "error", | ||
}, | ||
}, | ||
}; | ||
|
||
it("should have a result when there are more than 25 decision options", async () => { | ||
const flows = await core.parse([example_uri]); | ||
debugger; | ||
const results: core.ScanResult[] = core.scan(flows, defaultConfig); | ||
const occurringResults = results[0].ruleResults.filter((rule) => rule.occurs); | ||
expect(occurringResults).toHaveLength(1); | ||
expect(occurringResults[0].ruleName).toBe("CyclomaticComplexity"); | ||
}); | ||
|
||
it("should have no result when value is below threshold", async () => { | ||
const flows = await core.parse([other_uri]); | ||
|
||
const results: core.ScanResult[] = core.scan(flows, defaultConfig); | ||
const occurringResults = results[0].ruleResults.filter((rule) => rule.occurs); | ||
expect(occurringResults).toHaveLength(0); | ||
}); | ||
|
||
it("should have a result when value surpasses a configured threshold", async () => { | ||
const flows = await core.parse([other_uri]); | ||
const ruleConfig = { | ||
rules: { | ||
CyclomaticComplexity: { | ||
threshold: 1, | ||
severity: "error", | ||
}, | ||
}, | ||
}; | ||
|
||
const results: core.ScanResult[] = core.scan(flows, ruleConfig); | ||
const occurringResults = results[0].ruleResults.filter((rule) => rule.occurs); | ||
expect(occurringResults).toHaveLength(1); | ||
expect(occurringResults[0].ruleName).toBe("CyclomaticComplexity"); | ||
}); | ||
|
||
it("should correctly count the number of decisions and underlying rules one level", () => { | ||
const sut = new CyclomaticComplexity(); | ||
const raw = { | ||
elements: [ | ||
{ | ||
subtype: "decisions", | ||
element: { | ||
rules: [{}, {}, {}], | ||
}, | ||
}, | ||
], | ||
} as Partial<core.Flow>; | ||
const given = raw as core.Flow; | ||
sut.execute(given); | ||
expect(sut["cyclomaticComplexityUnit"]).toBe(5); | ||
}); | ||
|
||
it("should correctly count the number of decisions and underlying rules multi level", () => { | ||
const sut = new CyclomaticComplexity(); | ||
const raw = { | ||
elements: [ | ||
{ | ||
subtype: "decisions", | ||
element: { | ||
rules: [{}, {}, {}], | ||
}, | ||
}, | ||
{ subtype: "decisions", element: { rules: [{}] } }, | ||
], | ||
} as Partial<core.Flow>; | ||
const given = raw as core.Flow; | ||
sut.execute(given); | ||
expect(sut["cyclomaticComplexityUnit"]).toBe(7); | ||
}); | ||
|
||
it("should not throw an exception when theres no elements at all", () => { | ||
const sut = new CyclomaticComplexity(); | ||
const raw = { | ||
elements: [], | ||
} as Partial<core.Flow>; | ||
const given = raw as core.Flow; | ||
expect(() => { | ||
sut.execute(given); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it("should not throw an exception when element isn't present", () => { | ||
const sut = new CyclomaticComplexity(); | ||
const raw = {} as Partial<core.Flow>; | ||
const given = raw as core.Flow; | ||
expect(() => { | ||
sut.execute(given); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it("should correctly count the number of loops", () => { | ||
const sut = new CyclomaticComplexity(); | ||
const raw = { | ||
elements: [ | ||
{ | ||
subtype: "loops", | ||
}, | ||
{ | ||
subtype: "loops", | ||
}, | ||
], | ||
} as Partial<core.Flow>; | ||
const given = raw as core.Flow; | ||
sut.execute(given); | ||
expect(sut["cyclomaticComplexityUnit"]).toBe(3); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.