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 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: remove unused code * feat: provide otherMetricsValues in computeImplementation * refactor: implement Maintainability Index metric * chore: setup mi metric
- Loading branch information
1 parent
f3a0cf9
commit c80810d
Showing
21 changed files
with
242 additions
and
153 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
105 changes: 105 additions & 0 deletions
105
lib/src/analyzers/lint_analyzer/metrics/metrics_list/maintainability_index_metric.dart
This file contains 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,105 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
|
||
import '../../models/entity_type.dart'; | ||
import '../../models/internal_resolved_unit_result.dart'; | ||
import '../../models/scoped_class_declaration.dart'; | ||
import '../../models/scoped_function_declaration.dart'; | ||
import '../metric_utils.dart'; | ||
import '../models/function_metric.dart'; | ||
import '../models/metric_computation_result.dart'; | ||
import '../models/metric_documentation.dart'; | ||
import '../models/metric_value.dart'; | ||
import 'cyclomatic_complexity/cyclomatic_complexity_metric.dart'; | ||
import 'halstead_volume/halstead_volume_metric.dart'; | ||
import 'source_lines_of_code/source_lines_of_code_metric.dart'; | ||
|
||
const _documentation = MetricDocumentation( | ||
name: 'Maintainability Index', | ||
shortName: 'MI', | ||
brief: | ||
'Maintainability Index is a software metric which measures how maintainable (easy to support and change) the source code is.', | ||
measuredType: EntityType.methodEntity, | ||
recomendedThreshold: 50, | ||
); | ||
|
||
/// Maintainability Index (MI) | ||
/// | ||
/// Maintainability Index is a software metric which measures how maintainable | ||
/// (easy to support and change) the source code is. | ||
class MaintainabilityIndexMetric extends FunctionMetric<int> { | ||
static const String metricId = 'maintainability-index'; | ||
|
||
MaintainabilityIndexMetric({Map<String, Object> config = const {}}) | ||
: super( | ||
id: metricId, | ||
documentation: _documentation, | ||
threshold: readNullableThreshold<int>(config, metricId), | ||
levelComputer: invertValueLevel, | ||
); | ||
|
||
@override | ||
bool supports( | ||
Declaration node, | ||
Iterable<ScopedClassDeclaration> classDeclarations, | ||
Iterable<ScopedFunctionDeclaration> functionDeclarations, | ||
InternalResolvedUnitResult source, | ||
Iterable<MetricValue<num>> otherMetricsValues, | ||
) => | ||
super.supports( | ||
node, | ||
classDeclarations, | ||
functionDeclarations, | ||
source, | ||
otherMetricsValues, | ||
) && | ||
[ | ||
CyclomaticComplexityMetric.metricId, | ||
HalsteadVolumeMetric.metricId, | ||
SourceLinesOfCodeMetric.metricId, | ||
].every((metricId) => | ||
otherMetricsValues.any((value) => value.metricsId == metricId)); | ||
|
||
@override | ||
MetricComputationResult<int> computeImplementation( | ||
Declaration node, | ||
Iterable<ScopedClassDeclaration> classDeclarations, | ||
Iterable<ScopedFunctionDeclaration> functionDeclarations, | ||
InternalResolvedUnitResult source, | ||
Iterable<MetricValue<num>> otherMetricsValues, | ||
) { | ||
final halVol = otherMetricsValues.firstWhere( | ||
(value) => value.metricsId == HalsteadVolumeMetric.metricId, | ||
); | ||
|
||
final cyclomatic = otherMetricsValues.firstWhere( | ||
(value) => value.metricsId == CyclomaticComplexityMetric.metricId, | ||
); | ||
|
||
final sloc = otherMetricsValues.firstWhere( | ||
(value) => value.metricsId == SourceLinesOfCodeMetric.metricId, | ||
); | ||
|
||
final halVolScale = log(max(1, halVol.value)); | ||
final cycloScale = cyclomatic.value; | ||
final slocScale = log(max(1, sloc.value)); | ||
|
||
final maintainabilityIndex = | ||
(171 - halVolScale * 5.2 - cycloScale * 0.23 - slocScale * 16.2) / 171; | ||
|
||
return MetricComputationResult( | ||
value: (maintainabilityIndex * 100).clamp(0, 100).ceil(), | ||
); | ||
} | ||
|
||
@override | ||
String commentMessage(String nodeType, int value, int? threshold) { | ||
final exceeds = threshold != null && value > threshold | ||
? ', exceeds the minimum of $threshold allowed' | ||
: ''; | ||
final index = '$value maintability index'; | ||
|
||
return 'This $nodeType has $index$exceeds.'; | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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.