Search code examples
phprefactoringcopy-pastecode-duplication

Identifying repeated code within PHP project


I have a single PHP file within a legacy project that is at least a few thousand lines long. It is predominantly separated up into a number of different conditional blocks by a switch statement with about 10 cases. Within each case there is what appears to be a very similar - if not exact duplicate - block of code. What methods are available for me identifying these blocks of code as being either the same - or close to the same - so I can abstract that code out and begin to refactor the entire file? I know this is possible in very manual terms (separate each case statement in the code into individual files and Diff) but i'm interested in what tools i could be using to speed this process up.

Thanks.


Solution

  • You can use phpcpd.

    phpcpd is a Copy/Paste Detector (CPD) for PHP code. It scans a PHP project for duplicated code.

    Further resources: