-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathModuleMetricPostAverage.js
More file actions
80 lines (73 loc) · 3.7 KB
/
Copy pathModuleMetricPostAverage.js
File metadata and controls
80 lines (73 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Provides a typhonjs-escomplex-module / ESComplexModule plugin which gathers and calculates all default metrics.
*
* @see https://www.npmjs.com/package/typhonjs-escomplex-commons
* @see https://www.npmjs.com/package/typhonjs-escomplex-module
*/
export default class ModuleMetricPostAverage
{
/**
* Coordinates calculating all metrics. All module and class methods are traversed. If there are no module or class
* methods respectively the aggregate MethodReport is used for calculations.
*
* @param {ModuleReport} moduleReport - The ModuleReport being processed.
* @param {object} settings - Settings for module processing.
*
* @private
*/
static calculate(moduleReport, settings)
{
// Handle module report.
const moduleAggregateAverage = moduleReport.aggregateAverage;
ModuleMetricPostAverage.calculateMaintainabilityIndex(moduleReport, settings, moduleAggregateAverage.cyclomatic,
moduleAggregateAverage.halstead.effort, moduleAggregateAverage.sloc.logical);
// Handle module class reports.
moduleReport.classes.forEach((classReport) =>
{
// const classAggregateAverage = classReport.aggregateAverage;
//
// ModuleMetricPostAverage.calculateMaintainabilityIndex(classReport, settings, classAggregateAverage.cyclomatic,
// classAggregateAverage.halstead.effort, classAggregateAverage.sloc.logical);
const classMethodAverages = classReport.methodAverage;
ModuleMetricPostAverage.calculateMaintainabilityIndex(classReport, settings, classMethodAverages.cyclomatic,
classMethodAverages.halstead.effort, classMethodAverages.sloc.logical);
});
}
/**
* Designed in 1991 by Paul Oman and Jack Hagemeister at the University of Idaho, this metric is calculated at the
* whole program or module level from averages of the other 3 metrics, using the following formula:
* ```
* 171 -
* (3.42 * ln(mean effort)) -
* (0.23 * ln(mean cyclomatic complexity)) -
* (16.2 * ln(mean logical LOC))
* ```
* Values are on a logarithmic scale ranging from negative infinity up to 171, with greater numbers indicating a
* higher level of maintainability. In their original paper, Oman and Hagemeister identified 65 as the threshold
* value below which a program should be considered difficult to maintain.
*
* Note: Bare module reports with no branching control flow or module methods and class reports with no class
* methods start with a base cyclomatic complexity of 0. It is necessary to handle this case as
* Math.log(0) === -Infinity.
*
* @param {ClassReport|ModuleReport} report - A ClassReport or ModuleReport to perform calculations on.
* @param {object} settings - Settings for module processing.
* @param {number} averageCyclomatic - Average cyclomatic metric across a ClassReport / ModuleReport.
* @param {number} averageEffort - Average Halstead effort across a ClassReport / ModuleReport.
* @param {number} averageLoc - Average logical SLOC across a ClassReport / ModuleReport.
*
* @private
*/
static calculateMaintainabilityIndex(report, settings, averageCyclomatic, averageEffort, averageLoc)
{
report.maintainability =
171
- (3.42 * Math.log(averageEffort))
- (0.23 * (averageCyclomatic === 0 ? 0 : Math.log(averageCyclomatic)))
- (16.2 * Math.log(averageLoc));
/* istanbul ignore if */
if (report.maintainability > 171) { report.maintainability = 171; }
/* istanbul ignore if */
if (settings.newmi) { report.maintainability = Math.max(0, (report.maintainability * 100) / 171); }
}
}