PHP CPD (PHP Copy Paste Detector) – Detecting duplicate code in PHP files


What is PHP CPD:

  1. PHP CPD stands for PHP Copy Paste Detector.
  2. It is a PEAR tool that makes it easier to find duplicate code in php application.
  3. Copy and Paste coding is a common development practice among programmers so this tool will help to detect duplicate codes.

Installation: (Open terminal/command line and run below command)

  1. sudo wget
  2. chmod +x phpcpd.phar
  3. sudo mv phpcpd.phar /usr/local/bin/phpcpd


  1. Default command to run:
    phpcpd <file_name or directoy_name>
    Ex: phpcpd /var/www/html/edit.php
           phpcpd /var/www/html
    In below screen, it does not show report on duplicate code due to very less line of duplicate code as phpcpd searches for a minimum of 5 identical lines and 70 identical tokens. Now let’s go to the next point and see the report.
    Screen Shot 2016-02-14 at 10.18.31 am
    Screen Shot 2016-02-14 at 10.10.07 am   Screen Shot 2016-02-14 at 10.09.58 am
  2. Set a minimum line of duplicate line of code to find:
    By default, phpcpd searches for a minimum of 5 identical lines and 70 identical tokens so it ignores code having less than 5 duplicate lines or less than 70 identical tokens. options as –min-lines and –min-tokens can be used to override this.
    phpcpd –min-lines 2 –min-tokens 2 /var/www/htmlScreen Shot 2016-02-14 at 10.18.49 am
  3. Export report to XML format:
    phpcpd –log-pmd projectPhpcpd.xml /var/www/html
  4. Add PHP extension to parse:
    phpcpd uses .php extension by default when comparing files. Using –suffixes, more other PHP extension files can be included to parse for duplicate code.
    phpcpd –suffixes php,php5 /var/www/html
  5. Find phpcpd version
    phpcpd –version

About the Author

Sandeep Agrawal
working @ Kalpavruksh Technologies as Software Engineer. I am a simple and straightforward person adaptable to any environment and work culture. Like to work in a creative and challenging environment using cutting edge technologies where I could constantly learn and successfully deliver solutions to problems.
%d bloggers like this: