Skip to main content
Teams

Check Code Duplication

Checks for duplicate function, methods and constructors.

This command is resistant to some insignificant code changes (for example, a method with renamed parameters or variables is still considered a duplicate) as it performs a structural check rather than an exact match check.

To execute the command, run:

$ dcm check-code-duplication lib # or dcm cd lib

Full command description:

-h, --help                                         Print this usage information.


--per-package Compare code for duplications only within one package.
--exclude-overrides Exclude methods marked with @override.
--statements-threshold Minimum number of statements inside a declaration block.
(defaults to "3")


-r, --reporter=<console> The format of the output of the analysis.
[console (default), codeclimate, json, gitlab]
--json-path=<path/to/file.json> Path to the JSON file with the output of the analysis.


-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 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.


--[no-]fatal-found Treat found code duplications as fatal.
(defaults to on)

Suppressing the command

In order to suppress the command add the ignore: code-duplication comment. To suppress for an entire file add ignore_for_file: code-duplication to the beginning of a file.

Calculating duplication on per-package level

To compare code for duplication only within one package, pass the --per-package CLI flag.

Use this mode if you want to avoid unifying code between several packages.

Excluding overrides

To exclude methods marked with the "@override" annotation, pass the --exclude-overrides CLI flag.

Configuring the minimum number of statements

By default, the command checks for declarations with at least 3 statements and declarations with an expression body (e.g. (String param) => someMethod();).

To configure the minimum number of statements, pass the --statements-threshold CLI option.

info

Blocks with a single return statement and expression bodies are handled differently. This configuration does not affect them.

Output example

Console

Use --reporter=console to enable this format.

Console

JSON

The reporter prints a single JSON object containing meta information and the list of duplication issues.

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
  • codeDuplications - an array of code duplication issues
{
"formatVersion": 2,
"timestamp": "2021-04-11 14:44:42",
"codeDuplications": [
{
...
},
{
...
},
{
...
}
]
}

The codeDuplication object fields are

  • path - a relative path of the unused file
  • issues - an array of issues detected in the target file
{
"path": "lib/src/some/file.dart",
"issues": [
...
],
}

The issue object fields are

  • declarationType - duplication declaration type
  • declarationName - duplication declaration name
  • duplications - an array of duplication entries for this declaration
  • offset - a zero-based offset of the class member location in the source
  • line - a zero-based line of the class member location in the source
  • column - a zero-based column of class member the location in the source
{
"declarationType": "function",
"declarationName": "myFunction",
"duplications": [
...
],
"offset": 156,
"line": 7,
"column": 1
}

The duplication object fields are

  • declarationType - duplication entry declaration type
  • declarationName - duplication entry declaration name
  • relativePath - path, relative to the root directory
  • relativeToFirstDeclarationPath - path, relative to the main code duplication declaration
  • offset - a zero-based offset of the class member location in the source
  • line - a zero-based line of the class member location in the source
  • column - a zero-based column of class member the location in the source
{
"declarationType": "method",
"declarationName": "someMethod",
"relativePath": "lib/src/some/another_file.dart",
"relativeToFirstDeclarationPath": "./another_file.dart",
"offset": 156,
"line": 7,
"column": 1
}

GitLab

Reports unused code issues in merge requests based on Code Quality custom tool. Use --reporter=gitlab to enable this format.