Skip to content

[Utils] Add token-based duplicate code detector in utils/duplicates - #8482

Open
TomasVotruba wants to merge 1 commit into
mainfrom
tv-phpcpd
Open

[Utils] Add token-based duplicate code detector in utils/duplicates#8482
TomasVotruba wants to merge 1 commit into
mainfrom
tv-phpcpd

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Adds a small copy-paste detector under utils/duplicates, a minimal clone of phpcpd.

How it works

  • Tokenizes PHP with token_get_all() (no php-parser dependency).
  • Strips whitespace, comments and tags, builds a per-token signature (type byte + CRC32 of the token text).
  • Finds exact duplicate token spans with a Rabin-Karp rolling hash over a --min-tokens window, filtered by --min-lines.

Usage

php utils/duplicates/bin/duplicates.php src rules

Options: --min-lines (default 5), --min-tokens (default 70), --fuzzy (ignore variable names). Exit code is 1 when clones are found.

Covered by CloneDetectorTest.

Adds utils/duplicates, a minimal phpcpd clone (token_get_all + Rabin-Karp),
a Duplicated Code CI job failing on copy-paste >= 300 tokens, and extracts the
shared ForeachToArrayFind logic into a factory so src/rules stays under the gate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant