Teams
Calculate Metrics
Reports code metrics for Dart code.
To execute the command, run:
$ dcm calculate-metrics lib # or dcm cm lib
info
You need to configure metrics
entry in the analysis_options.yaml
to have metrics report included into the result.
Full command description:
Usage: dcm calculate-metrics [arguments] <directories>
-h, --help Print this usage information.
-r, --reporter=<console> The format of the output of the analysis.
[console (default), checkstyle, codeclimate, html, gitlab, json]
-o, --output-directory=<OUTPUT> Write HTML output to OUTPUT.
(defaults to "metrics")
--json-path=<path/to/file.json> Path to the JSON file with the output of the analysis.
--[no-]report-all Report all metric results (default is to report only violations, works only for console reporter).
-c, --print-config Print resolved config.
--root-folder=<./> Root folder.
(defaults to current directory)
--sdk-path=<directory-path> Dart SDK directory path.
If the project has a `.fvm/flutter_sdk` symlink, it will be used if the SDK is not found.
--exclude=<{**/*.g.dart,**/*.freezed.dart}> File paths in Glob syntax to be exclude.
(defaults to "{**/*.g.dart,**/*.freezed.dart}")
--no-congratulate Don't show output even when there are no issues.
--[no-]verbose Show verbose logs.
--ci-key The license key to run on CI server.
--email The email used to purchase the license.
--no-analytics Disable sending anonymous usage statistics.
--set-exit-on-violation-level=<high> Set exit code 2 if code violations same or higher level than selected are detected.
[near, high, very high]
Output example
Console
Use --reporter=console
to enable this format.
HTML
Use --reporter=html
to enable this format.
HTML report overview
HTML single file report
JSON
The reporter prints a single JSON object containing meta information and the violations grouped by a file. Use --reporter=json
to enable this format.
The root object fields are
formatVersion
- an integer representing the format version (will be incremented each time the serialization format changes)timestamp
- a creation time of the report in YYYY-MM-DD HH:MM:SS formatrecords
- an array of objectssummary
- an array of objects
{
"formatVersion": 2,
"timestamp": "2021-04-11 14:44:42",
"records": [
{
...
},
{
...
},
{
...
}
],
"summary": [
{
...
},
{
...
}
]
}
The record object fields are
path
- a relative path to the target filefileMetrics
- an array with target file metricsclasses
- a map with class name as the key and class report as the valuefunctions
- a map with function name as the key and function report as the value
{
"path": "lib/src/metrics/metric_computation_result.dart",
"classes": {
...
},
"functions": {
...
},
"fileMetrics": {
...
}
}
The summary-record object fields are
title
- a message with information about the recordvalue
- an actual value of this record (can be an array or a single value)violations
- a value of a violations count of a metric associated with this record
{
"title": "Average Cyclomatic Number per line of code",
"value": 0.3447098976109215,
"violations": 5
}
The report object fields are
{
"codeSpan": {
...
},
"metrics": [
...
]
}
The code span object fields are
start
- a start location of an entityend
- an end location of an entitytext
- a source code text of an entity
{
"start": {
...
},
"end": {
...
},
"text": "entity source code"
}
The location object fields are
offset
- a zero-based offset of the location in the sourceline
- a zero-based line of the location in the sourcecolumn
- a zero-based column of the location in the source
{
"offset": 156,
"line": 7,
"column": 1
}
The metric value object fields are
metricsId
- an id of the computed metricvalue
- an actual value computed by the metricunitType
- a human readable unit type (optional)level
- a level of the value computed by the metriccomment
- a message with information about the valuerecommendation
- a message with information about how the user can improve the value (optional)context
- an additional information associated with the value that helps understand how the metric was computed
{
"metricsId": "number-of-methods",
"value": 14,
"unitType": "methods",
"level": "warning",
"comment": "This class has 14 methods, which exceeds the maximum of 10 allowed.",
"recommendation": "Consider breaking this class up into smaller parts.",
"context": [
...
]
}
The context message object fields are
message
- an message to be displayed to the usercodeSpan
- a source code span associated with or referenced by the message
{
"message": "getter complexityEntities increase metric value",
"codeSpan": {
...
}
}
Other formats
GitLab, Codeclimate and Checkstyle formats are also supported. Refer to the analyze commands docs to set up them.