This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
utility_selector.dart
267 lines (232 loc) · 9.95 KB
/
utility_selector.dart
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import 'dart:math';
import 'package:code_checker/metrics.dart';
import 'package:quiver/iterables.dart' as quiver;
import '../config/config.dart';
import '../models/component_record.dart';
import '../models/component_report.dart';
import '../models/file_record.dart';
import '../models/file_report.dart';
import '../models/function_record.dart';
import '../models/function_report.dart';
double log2(num a) => log(a) / ln2;
num sum(Iterable<num> it) => it.fold(0, (a, b) => a + b);
double avg(Iterable<num> it) => it.isNotEmpty ? sum(it) / it.length : 0;
class UtilitySelector {
static FileReport analysisReportForRecords(
Iterable<FileRecord> records, Config config) =>
records.map((r) => fileReport(r, config)).reduce(mergeFileReports);
static FileReport fileReport(FileRecord record, Config config) {
final componentReports =
record.components.values.map((r) => componentReport(r, config));
final functionReports =
record.functions.values.map((r) => functionReport(r, config));
final averageArgumentCount =
avg(functionReports.map((r) => r.argumentsCount.value));
final totalArgumentsCountViolations = functionReports
.where((r) => isReportLevel(r.argumentsCount.level))
.length;
final averageMaintainabilityIndex =
avg(functionReports.map((r) => r.maintainabilityIndex.value));
final totalMaintainabilityIndexViolations = functionReports
.where((r) => isReportLevel(r.maintainabilityIndex.level))
.length;
final averageMethodsCount =
avg(componentReports.map((r) => r.methodsCount.value));
final totalMethodsCountViolations = componentReports
.where((r) => isReportLevel(r.methodsCount.level))
.length;
final totalCyclomaticComplexity =
sum(functionReports.map((r) => r.cyclomaticComplexity.value));
final totalCyclomaticComplexityViolations = functionReports
.where((r) => isReportLevel(r.cyclomaticComplexity.level))
.length;
final totalLinesOfExecutableCode =
sum(functionReports.map((r) => r.linesOfExecutableCode.value));
final totalLinesOfExecutableCodeViolations = functionReports
.where((r) => isReportLevel(r.linesOfExecutableCode.level))
.length;
final averageMaximumNestingLevel =
avg(functionReports.map((r) => r.maximumNestingLevel.value)).round();
final totalMaximumNestingLevelViolations = functionReports
.where((r) => isReportLevel(r.maximumNestingLevel.level))
.length;
return FileReport(
averageArgumentsCount: averageArgumentCount.round(),
argumentsCountViolations: totalArgumentsCountViolations,
averageMaintainabilityIndex: averageMaintainabilityIndex,
maintainabilityIndexViolations: totalMaintainabilityIndexViolations,
averageMethodsCount: averageMethodsCount.round(),
methodsCountViolations: totalMethodsCountViolations,
totalCyclomaticComplexity: totalCyclomaticComplexity.round(),
cyclomaticComplexityViolations: totalCyclomaticComplexityViolations,
totalLinesOfExecutableCode: totalLinesOfExecutableCode.round(),
linesOfExecutableCodeViolations: totalLinesOfExecutableCodeViolations,
averageMaximumNestingLevel: averageMaximumNestingLevel,
maximumNestingLevelViolations: totalMaximumNestingLevelViolations,
);
}
static ComponentReport componentReport(
ComponentRecord component, Config config) =>
ComponentReport(
methodsCount: MetricValue<int>(
metricsId: '',
value: component.methodsCount,
level: valueLevel(
component.methodsCount, config.numberOfMethodsWarningLevel),
comment: '',
),
weightOfClass: MetricValue<double>(
metricsId: '',
value: component.weightOfClass,
level: _violationLevelPercentInvert(
component.weightOfClass, config.weightOfClassWarningLevel),
comment: '',
),
);
static FunctionReport functionReport(FunctionRecord function, Config config) {
final cyclomaticComplexity =
sum(function.cyclomaticComplexityLines.values) + 1;
final linesOfExecutableCode = function.linesWithCode.length;
final maximumNestingLevel = function.nestingLines.fold<int>(
0, (previousValue, element) => max(previousValue, element.length));
// Total number of occurrences of operators.
final totalNumberOfOccurrencesOfOperators = sum(function.operators.values);
// Total number of occurrences of operands
final totalNumberOfOccurrencesOfOperands = sum(function.operands.values);
// Number of distinct operators.
final numberOfDistinctOperators = function.operators.keys.length;
// Number of distinct operands.
final numberOfDistinctOperands = function.operands.keys.length;
// Halstead Program Length – The total number of operator occurrences and the total number of operand occurrences.
final halsteadProgramLength = totalNumberOfOccurrencesOfOperators +
totalNumberOfOccurrencesOfOperands;
// Halstead Vocabulary – The total number of unique operator and unique operand occurrences.
final halsteadVocabulary =
numberOfDistinctOperators + numberOfDistinctOperands;
// Program Volume – Proportional to program size, represents the size, in bits, of space necessary for storing the program. This parameter is dependent on specific algorithm implementation.
final halsteadVolume =
halsteadProgramLength * log2(max(1, halsteadVocabulary));
final maintainabilityIndex = max(
0,
(171 -
5.2 * log(max(1, halsteadVolume)) -
cyclomaticComplexity * 0.23 -
16.2 * log(max(1, linesOfExecutableCode))) *
100 /
171,
).toDouble();
return FunctionReport(
cyclomaticComplexity: MetricValue<int>(
metricsId: '',
value: cyclomaticComplexity.round(),
level: valueLevel(
cyclomaticComplexity.round(),
config.cyclomaticComplexityWarningLevel,
),
comment: '',
),
linesOfExecutableCode: MetricValue<int>(
metricsId: '',
value: linesOfExecutableCode,
level: valueLevel(
linesOfExecutableCode, config.linesOfExecutableCodeWarningLevel),
comment: '',
),
maintainabilityIndex: MetricValue<double>(
metricsId: '',
value: maintainabilityIndex,
level: _maintainabilityIndexViolationLevel(maintainabilityIndex),
comment: '',
),
argumentsCount: MetricValue<int>(
metricsId: '',
value: function.argumentsCount,
level: valueLevel(
function.argumentsCount, config.numberOfArgumentsWarningLevel),
comment: '',
),
maximumNestingLevel: MetricValue<int>(
metricsId: '',
value: maximumNestingLevel,
level: valueLevel(
maximumNestingLevel,
config.maximumNestingWarningLevel,
),
comment: '',
),
);
}
static MetricValueLevel componentViolationLevel(ComponentReport report) =>
report.methodsCount.level;
static MetricValueLevel functionViolationLevel(FunctionReport report) =>
quiver.max([
report.cyclomaticComplexity.level,
report.linesOfExecutableCode.level,
report.maintainabilityIndex.level,
report.argumentsCount.level,
report.maximumNestingLevel.level,
]);
static MetricValueLevel maxViolationLevel(
Iterable<FileRecord> records, Config config) =>
quiver.max(records
.expand((fileRecord) => fileRecord.functions.values
.map((functionRecord) => functionReport(functionRecord, config)))
.map(functionViolationLevel));
static FileReport mergeFileReports(FileReport lhs, FileReport rhs) =>
FileReport(
averageArgumentsCount:
((lhs.averageArgumentsCount + rhs.averageArgumentsCount) / 2)
.round(),
argumentsCountViolations:
lhs.argumentsCountViolations + rhs.argumentsCountViolations,
averageMaintainabilityIndex: (lhs.averageMaintainabilityIndex +
rhs.averageMaintainabilityIndex) /
2,
maintainabilityIndexViolations: lhs.maintainabilityIndexViolations +
rhs.maintainabilityIndexViolations,
averageMethodsCount:
((lhs.averageMethodsCount + rhs.averageMethodsCount) / 2).round(),
methodsCountViolations:
lhs.methodsCountViolations + rhs.methodsCountViolations,
totalCyclomaticComplexity:
lhs.totalCyclomaticComplexity + rhs.totalCyclomaticComplexity,
cyclomaticComplexityViolations: lhs.cyclomaticComplexityViolations +
rhs.cyclomaticComplexityViolations,
totalLinesOfExecutableCode:
lhs.totalLinesOfExecutableCode + rhs.totalLinesOfExecutableCode,
linesOfExecutableCodeViolations: lhs.linesOfExecutableCodeViolations +
rhs.linesOfExecutableCodeViolations,
averageMaximumNestingLevel:
((lhs.averageMaximumNestingLevel + rhs.averageMaximumNestingLevel) /
2)
.round(),
maximumNestingLevelViolations: lhs.maximumNestingLevelViolations +
rhs.maximumNestingLevelViolations,
);
static MetricValueLevel _violationLevelPercentInvert(
double value,
double warningLevel,
) {
if (warningLevel == null) {
return MetricValueLevel.none;
}
if (value < warningLevel * 0.5) {
return MetricValueLevel.alarm;
} else if (value < warningLevel) {
return MetricValueLevel.warning;
} else if (value < (warningLevel * 2)) {
return MetricValueLevel.noted;
}
return MetricValueLevel.none;
}
static MetricValueLevel _maintainabilityIndexViolationLevel(double index) {
if (index < 10) {
return MetricValueLevel.alarm;
} else if (index < 20) {
return MetricValueLevel.warning;
} else if (index < 40) {
return MetricValueLevel.noted;
}
return MetricValueLevel.none;
}
}