CPD

Shipped with PMD, CPD helps identify blocks of duplication across files.

By default, the CPD engine isn’t enabled and isn’t run with a generic scanner run command. To specifically invoke CPD, you use the --engine option, like this:

Because CPD returns duplicated code fragments, meaningful output contains more than one file as a part of a group. Salesforce Code Analyzer represents each group by a short checksum of the corresponding code fragment. Every violation message contains this checksum, the number of tokens in the checksum, the total number of occurrences of this duplicated code, and the index of the current occurrence. This information can help you understand the impact of the duplication.

Consider this sample violation thrown from the CPD engine.

The message shows that there are four other duplicated segments in other files. Find these segments by searching for c42bf68 among the remaining violations detected. The number of lines and tokens in the message shows how large the duplicated block is. The line, column, endLine, and endColumn values help identify the actual block of code fragment within the file.