-
Notifications
You must be signed in to change notification settings - Fork 11
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 3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
import * as p from "path"; | ||
import p from "path"; | ||
import { Flow } from "../models/Flow"; | ||
import fs from "fs"; | ||
import * as fs from "fs"; | ||
import { convert } from "xmlbuilder2"; | ||
import { ParsedFlow } from "../models/ParsedFlow"; | ||
|
||
export async function ParseFlows(selectedUris: string[]): Promise<ParsedFlow[]> { | ||
export async function parse(selectedUris: string[]): Promise<ParsedFlow[]> { | ||
const parseResults: ParsedFlow[] = []; | ||
for (const uri of selectedUris) { | ||
try { | ||
const normalizedURI = p.normalize(uri); | ||
const fsPath = p.resolve(normalizedURI); | ||
let flowName = p.basename(p.basename(fsPath), p.extname(fsPath)); | ||
if (flowName.includes(".")) { | ||
flowName = flowName.split(".")[0]; | ||
} | ||
const content = await fs.readFileSync(normalizedURI); | ||
const xmlString = content.toString(); | ||
const flowObj = convert(xmlString, { format: "object" }); | ||
parseResults.push(new ParsedFlow(uri, new Flow(uri, flowObj))); | ||
parseResults.push(new ParsedFlow(uri, new Flow(flowName, flowObj))); | ||
} catch (e) { | ||
parseResults.push(new ParsedFlow(uri, undefined, e.errorMessage)); | ||
} | ||
} | ||
return parseResults; | ||
} | ||
|
||
export function parse(selectedUris: string[]): Promise<ParsedFlow[]> { | ||
return ParseFlows(selectedUris); | ||
} |
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,50 @@ | ||
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 subflows to reduce the cyclomatic complexity within a single flow, ensuring maintainability and simplicity.", | ||
supportedTypes: core.FlowType.backEndTypes, | ||
docRefs: [], | ||
isConfigurable: true, | ||
autoFixable: false, | ||
}); | ||
} | ||
|
||
public execute(flow: core.Flow, options?: { threshold: string }): core.RuleResult { | ||
// Set Threshold | ||
let threshold = 0; | ||
|
||
if (options && options.threshold) { | ||
threshold = +options.threshold; | ||
} else { | ||
threshold = 25; | ||
} | ||
|
||
// Calculate Cyclomatic Complexity based on the number of decision rules and loops, adding the number of decisions plus 1. | ||
let cyclomaticComplexity = 1; | ||
for (const decision of flow.decisions) { | ||
const rules = decision.element["rules"]; | ||
if (Array.isArray(rules)) { | ||
cyclomaticComplexity += rules.length + 1; | ||
} else { | ||
cyclomaticComplexity += 1; | ||
} | ||
} | ||
cyclomaticComplexity += flow.loops.length; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as core from "../src"; | ||
import * as path from "path"; | ||
|
||
import { describe, it, expect } from "@jest/globals"; | ||
|
||
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"); | ||
|
||
it("should have a result when there are more than 25 decision options", async () => { | ||
const flows = await core.parse([example_uri]); | ||
const results: core.ScanResult[] = core.scan(flows); | ||
const occurringResults = results[0].ruleResults.filter((rule) => rule.occurs); | ||
expect(occurringResults.length).toBeGreaterThanOrEqual(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 ruleConfig = { | ||
rules: { | ||
CyclomaticComplexity: { | ||
severity: "error", | ||
}, | ||
}, | ||
}; | ||
|
||
const results: core.ScanResult[] = core.scan(flows, ruleConfig); | ||
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); | ||
}); | ||
}); |
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
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.