Convert Figma logo to code with AI

sebastianbergmann logophpcpd

Copy/Paste Detector (CPD) for PHP code.

2,214
190
2,214
0

Top Related Projects

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.

12,802

PHP Static Analysis Tool - discover bugs in your code without running it!

5,537

A static analysis tool for finding errors in PHP applications

5,528

Phan is a static analyzer for PHP. Phan prefers to avoid false-positives and attempts to prove incorrectness rather than correctness.

2,321

PHPMD is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly frontend application for the raw metrics stream measured by PHP Depend.

A tool to automatically fix PHP Coding Standards issues

Quick Overview

PHPCPD (PHP Copy/Paste Detector) is a static code analysis tool for detecting duplicated code in PHP projects. It helps developers identify and eliminate code duplication, improving code quality and maintainability.

Pros

  • Easy to use and integrate into existing development workflows
  • Supports various output formats (text, XML, PMD-CPD, CSV)
  • Configurable to exclude specific files, directories, or patterns
  • Can be used as a standalone tool or integrated with Continuous Integration systems

Cons

  • May produce false positives for genuinely similar but necessary code
  • Limited to detecting exact or near-exact code duplications
  • Can be slow on large codebases
  • Requires PHP 7.3 or later to run

Getting Started

  1. Install PHPCPD using Composer:
composer require --dev sebastian/phpcpd
  1. Run PHPCPD on your project:
./vendor/bin/phpcpd src/
  1. Customize the analysis by excluding directories or adjusting minimum line count:
./vendor/bin/phpcpd --exclude vendor/ --min-lines 5 src/
  1. Integrate with CI/CD pipelines by adding it to your build scripts or using pre-commit hooks.

Competitor Comparisons

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.

Pros of PHP_CodeSniffer

  • More comprehensive code analysis, covering style, formatting, and potential errors
  • Highly customizable with support for custom coding standards
  • Integrates well with various IDEs and CI/CD pipelines

Cons of PHP_CodeSniffer

  • Can be slower to run on large codebases compared to PHPCPD
  • May produce more false positives, requiring fine-tuning of rules
  • Steeper learning curve for creating custom sniffs and standards

Code Comparison

PHP_CodeSniffer:

<?php
$phpcsFile = $this->phpcsFile;
$tokens = $phpcsFile->getTokens();
foreach ($tokens as $token) {
    // Analyze token
}

PHPCPD:

<?php
$finder = new Finder();
$finder->files()->in($path);
$strategy = new DefaultStrategy();
$detector = new Detector($strategy);
$clones = $detector->copyPasteDetection($finder);

PHP_CodeSniffer focuses on analyzing individual tokens for style and potential issues, while PHPCPD is specifically designed to detect copy-pasted code blocks. PHP_CodeSniffer offers a more comprehensive analysis of code quality, but PHPCPD excels at its specific task of identifying code duplication.

12,802

PHP Static Analysis Tool - discover bugs in your code without running it!

Pros of PHPStan

  • Performs static analysis to detect bugs and errors without running the code
  • Offers customizable rule sets and supports third-party extensions
  • Provides detailed error messages and suggestions for improvement

Cons of PHPStan

  • Steeper learning curve due to its complexity and configuration options
  • May produce false positives in certain scenarios, requiring fine-tuning

Code Comparison

PHPStan example:

<?php
function foo(int $a, int $b): int {
    return $a + $b;
}
foo('1', 2);

PHPCPD example:

<?php
function add($a, $b) {
    return $a + $b;
}
function sum($x, $y) {
    return $x + $y;
}

Key Differences

  • PHPStan focuses on static analysis and type checking, while PHPCPD detects code duplication
  • PHPStan provides more comprehensive code quality checks, whereas PHPCPD is specialized for identifying copy-pasted code
  • PHPStan requires more setup and configuration, while PHPCPD is simpler to use out of the box

Use Cases

  • Use PHPStan for in-depth code analysis and to catch potential bugs early in development
  • Use PHPCPD to identify and refactor duplicate code, improving maintainability

Both tools can be valuable additions to a PHP development workflow, complementing each other to enhance overall code quality.

5,537

A static analysis tool for finding errors in PHP applications

Pros of Psalm

  • More comprehensive static analysis tool, covering a wider range of potential issues
  • Supports type inference and advanced type checking
  • Integrates with popular IDEs and CI/CD pipelines

Cons of Psalm

  • Steeper learning curve due to its extensive feature set
  • May produce more false positives, requiring configuration tuning
  • Slower analysis time for large codebases compared to PHPCPD

Code Comparison

PHPCPD (Copy/Paste Detector):

<?php
function greet($name) {
    echo "Hello, " . $name . "!";
}

Psalm (Static Analysis):

<?php
/** @param string $name */
function greet(string $name): void {
    echo "Hello, " . $name . "!";
}

PHPCPD focuses on detecting duplicate code, while Psalm performs in-depth static analysis, including type checking and potential error detection. Psalm's example shows type hints and docblock annotations, which PHPCPD doesn't consider in its analysis.

5,528

Phan is a static analyzer for PHP. Phan prefers to avoid false-positives and attempts to prove incorrectness rather than correctness.

