Skip to main content
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 a metrics report included into the result.

Full command description:

Usage: dcm calculate-metrics [arguments] <directories>
-h, --help Print this usage information.


-r, --reporter=<console> Analysis output format.
[console (default), html, json, codeclimate, gitlab, checkstyle]
-o, --output-directory=<OUTPUT> Write HTML output to OUTPUT.
(defaults to "metrics")
--open Automatically open generated HTML report.
--json-path=<path/to/file.json> Path to the JSON file with the analysis output.
--report-all Report all metric results (default is to report only high or very high metric levels, works only for the console reporter).


-c, --print-config Print resolved config.


--root-folder=<./> Root folder.
(defaults to the 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.


--verbose Show verbose logs.


--ci-key The license key to run on CI server. Can be provided via DCM_CI_KEY env variable.
--email The email used to purchase the license. Can be provided via DCM_EMAIL env variable.


--no-analytics Disable sending anonymous usage statistics.


--fatal-level=<high> Treat given or higher metric level as fatal.
[near, high, very high]

Output example

Console

Use --reporter=console to enable this format.

Console

HTML

Use --reporter=html to enable this format.

HTML report overview

HTML

HTML single file report

HTML

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 format
  • records - an array of objects
  • summary - 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 file
  • fileMetrics - an array with target file metrics
  • classes - a map with class name as the key and class report as the value
  • functions - 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 record
  • value - 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 - a source code span of the target entity
  • metrics - an array with target entity metrics
{
"codeSpan": {
...
},
"metrics": [
...
]
}

The code span object fields are

  • start - a start location of an entity
  • end - an end location of an entity
  • text - 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 source
  • line - a zero-based line of the location in the source
  • column - 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 metric
  • value - an actual value computed by the metric
  • unitType - a human readable unit type (optional)
  • level - a level of the value computed by the metric
  • comment - a message with information about the value
  • recommendation - 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 user
  • codeSpan - a source code span associated with or referenced by the message
{
"message": "getter complexityEntities increase metric value",
"codeSpan": {
...
}
}

GitLab and Code Climate

GitLab and Code Climate formats are also supported. Refer to the analyze commands docs to set up them.

Checkstyle

Use --reporter=checkstyle to enable this format.

<?xml version="1.0"?>
<checkstyle version="10.0">
<file name="../abstract_class.dart">
<error line="0" message="metric comment" severity="warning" source="file-metric-id"/>
<error line="0" severity="error" message="metric comment" source="id"/>
</file>
<file name="../class_with_factory_constructors.dart">
<error line="0" severity="warning" message="metric comment" source="id"/>
</file>
</checkstyle>