What is PHP CPD:
- PHP CPD stands for PHP Copy Paste Detector.
- It is a PEAR tool that makes it easier to find duplicate code in php application.
- 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)
- sudo wget https://phar.phpunit.de/phpcpd.phar
- chmod +x phpcpd.phar
- sudo mv phpcpd.phar /usr/local/bin/phpcpd
Usage:
- 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.
- 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/html - Export report to XML format:
phpcpd –log-pmd projectPhpcpd.xml /var/www/html - 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 - Find phpcpd version
phpcpd –version