Pros of phan

  • More comprehensive static analysis, detecting a wider range of issues
  • Type inference and advanced type checking capabilities
  • Customizable and extensible through plugins

Cons of phan

  • Steeper learning curve due to its complexity
  • Potentially longer analysis time for large codebases
  • May produce more false positives requiring configuration tuning

Code Comparison

phan example:

<?php
function example(int $a, string $b): void {
    echo $a + $b; // Phan detects type mismatch
}

phpcpd example:

<?php
function foo() {
    echo "Hello, world!";
}
function bar() {
    echo "Hello, world!"; // phpcpd detects code duplication
}

Key Differences

  • Focus: phan is a static analyzer for detecting various issues, while phpcpd specifically targets copy-paste detection
  • Scope: phan offers broader code analysis, whereas phpcpd is specialized in identifying duplicated code
  • Complexity: phan is more feature-rich and complex, while phpcpd is simpler and focused on a single task

Use Cases

  • phan: Ideal for comprehensive code quality checks and type-related error detection
  • phpcpd: Best suited for identifying and reducing code duplication in PHP projects

Both tools can be valuable in a PHP development workflow, with phan providing deeper analysis and phpcpd offering quick insights into code redundancy.

2,321

PHPMD is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly frontend application for the raw metrics stream measured by PHP Depend.

Pros of PHPMD

  • Offers a wider range of code quality checks, including complexity, unused code, and naming conventions
  • Provides more detailed and actionable feedback on potential issues in the codebase
  • Highly configurable with custom rulesets and the ability to suppress specific warnings

Cons of PHPMD

  • May produce more false positives compared to PHPCPD, requiring more manual review
  • Can be slower to run on large codebases due to its comprehensive analysis
  • Has a steeper learning curve for configuring and customizing rulesets

Code Comparison

PHPMD example:

<?php
class Example {
    public function complexMethod($a, $b, $c) {
        if ($a && $b || $c) {
            // Complex logic here
        }
    }
}

PHPCPD example:

<?php
function duplicateCode() {
    echo "This is some duplicate code";
    // More duplicate code...
}

function anotherFunction() {
    echo "This is some duplicate code";
    // More duplicate code...
}

PHPMD would flag the complexMethod for high cyclomatic complexity, while PHPCPD would detect the duplicate code in duplicateCode and anotherFunction.

A tool to automatically fix PHP Coding Standards issues

Pros of PHP-CS-Fixer

  • Offers a wide range of coding style and code quality fixers
  • Highly configurable with support for custom rulesets
  • Actively maintained with frequent updates and improvements

Cons of PHP-CS-Fixer

  • Can be slower for large codebases due to its comprehensive analysis
  • May require more setup and configuration compared to simpler tools
  • Focuses on style and formatting, not specifically on copy-paste detection

Code Comparison

PHP-CS-Fixer example:

$config = new PhpCsFixer\Config();
return $config->setRules([
    '@PSR2' => true,
    'array_syntax' => ['syntax' => 'short'],
    'no_unused_imports' => true,
]);

PHPCPD example:

$finder = new Finder();
$finder->in(__DIR__)->name('*.php');
$cpd = new PHPCPD\PHPCPD();
$report = $cpd->run($finder);

PHP-CS-Fixer is a comprehensive tool for fixing coding standards and style issues in PHP code. It offers a wide range of fixers and is highly configurable. On the other hand, PHPCPD (PHP Copy/Paste Detector) is specifically designed to detect duplicated code in PHP projects. While PHP-CS-Fixer is more versatile for overall code quality improvement, PHPCPD excels at identifying potential code duplication issues. The choice between the two depends on the specific needs of your project and development workflow.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

This project is no longer maintained and its repository is only kept for archival purposes.

PHP Copy/Paste Detector (PHPCPD)

phpcpd is a Copy/Paste Detector (CPD) for PHP code.

Installation

This tool is distributed as a PHP Archive (PHAR):

$ wget https://phar.phpunit.de/phpcpd.phar

$ php phpcpd.phar --version

Using Phive is the recommended way for managing the tool dependencies of your project:

$ phive install phpcpd

$ ./tools/phpcpd --version

It is not recommended to use Composer to download and install this tool.

Usage Example

$ php phpcpd.phar --fuzzy wordpress-5.5
phpcpd 6.0.0 by Sebastian Bergmann.

Found 121 clones with 8137 duplicated lines in 69 files:

  - /home/sb/wordpress-5.5/wp-includes/sodium_compat/src/Core/Curve25519/H.php:19-1466 (1447 lines)
    /home/sb/wordpress-5.5/wp-includes/sodium_compat/src/Core32/Curve25519/H.php:19-1466
.
.
.
  - /home/sb/wordpress-5.5/wp-includes/sodium_compat/src/Core32/Curve25519.php:879-889 (10 lines)
    /home/sb/wordpress-5.5/wp-includes/sodium_compat/src/Core32/Curve25519.php:1072-1082

1.82% duplicated lines out of 446676 total lines of code.
Average size of duplication is 67 lines, largest clone has 1447 of lines

Time: 00:02.980, Memory: 318.00 MB