Skip to content

Optional code duplication analysis #550

Description

@nartzis

Feature request

PHP Metrics already provides a comprehensive static analysis report for PHP projects, including HTML, JSON, CSV and CI-oriented outputs.

Would you consider adding an optional token-based code duplication analysis?

Many PHP projects currently use JSCPD for copy-paste detection, even when the rest of their quality toolchain is PHP-based. A native duplication analyzer in PHP Metrics could provide a pure-PHP alternative and make duplication results part of the same report as complexity, coupling and maintainability metrics.

A possible configuration could look like this:

{
  "duplication": {
    "enabled": true,
    "min-lines": 5,
    "min-tokens": 70,
    "fuzzy": false
  }
}

The result could be exposed as a separate report section rather than as a per-class metric, because a clone usually spans multiple files and is represented by pairs or groups of source ranges.

For example, the JSON report could include:

{
  "duplication": {
    "clone_groups": 3,
    "duplicated_lines": 87,
    "duplicated_lines_percentage": 4.8,
    "clones": [
      {
        "tokens": 150,
        "lines": 12,
        "first": {
          "file": "src/Foo.php",
          "start_line": 20,
          "end_line": 31
        },
        "second": {
          "file": "src/Bar.php",
          "start_line": 44,
          "end_line": 55
        }
      }
    ]
  }
}

The feature could also optionally support a CI threshold, for example failing when the duplicated-lines percentage or the number of clone groups exceeds a configured value.

The detector could be token-based and use PHP's tokenizer, or another implementation considered appropriate by the project. The recently added duplicate detector in Rector is one possible reference, but this request is about the capability rather than reusing Rector's internal implementation.

Important requirements would probably include:

  • optional, disabled by default;
  • configurable minimum lines and tokens;
  • include/exclude support consistent with the existing analyzer;
  • JSON and HTML output;
  • deterministic source locations;
  • CI-friendly exit status or violations output;
  • tests for exact and fuzzy duplicates;
  • protection against double-counting overlapping clone ranges.

The Rector discussion related to the reference implementation is available here:

rectorphp/rector-src#8482 (comment)

The author of the original implementation confirmed that others are welcome to extract and reuse the approach. This could provide a useful starting point, while still allowing PHP Metrics to adapt the detector and result model to its own architecture and reporting formats.

I would be happy to contribute to the development of this feature. I can prepare an initial Merge Request, starting with the detector, configuration and tests, if this direction fits the scope and roadmap of PHP Metrics.

Would this kind of feature fit the scope and direction of PHP Metrics?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions